[Zope3-checkins] CVS: Zope3/lib/python/datetime - doc.txt:1.1 _datetime.py:1.9

Tim Peters tim.one@comcast.net
Sat, 21 Dec 2002 00:11:14 -0500


Update of /cvs-repository/Zope3/lib/python/datetime
In directory cvs.zope.org:/tmp/cvs-serv20074/lib/python/datetime

Modified Files:
	_datetime.py 
Added Files:
	doc.txt 
Log Message:
Changes sufficient so that pickles written by the Python implementation
can be read by the C implementation.  I don't really understand this.

Added the large, but unreviewed & plain-text, doc file from the Python
CVS sandbox.

The docs have changed to note the new requirement that tzinfo subclasses
have _an _init__ method that can be called with no arguments.  They may
also need a __safe_for_unpickling__ magic attr, but that can be added to
a class later (it seems), and I hope won't be needed by the time Python
2.3 is real.


=== Added File Zope3/lib/python/datetime/doc.txt === (1080/1180 lines abridged)
TODO/OPEN
=========
- The Python implementation is missing docstrings in many places.

- LaTeXize the docs.


CLOSED
======
- Pickle incompatibility?  It occurs to me that, because the
  implementations are so different, a pickle created by the Python
  implementation won't be unpicklable by the C implementation, or vice
  versa.  If this is important, the Python implementation will have to
  change, as the C implementation of pickling took days to get working
  and has no wiggle room (TOOWTDI indeed <wink>).

  Resolution:  Jim Fulton suggested adding various __reduce__ methods
  to the Python implementation, and that did lead to pickles that the
  C implementation groks.  I'm not sure why, and tzinfo subclasses must
  have an __init__ that can be called without arguments now (if
  they want to play with pickles).  It appears they must also set
  class attr __safe_for_unpickling__ = True, but only if they want a
  pickle written by the Python implementation to be readable by the C
  implementation.

- The test suite doesn't pass under 2.2.2, due to what Guido tracked
  to a bug in 2.2.2's implementation of __cmp__ for new-style classes.
  Later:  the test suite grew a version check to avoid provoking this
  bug under 2.2.2.

- What should str() do?  It generally acts like a synonym for isoformat()
  now.  But

    >>> print time(2)
    02:00:00.000000
    >>>

  is arguably better as '2:00:00' or even '2:00'.  The Python
  implementation has (overridden) "pretty __str__" according to one
  person's idea of "pretty", for a couple types.  Rat hole.
  Guido sez:  chop ".000000" when microseconds are 0, and that's it.
  Tim sez:  and having a fixed-size string when they are will make
  life easier for people implementing their own ideas of "pretty".
  Later:  isoformat() should also elide trailing microsecond when it's
  0.
  Done.

- pickles still "feel too big".  But this is a general issue w/ new-style
  classes.


[-=- -=- -=- 1080 lines omitted -=- -=- -=-]

    PyDateTime_DateTimeTZ
    PyDateTime_Time
    PyDateTime_TimeTZ
    PyDateTime_Delta
    PyDateTime_TZInfo

Type-check macros:

    PyDate_Check(op)
    PyDate_CheckExact(op)

    PyDateTime_Check(op)
    PyDateTime_CheckExact(op)

    PyDateTimeTZ_Check(op)
    PyDateTimeTZ_CheckExact(op)

    PyTime_Check(op)
    PyTime_CheckExact(op)

    PyTimeTZ_Check(op)
    PyTimeTZ_CheckExact(op)

    PyDelta_Check(op)
    PyDelta_CheckExact(op)

    PyTZInfo_Check(op)
    PyTZInfo_CheckExact(op

Accessor macros:

All objects are immutable, so accessors are read-only.  All macros
return ints:

    For date, datetime, and datetimetz instances:
        PyDateTime_GET_YEAR(o)
        PyDateTime_GET_MONTH(o)
        PyDateTime_GET_DAY(o)

    For datetime and datetimetz instances:
        PyDateTime_DATE_GET_HOUR(o)
        PyDateTime_DATE_GET_MINUTE(o)
        PyDateTime_DATE_GET_SECOND(o)
        PyDateTime_DATE_GET_MICROSECOND(o)

    For time and timetz instances:
        PyDateTime_TIME_GET_HOUR(o)
        PyDateTime_TIME_GET_MINUTE(o)
        PyDateTime_TIME_GET_SECOND(o)
        PyDateTime_TIME_GET_MICROSECOND(o)


=== Zope3/lib/python/datetime/_datetime.py 1.8 => 1.9 ===
--- Zope3/lib/python/datetime/_datetime.py:1.8	Fri Dec 20 17:30:37 2002
+++ Zope3/lib/python/datetime/_datetime.py	Sat Dec 21 00:10:43 2002
@@ -932,6 +932,12 @@
         """
         raise NotImplementedError("tzinfo subclass must override dst()")
 
+    # pickle support
+
+    __safe_for_unpickling__ = True
+
+    def __reduce__(self):
+        return type(self), (), self.__dict__
 
 class timetz(time):
     """Time with time zone.