summaryrefslogtreecommitdiff
path: root/Objects/object.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-11-19 16:03:17 +0000
committerGuido van Rossum <guido@python.org>1997-11-19 16:03:17 +0000
commitca5d06e7922c626d952a471acffb7e2c1e488499 (patch)
tree02d29960f5098879eb3ec3aff3cefd9419ead042 /Objects/object.c
parent5cd643e29bc6c5319a7c196dad9f3376995c5f56 (diff)
downloadcpython-ca5d06e7922c626d952a471acffb7e2c1e488499.tar.gz
Add a new function PyNumber_CoerceEx() which works just like
PyNumber_Coerce() except that when the coercion can't be done and no other exceptions happen, it returns 1 instead of raising an exception. Use this function in PyObject_Compare() to avoid raising an exception simply because two objects with numeric behavior can't be coerced to a common type; instead, proceed with the non-numeric default comparison. Note that this is a somewhat questionable practice -- comparisons for numeric objects shouldn't default to random behavior like this, but it is required for backward compatibility. (Case in point, it broke comparison of kjDict objects to integers in Aaron Watters' kjbuckets extension.) A correct fix (for python 2.0) should involve a different definiton of comparison altogether.
Diffstat (limited to 'Objects/object.c')
-rw-r--r--Objects/object.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 6adc2be113..46487de851 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -300,9 +300,11 @@ PyObject_Compare(v, w)
if ((tp = v->ob_type) != w->ob_type) {
if (tp->tp_as_number != NULL &&
w->ob_type->tp_as_number != NULL) {
- if (PyNumber_Coerce(&v, &w) != 0)
+ int err;
+ err = PyNumber_CoerceEx(&v, &w);
+ if (err < 0)
return -1;
- else {
+ else if (err == 0) {
int cmp = (*v->ob_type->tp_compare)(v, w);
Py_DECREF(v);
Py_DECREF(w);
@@ -472,7 +474,7 @@ PyObject_IsTrue(v)
*/
int
-PyNumber_Coerce(pv, pw)
+PyNumber_CoerceEx(pv, pw)
PyObject **pv, **pw;
{
register PyObject *v = *pv;
@@ -494,6 +496,16 @@ PyNumber_Coerce(pv, pw)
if (res <= 0)
return res;
}
+ return 1;
+}
+
+int
+PyNumber_Coerce(pv, pw)
+ PyObject **pv, **pw;
+{
+ int err = PyNumber_CoerceEx(pv, pw);
+ if (err <= 0)
+ return err;
PyErr_SetString(PyExc_TypeError, "number coercion failed");
return -1;
}