diff options
author | Raymond Hettinger <python@rcn.com> | 2005-09-24 21:23:05 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2005-09-24 21:23:05 (GMT) |
commit | 6b27cda64386195cd07dfb686e9486f1c4bc3159 (patch) | |
tree | 277a5c96e9683c63b573b508f8c4bd2d1374aed8 /Objects/abstract.c | |
parent | 9ceebd544516908e67c0f0d92c7a5f484e12beeb (diff) | |
download | cpython-6b27cda64386195cd07dfb686e9486f1c4bc3159.zip cpython-6b27cda64386195cd07dfb686e9486f1c4bc3159.tar.gz cpython-6b27cda64386195cd07dfb686e9486f1c4bc3159.tar.bz2 |
Convert iterator __len__() methods to a private API.
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 94af3da..1f8feb5 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -81,6 +81,31 @@ PyObject_Length(PyObject *o) } #define PyObject_Length PyObject_Size +int +_PyObject_LengthCue(PyObject *o) +{ + int rv = PyObject_Size(o); + if (rv != -1) + return rv; + if (PyErr_ExceptionMatches(PyExc_TypeError) || + PyErr_ExceptionMatches(PyExc_AttributeError)) { + PyObject *err_type, *err_value, *err_tb, *ro; + + PyErr_Fetch(&err_type, &err_value, &err_tb); + ro = PyObject_CallMethod(o, "_length_cue", NULL); + if (ro != NULL) { + rv = (int)PyInt_AsLong(ro); + Py_DECREF(ro); + Py_XDECREF(err_type); + Py_XDECREF(err_value); + Py_XDECREF(err_tb); + return rv; + } + PyErr_Restore(err_type, err_value, err_tb); + } + return -1; +} + PyObject * PyObject_GetItem(PyObject *o, PyObject *key) { @@ -1399,7 +1424,7 @@ PySequence_Tuple(PyObject *v) return NULL; /* Guess result size and allocate space. */ - n = PyObject_Size(v); + n = _PyObject_LengthCue(v); if (n < 0) { if (!PyErr_ExceptionMatches(PyExc_TypeError) && !PyErr_ExceptionMatches(PyExc_AttributeError)) { |