mirror of
https://github.com/python/cpython.git
synced 2025-12-15 21:44:50 +00:00
Fix SF bug #683467, 'int' ability to generate longs not inherited
When subclassing from an int but not overriding __new__, long values were not converted properly. Try to convert longs into an int.
This commit is contained in:
parent
9caf9c040e
commit
de8b94c3e1
3 changed files with 27 additions and 2 deletions
|
|
@ -836,16 +836,30 @@ static PyObject *
|
|||
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
PyObject *tmp, *new;
|
||||
long ival;
|
||||
|
||||
assert(PyType_IsSubtype(type, &PyInt_Type));
|
||||
tmp = int_new(&PyInt_Type, args, kwds);
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
assert(PyInt_Check(tmp));
|
||||
if (!PyInt_Check(tmp)) {
|
||||
if (!PyLong_Check(tmp)) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"value must convertable to an int");
|
||||
return NULL;
|
||||
}
|
||||
ival = PyLong_AsLong(tmp);
|
||||
if (ival == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
|
||||
} else {
|
||||
ival = ((PyIntObject *)tmp)->ob_ival;
|
||||
}
|
||||
|
||||
new = type->tp_alloc(type, 0);
|
||||
if (new == NULL)
|
||||
return NULL;
|
||||
((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
|
||||
((PyIntObject *)new)->ob_ival = ival;
|
||||
Py_DECREF(tmp);
|
||||
return new;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue