summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-09-26 10:37:12 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-09-26 10:37:12 (GMT)
commitcb61e5d9b59c1a5f18dd34edebb054d1e19f1581 (patch)
treea52fa36e6297747487027f5d2344696a112df566 /Objects
parent3eac591a5c33f4bd0bdd60a76053cd3b6b6e5918 (diff)
downloadcpython-cb61e5d9b59c1a5f18dd34edebb054d1e19f1581.zip
cpython-cb61e5d9b59c1a5f18dd34edebb054d1e19f1581.tar.gz
cpython-cb61e5d9b59c1a5f18dd34edebb054d1e19f1581.tar.bz2
Issue #9869: Make long() and PyNumber_Long return something of type
long for a class whose __long__ method returns a plain int. This fixes an interpreter crash (due to long_subtype_new assuming PyNumber_Long returns a long) when initializing an instance of a long subclass from an object whose __long__ method returns a plain int.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/abstract.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 75de1aa..1e79ddf 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1713,7 +1713,14 @@ PyNumber_Long(PyObject *o)
if (m && m->nb_long) { /* This should include subclasses of long */
/* Classic classes always take this branch. */
PyObject *res = m->nb_long(o);
- if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
+ if (res == NULL)
+ return NULL;
+ if (PyInt_Check(res)) {
+ long value = PyInt_AS_LONG(res);
+ Py_DECREF(res);
+ return PyLong_FromLong(value);
+ }
+ else if (!PyLong_Check(res)) {
PyErr_Format(PyExc_TypeError,
"__long__ returned non-long (type %.200s)",
res->ob_type->tp_name);