[Python-checkins] r82605 - in sandbox/branches/py3k-datetime: datetime.py test_datetime.py

alexander.belopolsky python-checkins at python.org
Tue Jul 6 15:54:53 CEST 2010


Author: alexander.belopolsky
Date: Tue Jul  6 15:54:47 2010
New Revision: 82605

Log:
Added type check for better match with C implementation

Modified:
   sandbox/branches/py3k-datetime/datetime.py
   sandbox/branches/py3k-datetime/test_datetime.py

Modified: sandbox/branches/py3k-datetime/datetime.py
==============================================================================
--- sandbox/branches/py3k-datetime/datetime.py	(original)
+++ sandbox/branches/py3k-datetime/datetime.py	Tue Jul  6 15:54:47 2010
@@ -665,7 +665,8 @@
 
         year, month, day (required, base 1)
         """
-        if isinstance(year, bytes) and len(year) == 4:
+        if (isinstance(year, bytes) and len(year) == 4 and
+            1 <= year[2] <= 12 and month is None):  # Month is sane
             # Pickle support
             self = object.__new__(cls)
             self.__setstate(year)
@@ -978,6 +979,8 @@
         else:
             return (self.__class__, args, state)
 
+_tzinfo_class = tzinfo
+
 class time:
     """Time with time zone.
 
@@ -1272,7 +1275,10 @@
         (self._hour, self._minute, self._second,
          us1, us2, us3) = string
         self._microsecond = (((us1 << 8) | us2) << 8) | us3
-        self._tzinfo = tzinfo
+        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 (time, self._getstate())
@@ -1741,7 +1747,10 @@
          self._minute, self._second, us1, us2, us3) = string
         self._year = yhi * 256 + ylo
         self._microsecond = (((us1 << 8) | us2) << 8) | us3
-        self._tzinfo = tzinfo
+        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())

Modified: sandbox/branches/py3k-datetime/test_datetime.py
==============================================================================
--- sandbox/branches/py3k-datetime/test_datetime.py	(original)
+++ sandbox/branches/py3k-datetime/test_datetime.py	Tue Jul  6 15:54:47 2010
@@ -69,34 +69,11 @@
         return self.__name
     def dst(self, dt):
         return self.__dstoffset
-    def __eq__(self, other):
-        return (self.__offset == other.__offset and
-                self.__name == other.__name and
-                self.__dstoffset == other.__dstoffset)
-    def __getinitargs__(self):
-        return (self.__offset,
-                self.__name,
-                self.__dstoffset)
 
 class PicklableFixedOffset(FixedOffset):
 
     def __init__(self, offset=None, name=None, dstoffset=None):
         FixedOffset.__init__(self, offset, name, dstoffset)
-        
-
-class PicklableFixedOffsetWithState(PicklableFixedOffset):
-
-    def __init__(self, offset=None, name=None, dstoffset=None,
-                 state=None):
-        FixedOffset.__init__(self, offset, name, dstoffset)
-        self.__state = state
-    def __getstate__(self):
-        return self.__state
-    def __setstate__(self, state):
-        self.__state = state
-    def __eq__(self, other):
-        return (FixedOffset.__eq__(self, other) and
-                self.__state == other.__state)
 
 class TestTZInfo(unittest.TestCase):
 
@@ -148,7 +125,6 @@
         offset = timedelta(minutes=-300)
         for otype, args in [
             (PicklableFixedOffset, (offset, 'cookie')),
-            (PicklableFixedOffsetWithState, (offset, 'cookie', 'state')),
             (timezone, (offset,)),
             (timezone, (offset, "EST"))]:
             orig = otype(*args)
@@ -164,7 +140,6 @@
                 self.assertIs(type(derived), otype)
                 self.assertEqual(derived.utcoffset(None), offset)
                 self.assertEqual(derived.tzname(None), oname)
-                self.assertEqual(derived, orig)
 
 class TestTimeZone(unittest.TestCase):
 
@@ -261,33 +236,6 @@
                              t.replace(tzinfo=tz).dst())
 
 #############################################################################
-# Base clase for testing pickling state of timedelta, time, date and
-# datetime instances.
-
-class ReduceStructure:
-    initargs = 1, 1, 1
-    statetype = (bytes, tzinfo)
-    def test_reduce(self):
-        obj = self.theclass(*self.initargs)
-        factory, state = obj.__reduce__()[:2]
-        self.assertIs(factory, self.theclass)
-        for v, t in zip(state, self.statetype):
-            self.assertIsInstance(v, t)
-
-    def test_construct_from_state(self):
-        obj = self.theclass(*self.initargs)
-        factory, state = obj.__reduce__()[:2]
-        initbytes = state[0]
-        # This test should be overriden in tests for classes that
-        # don't produce their state as bytes.  Curently, timedelta
-        # and timezone
-        assert isinstance(initbytes, bytes)
-        obj = factory(initbytes)
-        self.assertIs(type(obj), self.theclass)
-        self.assertRaises(TypeError, factory, initbytes[:-1]) 
-        self.assertRaises(TypeError, factory, initbytes + b'x') 
-
-#############################################################################
 # Base clase for testing a particular aspect of timedelta, time, date and
 # datetime comparisons.
 
@@ -324,10 +272,10 @@
 #############################################################################
 # timedelta tests
 
-class TestTimeDelta(HarmlessMixedComparison, ReduceStructure, unittest.TestCase):
+class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
 
     theclass = timedelta
-    statetype = (int, int, int)
+
     def test_constructor(self):
         eq = self.assertEqual
         td = timedelta
@@ -353,9 +301,6 @@
         eq(td(seconds=0.001), td(milliseconds=1))
         eq(td(milliseconds=0.001), td(microseconds=1))
 
-    def test_construct_from_state(self):
-        pass
-
     def test_computations(self):
         eq = self.assertEqual
         td = timedelta
@@ -788,12 +733,11 @@
 class SubclassDate(date):
     sub_var = 1
 
-class TestDate(HarmlessMixedComparison, ReduceStructure, unittest.TestCase):
+class TestDate(HarmlessMixedComparison, unittest.TestCase):
     # Tests here should pass for both dates and datetimes, except for a
     # few tests that TestDateTime overrides.
 
     theclass = date
-    statetype = (bytes,)
 
     def test_basic_attributes(self):
         dt = self.theclass(2002, 3, 1)
@@ -937,6 +881,7 @@
     def test_computations(self):
         a = self.theclass(2002, 1, 31)
         b = self.theclass(1956, 1, 31)
+        c = self.theclass(2001,2,1)
 
         diff = a-b
         self.assertEqual(diff.days, 46*365 + len(range(1956, 2002, 4)))
@@ -962,6 +907,7 @@
         self.assertEqual(a - (a + day), -day)
         self.assertEqual(a - (a - week), week)
         self.assertEqual(a - (a - day), day)
+        self.assertEqual(c - (c - day), day)
 
         # Add/sub ints or floats should be illegal
         for i in 1, 1.0:
@@ -1403,6 +1349,10 @@
         for month_byte in b'9', b'\0', b'\r', b'\xff':
             self.assertRaises(TypeError, self.theclass,
                                          base[:2] + month_byte + base[3:])
+        # Good bytes, but bad tzinfo:
+        self.assertRaises(TypeError, self.theclass,
+                          bytes([1] * len(base)), 'EST')
+
         for ord_byte in range(1, 13):
             # This shouldn't blow up because of the month byte alone.  If
             # the implementation changes to do more-careful checking, it may
@@ -1418,7 +1368,6 @@
 class TestDateTime(TestDate):
 
     theclass = datetime
-    statetype = (bytes, tzinfo)
 
     def test_basic_attributes(self):
         dt = self.theclass(2002, 3, 1, 12, 0)
@@ -1767,8 +1716,6 @@
         # than one microsecond smaller than an integer.
         self.assertEqual(self.theclass.fromtimestamp(0.9999999),
                          self.theclass.fromtimestamp(1))
-        self.assertEqual(self.theclass.utcfromtimestamp(0.9999999),
-                         self.theclass.utcfromtimestamp(1))
 
     def test_insane_fromtimestamp(self):
         # It's possible that some platform maps time_t to double,
@@ -1996,7 +1943,7 @@
 class SubclassTime(time):
     sub_var = 1
 
-class TestTime(HarmlessMixedComparison, ReduceStructure, unittest.TestCase):
+class TestTime(HarmlessMixedComparison, unittest.TestCase):
 
     theclass = time
 
@@ -3067,7 +3014,7 @@
 
     def test_utctimetuple(self):
         class DST(tzinfo):
-            def __init__(self, dstvalue):
+            def __init__(self, dstvalue=0):
                 if isinstance(dstvalue, int):
                     dstvalue = timedelta(minutes=dstvalue)
                 self.dstvalue = dstvalue
@@ -3102,6 +3049,26 @@
             # is never in effect for a UTC time.
             self.assertEqual(0, t.tm_isdst)
 
+        # For naive datetime, utctimetuple == timetuple except for isdst
+        d = cls(1, 2, 3, 10, 20, 30, 40)
+        t = d.utctimetuple()
+        self.assertEqual(t[:-1], d.timetuple()[:-1])
+        self.assertEqual(0, t.tm_isdst)
+        # Same if utcoffset is None
+        class NOFS(DST):
+            def utcoffset(self, dt):
+                return None
+        d = cls(1, 2, 3, 10, 20, 30, 40, tzinfo=NOFS())
+        t = d.utctimetuple()
+        self.assertEqual(t[:-1], d.timetuple()[:-1])
+        self.assertEqual(0, t.tm_isdst)
+        # Check that bad tzinfo is detected
+        class BOFS(DST):
+            def utcoffset(self, dt):
+                return "EST"
+        d = cls(1, 2, 3, 10, 20, 30, 40, tzinfo=BOFS())
+        self.assertRaises(TypeError, d.utctimetuple)
+
         # Check that utctimetuple() is the same as
         # astimezone(utc).timetuple()
         d = cls(2010, 11, 13, 14, 15, 16, 171819)


More information about the Python-checkins mailing list