Mailing List Archive

python/nondist/sandbox/datetime datetime.py,1.110,1.111 doc.txt,1.60,1.61
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv14047

Modified Files:
datetime.py doc.txt
Log Message:
Tried to straighten out the cross-version pickle mess. This was wholly
successful in one direction (the Python implementation can read pickles
produced by either implementation), but only half successful in the other
direction. Unfortunately, the other direction is the more important one.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.110
retrieving revision 1.111
diff -C2 -d -r1.110 -r1.111
*** datetime.py 16 Dec 2002 21:20:42 -0000 1.110
--- datetime.py 17 Dec 2002 02:04:41 -0000 1.111
***************
*** 542,545 ****
--- 542,550 ----
self.__microseconds != 0)

+ # Pickle support.
+ # This magic class attr is necessary for pickle compatibility with the
+ # C implementation.
+ __safe_for_unpickling__ = True
+
def __getstate__(self):
return (self.__days, self.__seconds, self.__microseconds)
***************
*** 1587,1590 ****
--- 1592,1666 ----
return week1monday

+ # Pickle support. __getstate__ and __setstate__ work fine on their own,
+ # but only because the classes here are implemented in Python. The C
+ # implementation had to get much trickier, and the code following emulates
+ # what the C code had to do, so that pickles produced by the Python
+ # implementation can be read by the C implementation, and vice versa.
+ # XXX This isn't entirely successful yet. The Python implementation can
+ # XXX read pickles written by the C implementation now, but the
+ # XXX C implementation can't read timetz, datetimetz, or timedelta
+ # XXX pickles written by the Python implementation. See doc.txt.
+
+ def _date_pickler(date):
+ state = date.__getstate__()
+ return _date_unpickler, (state,)
+
+ def _date_unpickler(state):
+ self = date(1, 1, 1)
+ self.__setstate__(state)
+ return self
+
+ def _datetime_pickler(dt):
+ state = dt.__getstate__()
+ return _datetime_unpickler, (state,)
+
+ def _datetime_unpickler(state):
+ self = datetime(1, 1, 1)
+ self.__setstate__(state)
+ return self
+
+ def _time_pickler(t):
+ state = t.__getstate__()
+ return _time_unpickler, (state,)
+
+ def _time_unpickler(state):
+ self = time()
+ self.__setstate__(state)
+ return self
+
+ def _tzinfo_pickler(tz):
+ return _tzinfo_unpickler, ()
+
+ def _tzinfo_unpickler():
+ self = tzinfo()
+ return self
+
+ def _timetz_pickler(tz):
+ state = tz.__getstate__()
+ return _timetz_unpickler, (state,)
+
+ def _timetz_unpickler(state):
+ self = timetz()
+ self.__setstate__(state)
+ return self
+
+ def _datetimetz_pickler(dtz):
+ state = dtz.__getstate__()
+ return _datetimetz_unpickler, (state,)
+
+ def _datetimetz_unpickler(state):
+ self = datetimetz(1, 1, 1)
+ self.__setstate__(state)
+ return self
+
+ # Register pickle/unpickle functions.
+ from copy_reg import pickle
+ pickle(date, _date_pickler, _date_unpickler)
+ pickle(datetime, _datetime_pickler, _datetime_unpickler)
+ pickle(time, _time_pickler, _time_unpickler)
+ pickle(tzinfo, _tzinfo_pickler, _tzinfo_unpickler)
+ pickle(timetz, _timetz_pickler, _timetz_unpickler)
+ pickle(datetimetz, _datetimetz_pickler, _datetimetz_unpickler)
+ del pickle

def _test():

Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.60
retrieving revision 1.61
diff -C2 -d -r1.60 -r1.61
*** doc.txt 16 Dec 2002 22:59:58 -0000 1.60
--- doc.txt 17 Dec 2002 02:04:41 -0000 1.61
***************
*** 3,6 ****
--- 3,8 ----
- The Python implementation is missing docstrings in many places.

+ - LaTeXize the docs.
+
- Pickle incompatibility? It occurs to me that, because the
implementations are so different, a pickle created by the Python
***************
*** 10,14 ****
and has no wiggle room (TOOWTDI indeed <wink>).

! - LaTeXize the docs.


--- 12,79 ----
and has no wiggle room (TOOWTDI indeed <wink>).

! OK, after lots of fiddling, here's the scoop: the Python implementation
! can now read pickles written by the C and Python implementations. The
! C implementation can read pickles written by the C implementation,
! and date, datetime, and time pickles produced by the Python
! implementation.
!
! However, the C implementation cannot read timedelta, timetz, or
! datetimetz pickles written by the Python implementation. This is
! bad, because Zope3 will use the Python implementation at first; if
! "something isn't done about it", pickles written via the Python
! implementation won't be readable when Zope3 moves to Python 2.3.
!
! Trying to read a Python (2.2.2) timedelta pickle under the C
! implementation (2.3):
!
! File "ptest.py", line 31, in check
! got = pickler.load(f)
! File "C:\CODE\PYTHON\lib\pickle.py", line 1058, in load
! return Unpickler(file).load()
! File "C:\CODE\PYTHON\lib\pickle.py", line 666, in load
! dispatch[key](self)
! File "C:\CODE\PYTHON\lib\pickle.py", line 928, in load_reduce
! value = apply(callable, arg_tup)
! File "C:\CODE\PYTHON\lib\copy_reg.py", line 40, in _reconstructor
! obj = base.__new__(cls, state)
! TypeError: object.__new__(datetime.timedelta) is not safe, use datetime.timedelta.__new__()
!
!
! Trying to read a Python (2.2.2) timetz pickle under the C
! implementation (2.3):
!
! File "ptest.py", line 58, in ?
! t.check()
! File "ptest.py", line 31, in check
! got = pickler.load(f)
! File "C:\CODE\PYTHON\lib\pickle.py", line 1058, in load
! return Unpickler(file).load()
! File "C:\CODE\PYTHON\lib\pickle.py", line 666, in load
! dispatch[key](self)
! File "C:\CODE\PYTHON\lib\pickle.py", line 928, in load_reduce
! value = apply(callable, arg_tup)
! File "C:\CODE\PYTHON\lib\copy_reg.py", line 40, in _reconstructor
! obj = base.__new__(cls, state)
! TypeError: object.__new__(EST) is not safe, use datetime.tzinfo.__new__()
!
!
! Trying to read a Python (2.2.2) datetimetz pickle under the C
! implementation (2.3):
!
! Traceback (most recent call last):
! File "ptest.py", line 58, in ?
! t.check()
! File "ptest.py", line 31, in check
! got = pickler.load(f)
! File "C:\CODE\PYTHON\lib\pickle.py", line 1058, in load
! return Unpickler(file).load()
! File "C:\CODE\PYTHON\lib\pickle.py", line 666, in load
! dispatch[key](self)
! File "C:\CODE\PYTHON\lib\pickle.py", line 928, in load_reduce
! value = apply(callable, arg_tup)
! File "C:\CODE\PYTHON\lib\copy_reg.py", line 40, in _reconstructor
! obj = base.__new__(cls, state)
! TypeError: object.__new__(EST) is not safe, use datetime.tzinfo.__new__()
!