[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.80,1.81 doc.txt,1.25,1.26 obj_date.c,1.38,1.39 obj_datetime.c,1.33,1.34 test_both.py,1.50,1.51

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sat, 07 Dec 2002 11:51:21 -0800


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

Modified Files:
	datetime.py doc.txt obj_date.c obj_datetime.c test_both.py 
Log Message:
The behavior of these objects in Boolean contexts wasn't documented or
tested, and differed between the Python and C implementations.  Repaired
all that.  I'm not sure it's of real value that timdelta(0) and time(0)
are considered false, and am especially dubious of the latter.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.80
retrieving revision 1.81
diff -C2 -d -r1.80 -r1.81
*** datetime.py	7 Dec 2002 19:17:34 -0000	1.80
--- datetime.py	7 Dec 2002 19:51:18 -0000	1.81
***************
*** 464,467 ****
--- 464,472 ----
          return hash((self.__days, self.__seconds, self.__microseconds))
  
+     def __nonzero__(self):
+         return (self.__days != 0 or
+                 self.__seconds != 0 or
+                 self.__microseconds != 0)
+ 
      def __getstate__(self):
          return (self.__days, self.__seconds, self.__microseconds)
***************
*** 827,830 ****
--- 832,841 ----
                                      self.__second, 0, 0, -1))
  
+ 
+     def __nonzero__(self):
+         return (self.__hour != 0 or
+                 self.__minute != 0 or
+                 self.__second != 0 or
+                 self.__microsecond != 0)
  
      # Pickle support.

Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -d -r1.25 -r1.26
*** doc.txt	7 Dec 2002 18:58:25 -0000	1.25
--- doc.txt	7 Dec 2002 19:51:18 -0000	1.26
***************
*** 148,156 ****
        t.days < 0.  This is exact, and cannot overflow.
  
!     - comparison of timedelta to timedelta
  
      - hash, use as dict key
  
!     - pickling
  
  
--- 148,160 ----
        t.days < 0.  This is exact, and cannot overflow.
  
!     - comparison of timedelta to timedelta; the timedelta representing
!       the smaller duration is considered to be the smaller timedelta
  
      - hash, use as dict key
  
!     - efficient pickling
! 
!     - in Boolean contexts, a timedelta object is consdired to be true
!       if and only if it isn't equal to timedelta(0)
  
  
***************
*** 247,251 ****
      - hash, use as dict key
  
!     - pickling
  
  Instance methods:
--- 251,257 ----
      - hash, use as dict key
  
!     - efficient pickling
! 
!     - in Boolean contexts, all date objects are considered to be true
  
  Instance methods:
***************
*** 440,444 ****
      - hash, use as dict key
  
!     - pickling
  
  Instance methods:
--- 446,452 ----
      - hash, use as dict key
  
!     - efficient pickling
! 
!     - in Boolean contexts, all datetime objects are considered to be true
  
  Instance methods:
***************
*** 550,554 ****
      - hash, use as dict key
  
!     - pickling
  
  Instance methods:
--- 558,565 ----
      - hash, use as dict key
  
!     - efficient pickling
! 
!     - in Boolean contexts, a time object is consdired to be true
!       if and only if it isn't equal to time(0)
  
  Instance methods:

Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -C2 -d -r1.38 -r1.39
*** obj_date.c	7 Dec 2002 19:03:14 -0000	1.38
--- obj_date.c	7 Dec 2002 19:51:18 -0000	1.39
***************
*** 400,410 ****
  }
  
- static int
- date_nonzero(PyDateTime_Date *self)
- {
- 	assert(GET_YEAR(self) >= 1);
- 	return 1;
- }
- 
  static PyObject *
  date_toordinal(PyDateTime_Date *self)
--- 400,403 ----
***************
*** 562,566 ****
  	0,						/* nb_positive */
  	0,						/* nb_absolute */
! 	(inquiry)date_nonzero,				/* nb_nonzero */
  };
  
--- 555,559 ----
  	0,						/* nb_positive */
  	0,						/* nb_absolute */
! 	0,						/* nb_nonzero */
  };
  

Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -d -r1.33 -r1.34
*** obj_datetime.c	7 Dec 2002 19:03:15 -0000	1.33
--- obj_datetime.c	7 Dec 2002 19:51:18 -0000	1.34
***************
*** 628,632 ****
  	0,					/* nb_positive */
  	0,					/* nb_absolute */
! 	(inquiry)date_nonzero,			/* nb_nonzero */
  };
  
--- 628,632 ----
  	0,					/* nb_positive */
  	0,					/* nb_absolute */
! 	0,					/* nb_nonzero */
  };
  

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.50
retrieving revision 1.51
diff -C2 -d -r1.50 -r1.51
*** test_both.py	7 Dec 2002 19:17:34 -0000	1.50
--- test_both.py	7 Dec 2002 19:51:18 -0000	1.51
***************
*** 328,331 ****
--- 328,338 ----
                           (-1, 24*3600-1, 999999))
  
+     def test_bool(self):
+         self.failUnless(timedelta(1))
+         self.failUnless(timedelta(0, 1))
+         self.failUnless(timedelta(0, 0, 1))
+         self.failUnless(timedelta(microseconds=1))
+         self.failUnless(not timedelta(0))
+ 
  #############################################################################
  # date tests
***************
*** 778,781 ****
--- 785,793 ----
              self.assertRaises(TypeError, lambda: badarg >= t1)
  
+     def test_bool(self):
+         # All dates are considered true.
+         self.failUnless(self.theclass.min)
+         self.failUnless(self.theclass.max)
+ 
  #############################################################################
  # datetime tests
***************
*** 1296,1299 ****
--- 1308,1320 ----
                  derived = pickler.loads(green)
                  self.assertEqual(orig, derived)
+ 
+     def test_bool(self):
+         cls = self.theclass
+         self.failUnless(cls(1))
+         self.failUnless(cls(0, 1))
+         self.failUnless(cls(0, 0, 1))
+         self.failUnless(cls(0, 0, 0, 1))
+         self.failUnless(not cls(0))
+         self.failUnless(not cls())
  
  def test_suite():