簡體   English   中英

在python3下取消python2 datetime

[英]Unpickling python2 datetime under python3

我選擇使用pickle(+ base64 + TCP套接字)在我的python3代碼和傳統python2代碼之間傳遞數據,但是我遇到了datetime對象的問題:

PY3對象在PY2上表現不佳,但反向在調用datetime構造函數時引發TypeError ,然后在load_reduce函數中引發UnicodeEncodeError

這個要點有一個簡短的測試程序和日志,包括PY2和PY3泡菜的輸出

  • 我在pickle.dumps(reply, protocol=2)使用pickle.dumps(reply, protocol=2)
    然后在pickle._loads(pickled, fix_imports=True, encoding='latin1')
    (嘗試無和utf-8沒有成功)

  • 原生cPickle loads解碼也失敗了,我只使用純python的_loads進行調試。

這是一個datetime錯誤嗎? 也許datetime.__getstate__/__setstate__實現不兼容?

歡迎任何關於代碼的評論......

補充

PY-3.4.0泡菜:

 0: \x80 PROTO      2
 2: c    GLOBAL     'datetime datetime'
21: q    BINPUT     0
23: c    GLOBAL     '_codecs encode'
39: q    BINPUT     1
41: X    BINUNICODE u'\x07\xde\x07\x11\x0f\x06\x11\x05\n\x90'
58: q    BINPUT     2
60: X    BINUNICODE u'latin1'
71: q    BINPUT     3
73: \x86 TUPLE2
74: q    BINPUT     4
76: R    REDUCE
77: q    BINPUT     5
79: \x85 TUPLE1
80: q    BINPUT     6
82: R    REDUCE
83: q    BINPUT     7
85: .    STOP

PY-2.7.6泡菜:

 0: \\x80 PROTO      2
 2: c    GLOBAL     'datetime datetime'
21: q    BINPUT     0
23: U    SHORT_BINSTRING '\\x07\xc3\x9e\\x07\\x11\\x0f\\x06\\x11\\x05\\n\\x90'
35: q    BINPUT     1
37: \\x85 TUPLE1
38: q    BINPUT     2
40: R    REDUCE
41: q    BINPUT     3
43: ]    EMPTY_LIST
44: q    BINPUT     4
46: N    NONE
47: \\x87 TUPLE3
48: q    BINPUT     5
50: .    STOP

PY-3.4.0 pickle.load_reduce

def load_reduce(self):
    stack = self.stack
    args = stack.pop()
    func = stack[-1]
    try:
        value = func(*args)
    except:
        print(sys.exc_info())
        print(func, args)
        raise
    stack[-1] = value
dispatch[REDUCE[0]] = load_reduce

PY-3.4.0 datetime泡菜支持:

# Pickle support.

def _getstate(self):
    yhi, ylo = divmod(self._year, 256)
    us2, us3 = divmod(self._microsecond, 256)
    us1, us2 = divmod(us2, 256)
    basestate = bytes([yhi, ylo, self._month, self._day,
                       self._hour, self._minute, self._second,
                       us1, us2, us3])
    if self._tzinfo is None:
        return (basestate,)
    else:
        return (basestate, self._tzinfo)

def __setstate(self, string, tzinfo):
    (yhi, ylo, self._month, self._day, self._hour,
     self._minute, self._second, us1, us2, us3) = string
    self._year = yhi * 256 + ylo
    self._microsecond = (((us1 << 8) | us2) << 8) | us3
    if tzinfo is None or isinstance(tzinfo, _tzinfo_class):
        self._tzinfo = tzinfo
    else:
        raise TypeError("bad tzinfo state arg %r" % tzinfo)

def __reduce__(self):
    return (self.__class__, self._getstate())

解決方法是使用encoding="bytes"如下所示:

pickled_bytes = bytes(pickled_str, encoding='latin1')  # If your input is a string(not my case)
data = pickle.loads(pickled_bytes, encoding='bytes')

(感謝Tim Peters提出的建議)

問題仍然在http://bugs.python.org/issue22005上打開,說明為什么需要這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM