mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
*sigh* deal with instances correctly
This commit is contained in:
parent
0a32f9c448
commit
784d455066
2 changed files with 18 additions and 1 deletions
|
@ -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)))
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue