diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-10-24 20:19:51 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-10-24 20:19:51 (GMT) |
commit | f740d467bf4c4552cf437d41838a75fb2dc168d8 (patch) | |
tree | 11c32ad95cc6db7b8958a7cc74ac9f86f18eb2b7 /Objects/abstract.c | |
parent | 8b150ecfc9a57fb2d564381464bb04c9a94ee053 (diff) | |
download | cpython-f740d467bf4c4552cf437d41838a75fb2dc168d8.zip cpython-f740d467bf4c4552cf437d41838a75fb2dc168d8.tar.gz cpython-f740d467bf4c4552cf437d41838a75fb2dc168d8.tar.bz2 |
Issue #19369: Optimized the usage of __length_hint__().
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index d937892..6c7a6cd 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) { |