mirror of
https://github.com/python/cpython.git
synced 2025-07-30 22:54:16 +00:00
Make int() and long() fall back to __trunc__(). See issue 2002.
This commit is contained in:
parent
72a6576279
commit
a26cf9b760
5 changed files with 232 additions and 3 deletions
|
@ -1798,7 +1798,29 @@ instance_index(PyInstanceObject *self)
|
|||
|
||||
|
||||
UNARY(instance_invert, "__invert__")
|
||||
UNARY(instance_int, "__int__")
|
||||
UNARY(_instance_trunc, "__trunc__")
|
||||
|
||||
static PyObject *
|
||||
instance_int(PyInstanceObject *self)
|
||||
{
|
||||
PyObject *truncated;
|
||||
static PyObject *int_name;
|
||||
if (int_name == NULL) {
|
||||
int_name = PyString_InternFromString("__int__");
|
||||
if (int_name == NULL)
|
||||
return NULL;
|
||||
}
|
||||
if (PyObject_HasAttr((PyObject*)self, int_name))
|
||||
return generic_unary_op(self, int_name);
|
||||
|
||||
truncated = _instance_trunc(self);
|
||||
/* __trunc__ is specified to return an Integral type, but
|
||||
int() needs to return an int. */
|
||||
return _PyNumber_ConvertIntegralToInt(
|
||||
truncated,
|
||||
"__trunc__ returned non-Integral (type %.200s)");
|
||||
}
|
||||
|
||||
UNARY_FB(instance_long, "__long__", instance_int)
|
||||
UNARY(instance_float, "__float__")
|
||||
UNARY(instance_oct, "__oct__")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue