[Python-checkins] cpython: Issue #19369: Optimized the usage of __length_hint__().

serhiy.storchaka python-checkins at python.org
Thu Oct 24 22:20:19 CEST 2013


http://hg.python.org/cpython/rev/bffb49efc383
changeset:   86602:bffb49efc383
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Thu Oct 24 23:19:51 2013 +0300
summary:
  Issue #19369: Optimized the usage of __length_hint__().

files:
  Misc/NEWS          |   2 ++
  Objects/abstract.c |  18 ++++++++++--------
  2 files changed, 12 insertions(+), 8 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 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.
 
diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -82,15 +82,17 @@
     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();
         }
-        PyErr_Clear();
-    }
-    else {
-        return res;
+        else {
+            return res;
+        }
     }
     hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
     if (hint == NULL) {

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


More information about the Python-checkins mailing list