Skip to content

Commit 4ef9c22

Browse files
author
armin.rigo
committed
Renamed _length_cue() to __length_hint__(). See:
http://mail.python.org/pipermail/python-dev/2006-February/060524.html git-svn-id: http://svn.python.org/projects/python/trunk@42326 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 9bc38bc commit 4ef9c22

File tree

14 files changed

+41
-32
lines changed

14 files changed

+41
-32
lines changed

Include/abstract.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,25 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
422422
PyAPI_FUNC(int) PyObject_Length(PyObject *o);
423423
#define PyObject_Length PyObject_Size
424424

425-
PyAPI_FUNC(int) _PyObject_LengthCue(PyObject *o);
425+
PyAPI_FUNC(int) _PyObject_LengthHint(PyObject *o);
426426

427427
/*
428428
Return the size of object o. If the object, o, provides
429429
both sequence and mapping protocols, the sequence size is
430430
returned. On error, -1 is returned. If the object provides
431-
a _length_cue() method, its value is returned. This is the
431+
a __length_hint__() method, its value is returned. This is an
432+
internal undocumented API provided for performance reasons;
433+
for compatibility, don't use it outside the core. This is the
432434
equivalent to the Python expression:
433435
try:
434436
return len(o)
435437
except (AttributeError, TypeError):
436-
if hasattr(o, '_length_cue'):
437-
return o._length_cue()
438-
raise
438+
exc_type, exc_value, exc_tb = sys.exc_info()
439+
try:
440+
return o.__length_hint__()
441+
except:
442+
pass
443+
raise exc_type, exc_value, exc_tb
439444
*/
440445

441446
PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);

Lib/test/test_iterlen.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def len(obj):
5555
return _len(obj)
5656
except TypeError:
5757
try:
58-
return obj._length_cue()
58+
# note: this is an internal undocumented API,
59+
# don't rely on it in your own programs
60+
return obj.__length_hint__()
5961
except AttributeError:
6062
raise TypeError
6163

Lib/test/test_set.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,9 @@ def test_iteration(self):
607607
for v in self.set:
608608
self.assert_(v in self.values)
609609
setiter = iter(self.set)
610-
self.assertEqual(setiter._length_cue(), len(self.set))
610+
# note: __length_hint__ is an internal undocumented API,
611+
# don't rely on it in your own programs
612+
self.assertEqual(setiter.__length_hint__(), len(self.set))
611613

612614
def test_pickling(self):
613615
p = pickle.dumps(self.set)

Modules/collectionsmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,10 @@ dequeiter_len(dequeiterobject *it)
941941
return PyInt_FromLong(it->counter);
942942
}
943943

944-
PyDoc_STRVAR(length_cue_doc, "Private method returning an estimate of len(list(it)).");
944+
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
945945

946946
static PyMethodDef dequeiter_methods[] = {
947-
{"_length_cue", (PyCFunction)dequeiter_len, METH_NOARGS, length_cue_doc},
947+
{"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
948948
{NULL, NULL} /* sentinel */
949949
};
950950

Modules/itertoolsmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,10 +2346,10 @@ repeat_len(repeatobject *ro)
23462346
return PyInt_FromLong(ro->cnt);
23472347
}
23482348

2349-
PyDoc_STRVAR(length_cue_doc, "Private method returning an estimate of len(list(it)).");
2349+
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
23502350

23512351
static PyMethodDef repeat_methods[] = {
2352-
{"_length_cue", (PyCFunction)repeat_len, METH_NOARGS, length_cue_doc},
2352+
{"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc},
23532353
{NULL, NULL} /* sentinel */
23542354
};
23552355

Objects/abstract.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ PyObject_Length(PyObject *o)
8282
#define PyObject_Length PyObject_Size
8383

8484
int
85-
_PyObject_LengthCue(PyObject *o)
85+
_PyObject_LengthHint(PyObject *o)
8686
{
8787
int rv = PyObject_Size(o);
8888
if (rv != -1)
@@ -92,7 +92,7 @@ _PyObject_LengthCue(PyObject *o)
9292
PyObject *err_type, *err_value, *err_tb, *ro;
9393

9494
PyErr_Fetch(&err_type, &err_value, &err_tb);
95-
ro = PyObject_CallMethod(o, "_length_cue", NULL);
95+
ro = PyObject_CallMethod(o, "__length_hint__", NULL);
9696
if (ro != NULL) {
9797
rv = (int)PyInt_AsLong(ro);
9898
Py_DECREF(ro);
@@ -1463,7 +1463,7 @@ PySequence_Tuple(PyObject *v)
14631463
return NULL;
14641464

14651465
/* Guess result size and allocate space. */
1466-
n = _PyObject_LengthCue(v);
1466+
n = _PyObject_LengthHint(v);
14671467
if (n < 0) {
14681468
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
14691469
!PyErr_ExceptionMatches(PyExc_AttributeError)) {

Objects/dictobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,10 +2063,10 @@ dictiter_len(dictiterobject *di)
20632063
return PyInt_FromLong(len);
20642064
}
20652065

2066-
PyDoc_STRVAR(length_cue_doc, "Private method returning an estimate of len(list(it)).");
2066+
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
20672067

20682068
static PyMethodDef dictiter_methods[] = {
2069-
{"_length_cue", (PyCFunction)dictiter_len, METH_NOARGS, length_cue_doc},
2069+
{"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
20702070
{NULL, NULL} /* sentinel */
20712071
};
20722072

Objects/enumobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ reversed_len(reversedobject *ro)
252252
return PyInt_FromLong((seqsize < position) ? 0 : position);
253253
}
254254

255-
PyDoc_STRVAR(length_cue_doc, "Private method returning an estimate of len(list(it)).");
255+
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
256256

257257
static PyMethodDef reversediter_methods[] = {
258-
{"_length_cue", (PyCFunction)reversed_len, METH_NOARGS, length_cue_doc},
258+
{"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
259259
{NULL, NULL} /* sentinel */
260260
};
261261

Objects/iterobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ iter_len(seqiterobject *it)
8787
return PyInt_FromLong(0);
8888
}
8989

90-
PyDoc_STRVAR(length_cue_doc, "Private method returning an estimate of len(list(it)).");
90+
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
9191

9292
static PyMethodDef seqiter_methods[] = {
93-
{"_length_cue", (PyCFunction)iter_len, METH_NOARGS, length_cue_doc},
93+
{"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
9494
{NULL, NULL} /* sentinel */
9595
};
9696

Objects/listobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ listextend(PyListObject *self, PyObject *b)
775775
iternext = *it->ob_type->tp_iternext;
776776

777777
/* Guess a result list size. */
778-
n = _PyObject_LengthCue(b);
778+
n = _PyObject_LengthHint(b);
779779
if (n < 0) {
780780
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
781781
!PyErr_ExceptionMatches(PyExc_AttributeError)) {
@@ -2776,10 +2776,10 @@ listiter_len(listiterobject *it)
27762776
return PyInt_FromLong(0);
27772777
}
27782778

2779-
PyDoc_STRVAR(length_cue_doc, "Private method returning an estimate of len(list(it)).");
2779+
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
27802780

27812781
static PyMethodDef listiter_methods[] = {
2782-
{"_length_cue", (PyCFunction)listiter_len, METH_NOARGS, length_cue_doc},
2782+
{"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
27832783
{NULL, NULL} /* sentinel */
27842784
};
27852785

0 commit comments

Comments
 (0)