Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f740d46

Browse files
Issue #19369: Optimized the usage of __length_hint__().
1 parent 8b150ec commit f740d46

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Projected release date: 2013-11-24
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #19369: Optimized the usage of __length_hint__().
14+
1315
- Issue #18603: Ensure that PyOS_mystricmp and PyOS_mystrnicmp are in the
1416
Python executable and not removed by the linker's optimizer.
1517

Objects/abstract.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
8282
PyObject *hint, *result;
8383
Py_ssize_t res;
8484
_Py_IDENTIFIER(__length_hint__);
85-
res = PyObject_Length(o);
86-
if (res < 0 && PyErr_Occurred()) {
87-
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
88-
return -1;
85+
if (_PyObject_HasLen(o)) {
86+
res = PyObject_Length(o);
87+
if (res < 0 && PyErr_Occurred()) {
88+
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
89+
return -1;
90+
}
91+
PyErr_Clear();
92+
}
93+
else {
94+
return res;
8995
}
90-
PyErr_Clear();
91-
}
92-
else {
93-
return res;
9496
}
9597
hint = _PyObject_LookupSpecial(o, &PyId___length_hint__);
9698
if (hint == NULL) {

0 commit comments

Comments
 (0)