Skip to content

Commit 053c61f

Browse files
committed
Merged revisions 72495 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r72495 | benjamin.peterson | 2009-05-08 21:07:04 -0500 (Fri, 08 May 2009) | 1 line lookup __reversed__ correctly as a special method ........
1 parent be720e0 commit 053c61f

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

‎Lib/test/test_descr.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,12 +1549,15 @@ def iden(self):
15491549
return self
15501550
def hello(self):
15511551
return b"hello"
1552+
def empty_seq(self):
1553+
return []
15521554

15531555
# It would be nice to have every special method tested here, but I'm
15541556
# only listing the ones I can remember outside of typeobject.c, since it
15551557
# does it right.
15561558
specials = [
15571559
("__bytes__", bytes, hello),
1560+
("__reversed__", reversed, empty_seq),
15581561
# These two fail because the compiler generates LOAD_ATTR to look
15591562
# them up. We'd have to add a new opcode to fix this, and it's
15601563
# probably not worth it.

‎Objects/enumobject.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ static PyObject *
222222
reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
223223
{
224224
Py_ssize_t n;
225-
PyObject *seq;
225+
PyObject *seq, *reversed_meth;
226+
static PyObject *reversed_cache = NULL;
226227
reversedobject *ro;
227228

228229
if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
@@ -231,8 +232,12 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
231232
if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
232233
return NULL;
233234

234-
if (PyObject_HasAttrString(seq, "__reversed__"))
235-
return PyObject_CallMethod(seq, "__reversed__", NULL);
235+
reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache);
236+
if (reversed_meth != NULL) {
237+
PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
238+
Py_DECREF(reversed_meth);
239+
return res;
240+
}
236241

237242
if (!PySequence_Check(seq)) {
238243
PyErr_SetString(PyExc_TypeError,

0 commit comments

Comments
 (0)