mirror of https://github.com/python/cpython
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
|
@ -458,12 +458,20 @@ def ints():
|
||||||
class C(int):
|
class C(int):
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
vereq(C(5L), 5)
|
||||||
try:
|
try:
|
||||||
C() + ""
|
C() + ""
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed, "NotImplemented should have caused TypeError"
|
raise TestFailed, "NotImplemented should have caused TypeError"
|
||||||
|
import sys
|
||||||
|
try:
|
||||||
|
C(sys.maxint+1)
|
||||||
|
except OverflowError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise TestFailed, "should have raised OverflowError"
|
||||||
|
|
||||||
def longs():
|
def longs():
|
||||||
if verbose: print "Testing long operations..."
|
if verbose: print "Testing long operations..."
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.3 alpha 2?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- int subclasses can be initialized with longs if the value fits in an int.
|
||||||
|
See SF bug #683467.
|
||||||
|
|
||||||
- long(string, base) takes time linear in len(string) when base is a power
|
- long(string, base) takes time linear in len(string) when base is a power
|
||||||
of 2 now. It used to take time quadratic in len(string).
|
of 2 now. It used to take time quadratic in len(string).
|
||||||
|
|
||||||
|
|
|
@ -836,16 +836,30 @@ static PyObject *
|
||||||
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
PyObject *tmp, *new;
|
PyObject *tmp, *new;
|
||||||
|
long ival;
|
||||||
|
|
||||||
assert(PyType_IsSubtype(type, &PyInt_Type));
|
assert(PyType_IsSubtype(type, &PyInt_Type));
|
||||||
tmp = int_new(&PyInt_Type, args, kwds);
|
tmp = int_new(&PyInt_Type, args, kwds);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
return 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);
|
new = type->tp_alloc(type, 0);
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
|
((PyIntObject *)new)->ob_ival = ival;
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue