]> granicus.if.org Git - python/commitdiff
correctly lookup __trunc__ in int() constructor
authorBenjamin Peterson <benjamin@python.org>
Wed, 21 Mar 2012 03:48:11 +0000 (23:48 -0400)
committerBenjamin Peterson <benjamin@python.org>
Wed, 21 Mar 2012 03:48:11 +0000 (23:48 -0400)
Lib/test/test_descr.py
Objects/abstract.c

index 5759effca719bd35ed6790f24dabe1f8c3288321..3051e57eef70212e6183d611ed0a3dc98a271241 100644 (file)
@@ -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(), {}),
             ]
index b8df9504d72d2448e85f82f2f5ad1c119e7ef16e..924ccd1ffe86df1b03b4449d359ba318c436f796 100644 (file)
@@ -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()