diff options
author | Tim Peters <tim.peters@gmail.com> | 2003-01-24 22:36:34 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2003-01-24 22:36:34 (GMT) |
commit | 8d81a012efa7fde5e43b8ea7275c7fc995cf74fa (patch) | |
tree | 4fad231a65954388e45221d5ca5a3013897e7efa /Modules/datetimemodule.c | |
parent | cd63e619b4703ed5701589d367cccb7357d67aa8 (diff) | |
download | cpython-8d81a012efa7fde5e43b8ea7275c7fc995cf74fa.zip cpython-8d81a012efa7fde5e43b8ea7275c7fc995cf74fa.tar.gz cpython-8d81a012efa7fde5e43b8ea7275c7fc995cf74fa.tar.bz2 |
date and datetime comparison: when we don't know how to
compare against "the other" argument, we raise TypeError,
in order to prevent comparison from falling back to the
default (and worse than useless, in this case) comparison
by object address.
That's fine so far as it goes, but leaves no way for
another date/datetime object to make itself comparable
to our objects. For example, it leaves Marc-Andre no way
to teach mxDateTime dates how to compare against Python
dates.
Discussion on Python-Dev raised a number of impractical
ideas, and the simple one implemented here: when we don't
know how to compare against "the other" argument, we raise
TypeError *unless* the other object has a timetuple attr.
In that case, we return NotImplemented instead, and Python
will give the other object a shot at handling the
comparison then.
Note that comparisons of time and timedelta objects still
suffer the original problem, though.
Diffstat (limited to 'Modules/datetimemodule.c')
-rw-r--r-- | Modules/datetimemodule.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 6f0cf1e..31e006d 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -2437,8 +2437,15 @@ 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 date to %s instance", + "can't compare '%s' to '%s'", + self->ob_type->tp_name, other->ob_type->tp_name); return NULL; } @@ -4018,6 +4025,11 @@ datetime_richcompare(PyDateTime_DateTime *self, PyObject *other, int op) int offset1, offset2; if (! PyDateTime_Check(other)) { + if (PyObject_HasAttrString(other, "timetuple")) { + /* A hook for other kinds of datetime 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'", |