[Python-checkins] python/nondist/sandbox/datetime obj_datetime.c,1.8,1.9 test_both.py,1.21,1.22

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2002年12月02日 13:55:26 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv4482
Modified Files:
	obj_datetime.c test_both.py 
Log Message:
Repaired comparison for C datetime objects. All the tests currently
pass. datetime objects could stand to have another comparison test
(they're only tested now to the extent that the inherited TestDate
tests exercise the year, month, and day fields).
Index: obj_datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_datetime.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** obj_datetime.c	2 Dec 2002 19:40:42 -0000	1.8
--- obj_datetime.c	2 Dec 2002 21:55:21 -0000	1.9
***************
*** 126,139 ****
 }
 
! static int
! datetime_compare(PyDateTime_DateTime *self, PyObject *other)
 {
 	if (!PyType_IsSubtype(other->ob_type, &PyDateTime_DateTimeType)) {
 		PyErr_SetString(PyExc_TypeError,
 				"can't compare date to %s instance");
! 		return -1;
 	}
! 	return memcmp(self->data, ((PyDateTime_DateTime *)other)->data,
 		 _PyDateTime_DATETIME_DATA_SIZE);
 }
 
--- 126,146 ----
 }
 
! /* This is more natural as a tp_compare, but doesn't work then: for whatever
! * reason, Python's try_3way_compare ignores tp_compare unless
! * PyInstance_Check returns true, but these aren't old-style classes.
! */
! static PyObject *
! datetime_richcompare(PyDateTime_DateTime *self, PyObject *other, int op)
 {
+ 	long diff;
+ 
 	if (!PyType_IsSubtype(other->ob_type, &PyDateTime_DateTimeType)) {
 		PyErr_SetString(PyExc_TypeError,
 				"can't compare date to %s instance");
! 		return NULL;
 	}
! 	diff = memcmp(self->data, ((PyDateTime_DateTime *)other)->data,
 		 _PyDateTime_DATETIME_DATA_SIZE);
+ 	return diff_to_bool(diff, op);
 }
 
***************
*** 466,470 ****
 	0,					/* tp_getattr */
 	0,					/* tp_setattr */
! 	(cmpfunc)datetime_compare,		/* tp_compare */
 	(reprfunc)datetime_repr,		/* tp_repr */
 	&datetime_as_number,			/* tp_as_number */
--- 473,477 ----
 	0,					/* tp_getattr */
 	0,					/* tp_setattr */
! 	0,					/* tp_compare */
 	(reprfunc)datetime_repr,		/* tp_repr */
 	&datetime_as_number,			/* tp_as_number */
***************
*** 482,486 ****
 	0,					/* tp_traverse */
 	0,					/* tp_clear */
! 	0,					/* tp_richcompare */
 	0,					/* tp_weaklistoffset */
 	0,					/* tp_iter */
--- 489,493 ----
 	0,					/* tp_traverse */
 	0,					/* tp_clear */
! 	(richcmpfunc)datetime_richcompare,	/* tp_richcompare */
 	0,					/* tp_weaklistoffset */
 	0,					/* tp_iter */
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** test_both.py	2 Dec 2002 21:51:00 -0000	1.21
--- test_both.py	2 Dec 2002 21:55:21 -0000	1.22
***************
*** 458,464 ****
 self.assertEqual(orig, derived)
 
- # XXX This currently fails in the C implementation, for datetime (but not
- # XXX date) objects, due to never raising TypeError. Needs to be fixed
- # XXX as timedelta and date got fixed.
 def test_compare(self):
 t1 = self.theclass(2, 3, 4)
--- 458,461 ----

AltStyle によって変換されたページ (->オリジナル) /