[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.29,1.30 obj_delta.c,1.8,1.9 test_both.py,1.11,1.12

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sun, 01 Dec 2002 15:49:59 -0800


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

Modified Files:
	datetime.c obj_delta.c test_both.py 
Log Message:
Purged some now-unused code and fixed some comments.

PROBLEM:  Noted that timedelta comparison doesn't work as intended.  I
assume the same is true for all comparisons defined here.  I don't know
whether this is a bug in Python (current CVS), or a bug here, so don't
know how to fix it.  For example, cmp(timedelta(whatever), 42) ends up
comparing the type names of the objects, despite that timedelta's
tp_compare function is trying its darnedest to raise TypeError then.
Turns out the tp_compare function isn't called unless both objects are
timedeltas.


Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** datetime.c	1 Dec 2002 19:37:28 -0000	1.29
--- datetime.c	1 Dec 2002 23:49:56 -0000	1.30
***************
*** 374,404 ****
  }
  
- /* Fiddle days (d), hours (h), and minutes (m) so that
-  * 	0 <= *h < 24
-  * 	0 <= *m < 60
-  * The input values must be such that the internals don't overflow.
-  * The way this routine is used, we don't get close.
-  */
- static void
- normalize_d_h_m(long *d, long *h, long *m)
- {
- 	if (*m < 0 || *m >= 60) {
- 		normalize_pair(h, m, 60);
- 		/* |h| can't be bigger than about
- 		 * |original h| + |original m|/60 now.
- 		 */
- 
- 	}
- 	if (*h < 0 || *h >= 24) {
- 		normalize_pair(d, h, 24);
- 		/* |d| can't be bigger than about
- 		 * |original d| +
- 		 * (|original h| + |original m|/60) / 24 now.
- 		 */
- 	}
- 	assert(0 <= *h && *h < 24);
- 	assert(0 <= *m && *m < 60);
- }
- 
  /* Fiddle days (d), seconds (s), and microseconds (us) so that
   * 	0 <= *s < 24*3600
--- 374,377 ----
***************
*** 426,489 ****
  	assert(0 <= *s && *s < 24*3600);
  	assert(0 <= *us && *us < 1000000);
- }
- 
- /* Fiddle days (d), seconds (s), and microseconds (us) so that the output
-  * duration is the same as the input duration, but with hours (h),
-  * minutes (m), and milliseconds (ms) all 0.
-  * The input values must be such that the internals don't overflow.  The
-  * way this routine is used, we don't get close.
-  * The output d, s, and us are intended to be passed to normalize_d_s_us
-  * to get them into their proper ranges.
-  */
- static void
- squash_h_m_ms_out_of_d_h_m_s_ms_us(long *d, long h, long m,
- 				   long *s, long ms, long *us)
- {
- 	if (h) {
- 		long new_minutes;
- 
-        		normalize_pair(d, &h, 24);
- 		/* |d| can't be bigger than about
- 		 * |original d| + |original h|/24 now.
- 		 * h can't bigger than 23.
- 		 */
- 		new_minutes = m + h*60;
- 		assert(! SIGNED_ADD_OVERFLOWED(new_minutes, m, h*60));
- 		m = new_minutes;
- 		/* |m| can't be bigger than about
- 		 * |original m| + 23*60 now.
- 		 */
- 	}
- 	if (m) {
- 		long new_seconds;
- 
- 		normalize_pair(d, &m, 24*60);
- 		/* |d| can't be bigger than about
- 		 * |original d| + |original h|/24 +
- 		 * (|original m| + 23*60)/(24*60) now.
- 		 * m can't bigger than 24*60-1.
- 		 */
- 		new_seconds = *s + m*60;
- 		assert(! SIGNED_ADD_OVERFLOWED(new_seconds, *s, m*60));
- 		*s = new_seconds;
- 		/* |s| can't be bigger than about
- 		 * |original s| + (24*60-1) * 60 now.
- 		 */
- 	}
- 	if (ms) {
- 		long new_us;
- 
- 		normalize_pair(s, &ms, 1000);
- 		/* |s| can't be bigger than about
- 		 * |original s| + (24*60-1) * 60 + |original ms|/1000 now.
- 		 * ms can't be bigger than 999.
- 		 */
- 		new_us = *us + ms * 1000;
- 		assert(! SIGNED_ADD_OVERFLOWED(new_us, *us, ms*1000));
- 		*us = new_us;
- 		/* |us| can't be bigger than about
- 		 * |original us| + 999 * 1000.
- 		 */
- 	}
  }
  
--- 399,402 ----

Index: obj_delta.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_delta.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** obj_delta.c	1 Dec 2002 19:37:28 -0000	1.8
--- obj_delta.c	1 Dec 2002 23:49:56 -0000	1.9
***************
*** 169,177 ****
  delta_add(PyObject *left, PyObject *right)
  {
! 	PyTypeObject *left_type = left->ob_type;
! 	PyTypeObject *right_type = right->ob_type;
  
! 	if (PyType_IsSubtype(left_type, &PyDateTime_DeltaType) &&
! 	    PyType_IsSubtype(right_type, &PyDateTime_DeltaType)) {
  		/* delta + delta */
  		/* The C-level additions can't overflow because of the
--- 169,176 ----
  delta_add(PyObject *left, PyObject *right)
  {
! 	PyObject *result = Py_NotImplemented;
  
! 	if (PyType_IsSubtype(left->ob_type, &PyDateTime_DeltaType) &&
! 	    PyType_IsSubtype(right->ob_type, &PyDateTime_DeltaType)) {
  		/* delta + delta */
  		/* The C-level additions can't overflow because of the
***************
*** 182,189 ****
  		long microseconds = GET_TD_MICROSECONDS(left) +
  				    GET_TD_MICROSECONDS(right);
! 		return new_delta(days, seconds, microseconds);
  	}
! 	Py_INCREF(Py_NotImplemented);
! 	return Py_NotImplemented;
  }
  
--- 181,190 ----
  		long microseconds = GET_TD_MICROSECONDS(left) +
  				    GET_TD_MICROSECONDS(right);
! 		result = new_delta(days, seconds, microseconds);
  	}
! 
! 	if (result == Py_NotImplemented)
! 		Py_INCREF(result);
! 	return result;
  }
  
***************
*** 226,232 ****
  delta_subtract(PyObject *left, PyObject *right)
  {
! 	PyObject *result = NULL;
  
- 	/* XXX It's unclear to me exactly which rules we intend here. */
  	if (PyType_IsSubtype(left->ob_type, &PyDateTime_DeltaType) &&
  	    PyType_IsSubtype(right->ob_type, &PyDateTime_DeltaType)) {
--- 227,232 ----
  delta_subtract(PyObject *left, PyObject *right)
  {
! 	PyObject *result = Py_NotImplemented;
  
  	if (PyType_IsSubtype(left->ob_type, &PyDateTime_DeltaType) &&
  	    PyType_IsSubtype(right->ob_type, &PyDateTime_DeltaType)) {
***************
*** 237,248 ****
  	    		Py_DECREF(minus_right);
  	    	}
  	}
- 	else
- 		result = Py_NotImplemented;
  
! 	Py_XINCREF(result);
  	return result;
  }
  
  static int
  delta_compare(PyDateTime_Delta *self, PyObject *other)
--- 237,254 ----
  	    		Py_DECREF(minus_right);
  	    	}
+ 	    	else
+ 	    		result = NULL;
  	}
  
! 	if (result == Py_NotImplemented)
! 		Py_INCREF(result);
  	return result;
  }
  
+ /* XXX This routine is never entered unless self and other are both
+  * XXX PyDateTime_Delta.  For whatever reason, Python's try_3way_compare
+  * XXX ignores tp_compare unless PyInstance_Check returns true, but
+  * XXX these aren't old-style classes.
+  */
  static int
  delta_compare(PyDateTime_Delta *self, PyObject *other)
***************
*** 250,257 ****
  	int result = -1;
  
! 	if (!PyObject_TypeCheck(other, &PyDateTime_DeltaType))
  		PyErr_Format(PyExc_TypeError,
  			     "can't compare %s to %s instance",
  			     self->ob_type->tp_name, other->ob_type->tp_name);
  	else {
  		long diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
--- 256,265 ----
  	int result = -1;
  
! 	if (! PyObject_TypeCheck(other, &PyDateTime_DeltaType)) {
! 		/* XXX Dead code!  See note above. */
  		PyErr_Format(PyExc_TypeError,
  			     "can't compare %s to %s instance",
  			     self->ob_type->tp_name, other->ob_type->tp_name);
+ 	}
  	else {
  		long diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
***************
*** 325,329 ****
  
  /* Fold in the value of the tag ("seconds", "weeks", etc) component of a
!  * a timedelta constructor.  sofar is the # of microseconds accounted for
   * so far, and there are factor microseconds per current unit, the number
   * of which is given by num.  num * factor is added to sofar in a
--- 333,337 ----
  
  /* Fold in the value of the tag ("seconds", "weeks", etc) component of a
!  * timedelta constructor.  sofar is the # of microseconds accounted for
   * so far, and there are factor microseconds per current unit, the number
   * of which is given by num.  num * factor is added to sofar in a
***************
*** 332,336 ****
   * added into *leftover.
   * If num is NULL, no computation is done, and sofar is returned (after
!  * incremented its refcount).
   * Note that there are many ways this can give an error (NULL) return.
   */
--- 340,344 ----
   * added into *leftover.
   * If num is NULL, no computation is done, and sofar is returned (after
!  * incrementing its refcount).
   * Note that there are many ways this can give an error (NULL) return.
   */
***************
*** 502,505 ****
--- 510,514 ----
  
  	if (leftover_us) {
+ 		/* Round to nearest whole # of us, and add into x. */
  		PyObject *temp;
  		if (leftover_us >= 0.0)

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** test_both.py	1 Dec 2002 19:37:28 -0000	1.11
--- test_both.py	1 Dec 2002 23:49:56 -0000	1.12
***************
*** 112,120 ****
      def test_hash_equality(self):
          t1 = timedelta(days=100,
!                    weeks=-7,
!                    hours=-24*(100-49),
!                    minutes=-3,
!                    seconds=12,
!                    microseconds=(3*60 - 12) * 1000000)
          t2 = timedelta()
          self.assertEqual(hash(t1), hash(t2))
--- 112,120 ----
      def test_hash_equality(self):
          t1 = timedelta(days=100,
!                        weeks=-7,
!                        hours=-24*(100-49),
!                        minutes=-3,
!                        seconds=12,
!                        microseconds=(3*60 - 12) * 1000000)
          t2 = timedelta()
          self.assertEqual(hash(t1), hash(t2))