[Python-checkins] python/dist/src/Modules datetimemodule.c,1.57,1.58
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
2003年2月07日 14:50:30 -0800
Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv12260/python/Modules
Modified Files:
datetimemodule.c
Log Message:
Comparison for timedelta, time, date and datetime objects: __eq__ and
__ne__ no longer complain if they don't know how to compare to the other
thing. If no meaningful way to compare is known, saying "not equal" is
sensible. This allows things like
if adatetime in some_sequence:
and
somedict[adatetime] = whatever
to work as expected even if some_sequence contains non-datetime objects,
or somedict non-datetime keys, because they only call __eq__.
It still complains (raises TypeError) for mixed-type comparisons in
contexts that require a total ordering, such as list.sort(), use as a
key in a BTree-based data structure, and cmp().
Index: datetimemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/datetimemodule.c,v
retrieving revision 1.57
retrieving revision 1.58
diff -C2 -d -r1.57 -r1.58
*** datetimemodule.c 4 Feb 2003 20:45:17 -0000 1.57
--- datetimemodule.c 7 Feb 2003 22:50:28 -0000 1.58
***************
*** 1225,1228 ****
--- 1225,1238 ----
}
+ /* Raises a "can't compare" TypeError and returns NULL. */
+ static PyObject *
+ cmperror(PyObject *a, PyObject *b)
+ {
+ PyErr_Format(PyExc_TypeError,
+ "can't compare %s to %s",
+ a->ob_type->tp_name, b->ob_type->tp_name);
+ return NULL;
+ }
+
/* ---------------------------------------------------------------------------
* Basic object allocation. These allocate Python objects of the right
***************
*** 1655,1673 ****
delta_richcompare(PyDateTime_Delta *self, PyObject *other, int op)
{
! int diff;
! if (! PyDelta_CheckExact(other)) {
! PyErr_Format(PyExc_TypeError,
! "can't compare %s to %s instance",
! self->ob_type->tp_name, other->ob_type->tp_name);
! return NULL;
! }
! diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
! if (diff == 0) {
! diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
! if (diff == 0)
! diff = GET_TD_MICROSECONDS(self) -
! GET_TD_MICROSECONDS(other);
}
return diff_to_bool(diff, op);
}
--- 1665,1685 ----
delta_richcompare(PyDateTime_Delta *self, PyObject *other, int op)
{
! int diff = 42; /* nonsense */
! if (PyDelta_CheckExact(other)) {
! diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
! if (diff == 0) {
! diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
! if (diff == 0)
! diff = GET_TD_MICROSECONDS(self) -
! GET_TD_MICROSECONDS(other);
! }
}
+ else if (op == Py_EQ || op == Py_NE)
+ diff = 1; /* any non-zero value will do */
+
+ else /* stop this from falling back to address comparison */
+ return cmperror((PyObject *)self, other);
+
return diff_to_bool(diff, op);
}
***************
*** 2444,2464 ****
date_richcompare(PyDateTime_Date *self, PyObject *other, int op)
{
! int diff;
! if (! PyDate_Check(other)) {
! if (PyObject_HasAttrString(other, "timetuple")) {
! /* A hook for other kinds of date objects. */
! Py_INCREF(Py_NotImplemented);
! return Py_NotImplemented;
! }
! /* Stop this from falling back to address comparison. */
! PyErr_Format(PyExc_TypeError,
! "can't compare '%s' to '%s'",
! self->ob_type->tp_name,
! other->ob_type->tp_name);
! return NULL;
}
! diff = memcmp(self->data, ((PyDateTime_Date *)other)->data,
! _PyDateTime_DATE_DATASIZE);
return diff_to_bool(diff, op);
}
--- 2456,2476 ----
date_richcompare(PyDateTime_Date *self, PyObject *other, int op)
{
! int diff = 42; /* nonsense */
! if (PyDate_Check(other))
! diff = memcmp(self->data, ((PyDateTime_Date *)other)->data,
! _PyDateTime_DATE_DATASIZE);
!
! else if (PyObject_HasAttrString(other, "timetuple")) {
! /* A hook for other kinds of date objects. */
! Py_INCREF(Py_NotImplemented);
! return Py_NotImplemented;
}
! else if (op == Py_EQ || op == Py_NE)
! diff = 1; /* any non-zero value will do */
!
! else /* stop this from falling back to address comparison */
! return cmperror((PyObject *)self, other);
!
return diff_to_bool(diff, op);
}
***************
*** 3174,3183 ****
if (! PyTime_Check(other)) {
/* Stop this from falling back to address comparison. */
! PyErr_Format(PyExc_TypeError,
! "can't compare '%s' to '%s'",
! self->ob_type->tp_name,
! other->ob_type->tp_name);
! return NULL;
}
if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1, Py_None,
--- 3186,3196 ----
if (! PyTime_Check(other)) {
+ if (op == Py_EQ || op == Py_NE) {
+ PyObject *result = op == Py_EQ ? Py_False : Py_True;
+ Py_INCREF(result);
+ return result;
+ }
/* Stop this from falling back to address comparison. */
! return cmperror((PyObject *)self, other);
}
if (classify_two_utcoffsets((PyObject *)self, &offset1, &n1, Py_None,
***************
*** 4012,4021 ****
return Py_NotImplemented;
}
/* Stop this from falling back to address comparison. */
! PyErr_Format(PyExc_TypeError,
! "can't compare '%s' to '%s'",
! self->ob_type->tp_name,
! other->ob_type->tp_name);
! return NULL;
}
--- 4025,4035 ----
return Py_NotImplemented;
}
+ if (op == Py_EQ || op == Py_NE) {
+ PyObject *result = op == Py_EQ ? Py_False : Py_True;
+ Py_INCREF(result);
+ return result;
+ }
/* Stop this from falling back to address comparison. */
! return cmperror((PyObject *)self, other);
}