Skip to content

Commit

Permalink
Merged revisions 72495 via svnmerge from
Browse files Browse the repository at this point in the history
svn+ssh://[email protected]/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
........
  • Loading branch information
benjaminp committed May 9, 2009
1 parent be720e0 commit 053c61f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,12 +1549,15 @@ def iden(self):
return self
def hello(self):
return b"hello"
def empty_seq(self):
return []

# It would be nice to have every special method tested here, but I'm
# only listing the ones I can remember outside of typeobject.c, since it
# does it right.
specials = [
("__bytes__", bytes, hello),
("__reversed__", reversed, empty_seq),
# These two fail because the compiler generates LOAD_ATTR to look
# them up. We'd have to add a new opcode to fix this, and it's
# probably not worth it.
Expand Down
11 changes: 8 additions & 3 deletions Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ static PyObject *
reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Py_ssize_t n;
PyObject *seq;
PyObject *seq, *reversed_meth;
static PyObject *reversed_cache = NULL;
reversedobject *ro;

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

if (PyObject_HasAttrString(seq, "__reversed__"))
return PyObject_CallMethod(seq, "__reversed__", NULL);
reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache);
if (reversed_meth != NULL) {
PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
Py_DECREF(reversed_meth);
return res;
}

if (!PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError,
Expand Down

0 comments on commit 053c61f

Please sign in to comment.