[Python-checkins] cpython: rewrite this function, which was still accounting for classic classes

benjamin.peterson python-checkins at python.org
Wed Mar 21 19:51:49 CET 2012


http://hg.python.org/cpython/rev/006e4a3a4b29
changeset:   75856:006e4a3a4b29
user:        Benjamin Peterson <benjamin at python.org>
date:        Wed Mar 21 14:38:11 2012 -0400
summary:
  rewrite this function, which was still accounting for classic classes

files:
  Objects/abstract.c |  36 +++++++++++----------------------
  1 files changed, 12 insertions(+), 24 deletions(-)


diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1271,34 +1271,22 @@
 PyObject *
 _PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
 {
-    static PyObject *int_name = NULL;
-    if (int_name == NULL) {
-        int_name = PyUnicode_InternFromString("__int__");
-        if (int_name == NULL)
+    PyNumberMethods *nb;
+    if (PyLong_Check(integral))
+        return integral;
+    nb = Py_TYPE(integral)->tp_as_number;
+    if (nb->nb_int) {
+        PyObject *as_int = nb->nb_int(integral);
+        Py_DECREF(integral);
+        if (!as_int)
             return NULL;
+        if (PyLong_Check(as_int))
+            return as_int;
+        Py_DECREF(as_int);
     }
-
-    if (integral && !PyLong_Check(integral)) {
-        /* Don't go through tp_as_number->nb_int to avoid
-           hitting the classic class fallback to __trunc__. */
-        PyObject *int_func = PyObject_GetAttr(integral, int_name);
-        if (int_func == NULL) {
-            PyErr_Clear(); /* Raise a different error. */
-            goto non_integral_error;
-        }
-        Py_DECREF(integral);
-        integral = PyEval_CallObject(int_func, NULL);
-        Py_DECREF(int_func);
-        if (integral && !PyLong_Check(integral)) {
-            goto non_integral_error;
-        }
-    }
-    return integral;
-
-non_integral_error:
     PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
     Py_DECREF(integral);
-    return NULL;
+    return NULL;    
 }
 
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list