[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.40,1.41 test_datetime.py,1.26,1.27

Tim Peters tim_one@users.sourceforge.net
Mon, 04 Mar 2002 10:49:10 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory usw-pr-cvs1:/tmp/cvs-serv29066

Modified Files:
	datetime.py test_datetime.py 
Log Message:
datetime.__add__:  raise OverflowError if result out of range, meaning
specifcally that the result year is out of range.

New test test_overflow.

Note:  tmxxx intentionally ignores year out of range, and should ignore it.
Else, e.g., timezone adjustments done for internal datetime purposes of
hashing or comparing could raise bogus OverflowErrors at the boundaries:
some legit datetime objects can't be converted to legit UTC datetimes.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.40
retrieving revision 1.41
diff -C2 -d -r1.40 -r1.41
*** datetime.py	4 Mar 2002 15:34:29 -0000	1.40
--- datetime.py	4 Mar 2002 18:49:07 -0000	1.41
***************
*** 655,658 ****
--- 655,663 ----
      # Computations
  
+     def _checkOverflow(self, year):
+         if not MINYEAR <= year <= MAXYEAR:
+             raise OverflowError("datetime +/-: result year %d not in %d..%d" %
+                                 (year, MINYEAR, MAXYEAR))
+ 
      def __add__(self, other):
          "Add a datetime to a timedelta."
***************
*** 665,668 ****
--- 670,674 ----
                        self.__second + other.seconds,
                        self.__microsecond + other.microseconds)
+             self._checkOverflow(t.year)
              result = datetime.new(t.year, t.month, t.day,
                                    t.hour, t.minute, t.second,

Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_datetime.py,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** test_datetime.py	4 Mar 2002 18:32:07 -0000	1.26
--- test_datetime.py	4 Mar 2002 18:49:07 -0000	1.27
***************
*** 253,256 ****
--- 253,271 ----
              self.assertRaises(TypeError, lambda: i-a)
  
+     def test_overflow(self):
+         us = timedelta(microseconds=1)
+         mus = - us
+ 
+         dt = datetime.new(MINYEAR, 1, 1, microsecond=1)
+         dt -= us    # no problem
+         self.assertRaises(OverflowError, dt.__sub__, us)
+         self.assertRaises(OverflowError, dt.__add__, mus)
+ 
+         dt = datetime.new(MAXYEAR, 12, 31, 23, 59, 59, 999998)
+         dt += us    # no problem
+         self.assertRaises(OverflowError, dt.__add__, us)
+         self.assertRaises(OverflowError, dt.__sub__, mus)
+ 
+         
      def test_weekday(self):
          for i in range(7):