summaryrefslogtreecommitdiffstats
path: root/Objects/object.c
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2004-12-23 22:13:13 (GMT)
committerArmin Rigo <arigo@tunes.org>2004-12-23 22:13:13 (GMT)
commita1748131132ebc83f443b2a8ddc5073bfefa504f (patch)
tree174f8f8bfecdb210261d30a889fb5102f2f86b06 /Objects/object.c
parent0a6864ecc5d0188c5dcaf2b5e7fb725668b13749 (diff)
downloadcpython-a1748131132ebc83f443b2a8ddc5073bfefa504f.zip
cpython-a1748131132ebc83f443b2a8ddc5073bfefa504f.tar.gz
cpython-a1748131132ebc83f443b2a8ddc5073bfefa504f.tar.bz2
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
a minor change after the coercion, to accept two objects not necessarily of the same type but with the same tp_compare.
Diffstat (limited to 'Objects/object.c')
-rw-r--r--Objects/object.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 3f70009..d86d74f 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -606,33 +606,28 @@ try_3way_compare(PyObject *v, PyObject *w)
w->ob_type->tp_compare == _PyObject_SlotCompare)
return _PyObject_SlotCompare(v, w);
- /* Try coercion; if it fails, give up */
+ /* If we're here, v and w,
+ a) are not instances;
+ b) have different types or a type without tp_compare; and
+ c) don't have a user-defined tp_compare.
+ tp_compare implementations in C assume that both arguments
+ have their type, so we give up if the coercion fails or if
+ it yields types which are still incompatible (which can
+ happen with a user-defined nb_coerce).
+ */
c = PyNumber_CoerceEx(&v, &w);
if (c < 0)
return -2;
if (c > 0)
return 2;
-
- /* Try v's comparison, if defined */
- if ((f = v->ob_type->tp_compare) != NULL) {
+ f = v->ob_type->tp_compare;
+ if (f != NULL && f == w->ob_type->tp_compare) {
c = (*f)(v, w);
Py_DECREF(v);
Py_DECREF(w);
return adjust_tp_compare(c);
}
- /* Try w's comparison, if defined */
- if ((f = w->ob_type->tp_compare) != NULL) {
- c = (*f)(w, v); /* swapped! */
- Py_DECREF(v);
- Py_DECREF(w);
- c = adjust_tp_compare(c);
- if (c >= -1)
- return -c; /* Swapped! */
- else
- return c;
- }
-
/* No comparison defined */
Py_DECREF(v);
Py_DECREF(w);