[Python-checkins] python/dist/src/Lib/test test_time.py, 1.15, 1.16 test_strftime.py, 1.27, 1.28

bcannon at users.sourceforge.net bcannon at users.sourceforge.net
Mon Mar 1 23:38:13 EST 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29826/Lib/test

Modified Files:
	test_time.py test_strftime.py 
Log Message:
Have strftime() check its time tuple argument to make sure the tuple's values
are within proper boundaries as specified in the docs.

This can break possible code (datetime module needed changing, for instance)
that uses 0 for values that need to be greater 1 or greater (month, day, and
day of year).

Fixes bug #897625.


Index: test_time.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_time.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** test_time.py	24 Apr 2003 16:02:53 -0000	1.15
--- test_time.py	2 Mar 2004 04:38:10 -0000	1.16
***************
*** 38,41 ****
--- 38,97 ----
                  self.fail('conversion specifier: %r failed.' % format)
  
+     def test_strftime_bounds_checking(self):
+         # Make sure that strftime() checks the bounds of the various parts
+         #of the time tuple.
+ 
+         # Check year
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1899, 1, 1, 0, 0, 0, 0, 1, -1))
+         if time.accept2dyear:
+             self.assertRaises(ValueError, time.strftime, '',
+                                 (-1, 1, 1, 0, 0, 0, 0, 1, -1))
+             self.assertRaises(ValueError, time.strftime, '',
+                                 (100, 1, 1, 0, 0, 0, 0, 1, -1))
+         # Check month
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 0, 1, 0, 0, 0, 0, 1, -1))
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 13, 1, 0, 0, 0, 0, 1, -1))
+         # Check day of month
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 0, 0, 0, 0, 0, 1, -1))
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 32, 0, 0, 0, 0, 1, -1))
+         # Check hour
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, -1, 0, 0, 0, 1, -1))
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 24, 0, 0, 0, 1, -1))
+         # Check minute
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, -1, 0, 0, 1, -1))
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 60, 0, 0, 1, -1))
+         # Check second
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 0, -1, 0, 1, -1))
+         # C99 only requires allowing for one leap second, but Python's docs say
+         # allow two leap seconds (0..61)
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 0, 62, 0, 1, -1))
+         # No check for upper-bound day of week;
+         #  value forced into range by a ``% 7`` calculation.
+         # Start check at -2 since gettmarg() increments value before taking
+         #  modulo.
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 0, 0, -2, 1, -1))
+         # Check day of the year
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 0, 0, 0, 0, -1))
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 0, 0, 0, 367, -1))
+         # Check daylight savings flag
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 0, 0, 0, 1, -2))
+         self.assertRaises(ValueError, time.strftime, '',
+                             (1900, 1, 1, 0, 0, 0, 0, 1, 2))
+ 
      def test_strptime(self):
          tt = time.gmtime(self.t)

Index: test_strftime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_strftime.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** test_strftime.py	23 Jul 2002 19:04:03 -0000	1.27
--- test_strftime.py	2 Mar 2004 04:38:10 -0000	1.28
***************
*** 39,43 ****
      else: ampm='(PM|pm)'
  
!     jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
  
      try:
--- 39,43 ----
      else: ampm='(PM|pm)'
  
!     jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0)))
  
      try:




More information about the Python-checkins mailing list