]> granicus.if.org Git - python/commitdiff
Issue #19369: Optimized the usage of __length_hint__().
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 24 Oct 2013 20:19:51 +0000 (23:19 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 24 Oct 2013 20:19:51 +0000 (23:19 +0300)
Misc/NEWS
Objects/abstract.c

index 26e418b2a8d6e50cc2ba1ca0a92694453d22d791..69cb09457c0b24f58e71dd8315e059d583eae255 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Projected release date: 2013-11-24
 Core and Builtins
 -----------------
 
+- Issue #19369: Optimized the usage of __length_hint__().
+
 - Issue #18603: Ensure that PyOS_mystricmp and PyOS_mystrnicmp are in the
   Python executable and not removed by the linker's optimizer.
 
index d937892781628acf23bfbda39398d37b133bf19f..6c7a6cd2269ef098bf8b01402b1e8a180df9e04c 100644 (file)
@@ -82,15 +82,17 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
     PyObject *hint, *result;
     Py_ssize_t res;
     _Py_IDENTIFIER(__length_hint__);
-    res = PyObject_Length(o);
-    if (res < 0 && PyErr_Occurred()) {
-        if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
-            return -1;
+    if (_PyObject_HasLen(o)) {
+        res = PyObject_Length(o);
+        if (res < 0 && PyErr_Occurred()) {
+            if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
+                return -1;
+            }
+            PyErr_Clear();
+        }
+        else {
+            return res;
         }
-        PyErr_Clear();
-    }
-    else {
-        return res;
     }
     hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
     if (hint == NULL) {