summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-02-08 10:49:42 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-02-08 10:49:42 (GMT)
commit06353f76beab69a538d3a55ca9b6df7e9754b999 (patch)
tree3d0caeac4f7b271d32c35333a6b4f47baedbfa46
parent8a1a5945908c7a0b2b487a98431ff46ab934014a (diff)
downloadcpython-06353f76beab69a538d3a55ca9b6df7e9754b999.zip
cpython-06353f76beab69a538d3a55ca9b6df7e9754b999.tar.gz
cpython-06353f76beab69a538d3a55ca9b6df7e9754b999.tar.bz2
Let reversed() work with itself.
-rw-r--r--Include/listobject.h1
-rw-r--r--Lib/test/test_enumerate.py4
-rw-r--r--Objects/enumobject.c13
3 files changed, 17 insertions, 1 deletions
diff --git a/Include/listobject.h b/Include/listobject.h
index 14ed72e..6221b80 100644
--- a/Include/listobject.h
+++ b/Include/listobject.h
@@ -22,6 +22,7 @@ extern "C" {
typedef struct {
PyObject_VAR_HEAD
PyObject **ob_item;
+ int allocated;
} PyListObject;
PyAPI_DATA(PyTypeObject) PyList_Type;
diff --git a/Lib/test/test_enumerate.py b/Lib/test/test_enumerate.py
index 636f883..ab1a60d 100644
--- a/Lib/test/test_enumerate.py
+++ b/Lib/test/test_enumerate.py
@@ -141,6 +141,10 @@ class TestReversed(unittest.TestCase):
x = xrange(1)
self.assertEqual(type(reversed(x)), type(iter(x)))
+ def test_double_reverse(self):
+ s = 'hello'
+ self.assertEqual(list(reversed(reversed(s))), list(s))
+
def test_main(verbose=None):
testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig,
TestReversed)
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index 7ed58da..3aac680 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -230,11 +230,22 @@ reversed_next(reversedobject *ro)
return item;
}
+static PyObject *
+reversed_reverse(reversedobject *ro, PyObject *unused)
+{
+ return PyObject_GetIter(ro->seq);
+}
+
PyDoc_STRVAR(reversed_doc,
"reverse(sequence) -> reverse iterator over values of the sequence\n"
"\n"
"Return a reverse iterator");
+static PyMethodDef reversed_methods[] = {
+ {"__reversed__", (PyCFunction)reversed_reverse,
+ METH_NOARGS, reversed_doc},
+};
+
PyTypeObject PyReversed_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
@@ -266,7 +277,7 @@ PyTypeObject PyReversed_Type = {
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)reversed_next, /* tp_iternext */
- 0, /* tp_methods */
+ reversed_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */