From 784d45506670ce86d857ee118f0f64a2b667d386 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 9 May 2009 17:23:03 +0000 Subject: *sigh* deal with instances correctly --- Lib/test/test_enumerate.py | 6 ++++++ Objects/enumobject.c | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_enumerate.py b/Lib/test/test_enumerate.py index 074179c..d8a3251 100644 --- a/Lib/test/test_enumerate.py +++ b/Lib/test/test_enumerate.py @@ -141,6 +141,12 @@ class TestReversed(unittest.TestCase): # don't allow keyword arguments self.assertRaises(TypeError, reversed, [], a=1) + def test_class_class(self): + class A: + def __reversed__(self): + return [2, 1] + self.assertEqual(list(reversed(A())), [2, 1]) + def test_xrange_optimization(self): x = xrange(1) self.assertEqual(type(reversed(x)), type(iter(x))) diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 12a226c..65d4633 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -232,7 +232,18 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) ) return NULL; - reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache); + if (PyInstance_Check(seq)) { + reversed_meth = PyObject_GetAttrString(seq, "__reversed__"); + if (reversed_meth == NULL) { + if (PyErr_ExceptionMatches(PyExc_AttributeError)) + PyErr_Clear(); + else + return NULL; + } + } + else + reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", + &reversed_cache); if (reversed_meth != NULL) { PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL); Py_DECREF(reversed_meth); -- cgit v0.12