From f740d467bf4c4552cf437d41838a75fb2dc168d8 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 24 Oct 2013 23:19:51 +0300 Subject: [PATCH] Issue #19369: Optimized the usage of __length_hint__(). --- Misc/NEWS | 2 ++ Objects/abstract.c | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 26e418b2a8d..69cb09457c0 100644 --- 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. diff --git a/Objects/abstract.c b/Objects/abstract.c index d9378927816..6c7a6cd2269 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -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) {