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

Skip to content

Commit 74b38b1

Browse files
committed
Issue #16148: Small improvements and cleanup. Added version information
to docs.
1 parent 96e9367 commit 74b38b1

6 files changed

Lines changed: 24 additions & 9 deletions

File tree

Doc/c-api/object.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ is considered sufficient for this determination.
349349
returning the default value. On error ``-1`` is returned. This is the
350350
equivalent to the Python expression ``operator.length_hint(o, default)``.
351351
352+
.. versionadded:: 3.4
353+
352354
.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
353355
354356
Return element of *o* corresponding to the object *key* or *NULL* on failure.

Doc/library/operator.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ their character equivalents.
241241
actual length, then an estimate using ``__length_hint__``, and finally
242242
returning the default value.
243243

244+
.. versionadded:: 3.4
245+
244246
The :mod:`operator` module also defines tools for generalized attribute and item
245247
lookups. These are useful for making fast field extractors as arguments for
246248
:func:`map`, :func:`sorted`, :meth:`itertools.groupby`, or other functions that

Doc/reference/datamodel.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,15 @@ through the container; for mappings, :meth:`__iter__` should be the same as
18051805
considered to be false in a Boolean context.
18061806

18071807

1808+
.. method:: object.__length_hint__(self)
1809+
1810+
Called to implement ``operator.length_hint``. Should return an estimated
1811+
length for the object (which may be greater or less than the actual length).
1812+
The length must be an integer ``>=`` 0. This method is purely an
1813+
optimization and is never required for correctness.
1814+
1815+
.. versionadded:: 3.4
1816+
18081817
.. note::
18091818

18101819
Slicing is done exclusively with the following three methods. A call like ::

Include/abstract.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
403403
PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
404404
#define PyObject_Length PyObject_Size
405405

406-
PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
406+
#ifndef Py_LIMITED_API
407+
PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
408+
#endif
407409
PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
408410

409411
/*

Lib/test/test_enumerate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ def test_range_optimization(self):
170170
self.assertEqual(type(reversed(x)), type(iter(x)))
171171

172172
def test_len(self):
173-
# This is an implementation detail, not an interface requirement
174173
for s in ('hello', tuple('hello'), list('hello'), range(5)):
175174
self.assertEqual(operator.length_hint(reversed(s)), len(s))
176175
r = reversed(s)

Objects/abstract.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ _PyObject_HasLen(PyObject *o) {
7171
}
7272

7373
/* The length hint function returns a non-negative value from o.__len__()
74-
or o.__length_hint__(). If those methods aren't found. If one of the calls
75-
fails this function returns -1.
74+
or o.__length_hint__(). If those methods aren't found the defaultvalue is
75+
returned. If one of the calls fails with an exception other than TypeError
76+
this function returns -1.
7677
*/
7778

7879
Py_ssize_t
@@ -112,21 +113,21 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
112113
return defaultvalue;
113114
}
114115
if (!PyLong_Check(result)) {
115-
PyErr_Format(PyExc_TypeError, "Length hint must be an integer, not %s",
116+
PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s",
116117
Py_TYPE(result)->tp_name);
117118
Py_DECREF(result);
118119
return -1;
119120
}
120-
defaultvalue = PyLong_AsSsize_t(result);
121+
res = PyLong_AsSsize_t(result);
121122
Py_DECREF(result);
122-
if (defaultvalue < 0 && PyErr_Occurred()) {
123+
if (res < 0 && PyErr_Occurred()) {
123124
return -1;
124125
}
125-
if (defaultvalue < 0) {
126+
if (res < 0) {
126127
PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0");
127128
return -1;
128129
}
129-
return defaultvalue;
130+
return res;
130131
}
131132

132133
PyObject *

0 commit comments

Comments
 (0)