From: Benjamin Peterson Date: Wed, 21 Mar 2012 03:48:11 +0000 (-0400) Subject: correctly lookup __trunc__ in int() constructor X-Git-Tag: v3.3.0a2~97^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1b1a8e7cb514d95546af9dcfd42b83cea27182fa;p=python correctly lookup __trunc__ in int() constructor --- diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 5759effca7..3051e57eef 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1770,6 +1770,7 @@ order (MRO) for bases """ ("__format__", format, format_impl, set(), {}), ("__floor__", math.floor, zero, set(), {}), ("__trunc__", math.trunc, zero, set(), {}), + ("__trunc__", int, zero, set(), {}), ("__ceil__", math.ceil, zero, set(), {}), ("__dir__", dir, empty_seq, set(), {}), ] diff --git a/Objects/abstract.c b/Objects/abstract.c index b8df9504d7..924ccd1ffe 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1350,7 +1350,7 @@ PyNumber_Long(PyObject *o) } if (PyLong_Check(o)) /* An int subclass without nb_int */ return _PyLong_Copy((PyLongObject *)o); - trunc_func = _PyObject_GetAttrId(o, &PyId___trunc__); + trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__); if (trunc_func) { PyObject *truncated = PyEval_CallObject(trunc_func, NULL); PyObject *int_instance; @@ -1362,7 +1362,8 @@ PyNumber_Long(PyObject *o) "__trunc__ returned non-Integral (type %.200s)"); return int_instance; } - PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */ + if (PyErr_Occurred()) + return NULL; if (PyBytes_Check(o)) /* need to do extra error checking that PyLong_FromString()