mirror of
https://github.com/python/cpython.git
synced 2025-12-09 18:48:05 +00:00
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.
This commit is contained in:
parent
3eac591a5c
commit
cb61e5d9b5
4 changed files with 30 additions and 2 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue