summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2002-04-26 02:49:14 (GMT)
committerGuido van Rossum <guido@python.org>2002-04-26 02:49:14 (GMT)
commit517c7d4fd366a03ab033e615b9a37a199c98d8a3 (patch)
tree36fa672b35d300b76f2e0217fe83f0c9c74dd5f2
parentd451ec1cdb7bd01786c492a485f3813502640daf (diff)
downloadcpython-517c7d4fd366a03ab033e615b9a37a199c98d8a3.zip
cpython-517c7d4fd366a03ab033e615b9a37a199c98d8a3.tar.gz
cpython-517c7d4fd366a03ab033e615b9a37a199c98d8a3.tar.bz2
PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
left and right type were of the same type and not classic instances. This shortcut is dangerous for proxy types, because it means that coerce(Proxy(1), Proxy(2.1)) leaves Proxy(1) unchanged rather than turning it into Proxy(1.0). In an ever-so-slight change of semantics, I now only take the shortcut when the left and right types are of the same type and don't have the CHECKTYPES feature. It so happens that classic instances have this flag, so the shortcut is still skipped in this case (i.e. nothing changes for classic instances). Proxies also have this flag set (otherwise implementing numeric operations on proxies would become nightmarish) and this means that the shortcut is also skipped there, as desired. It so happens that int, long and float also have this flag set; that means that e.g. coerce(1, 1) will now invoke int_coerce(). This is fine: int_coerce() can deal with this, and I'm not worried about the performance; int_coerce() is only invoked when the user explicitly calls coerce(), which should be rarer than rare.
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/object.c5
2 files changed, 8 insertions, 1 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 9363694..c63db58 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -163,6 +163,10 @@ Build
C API
+- PyNumber_Coerce() and PyNumber_CoerceEx() now also invoke the type's
+ coercion if both arguments have the same type but this type has the
+ CHECKTYPES flag set. This is to better support proxies.
+
- The type of tp_free has been changed from "void (*)(PyObject *)" to
"void (*)(void *)".
diff --git a/Objects/object.c b/Objects/object.c
index e47f80e..85fd35f 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1427,7 +1427,10 @@ PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
register PyObject *w = *pw;
int res;
- if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
+ /* Shortcut only for old-style types */
+ if (v->ob_type == w->ob_type &&
+ !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES))
+ {
Py_INCREF(v);
Py_INCREF(w);
return 0;