Make int() and long() fall back to __trunc__(). See issue 2002.

This commit is contained in:
Jeffrey Yasskin 2008-02-04 01:04:35 +00:00
parent 72a6576279
commit a26cf9b760
5 changed files with 232 additions and 3 deletions

View file

@ -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__")