mirror of https://github.com/python/cpython
Let reversed() work with itself.
This commit is contained in:
parent
8a1a594590
commit
06353f76be
|
@ -22,6 +22,7 @@ extern "C" {
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_VAR_HEAD
|
PyObject_VAR_HEAD
|
||||||
PyObject **ob_item;
|
PyObject **ob_item;
|
||||||
|
int allocated;
|
||||||
} PyListObject;
|
} PyListObject;
|
||||||
|
|
||||||
PyAPI_DATA(PyTypeObject) PyList_Type;
|
PyAPI_DATA(PyTypeObject) PyList_Type;
|
||||||
|
|
|
@ -141,6 +141,10 @@ class TestReversed(unittest.TestCase):
|
||||||
x = xrange(1)
|
x = xrange(1)
|
||||||
self.assertEqual(type(reversed(x)), type(iter(x)))
|
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):
|
def test_main(verbose=None):
|
||||||
testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig,
|
testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig,
|
||||||
TestReversed)
|
TestReversed)
|
||||||
|
|
|
@ -230,11 +230,22 @@ reversed_next(reversedobject *ro)
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
reversed_reverse(reversedobject *ro, PyObject *unused)
|
||||||
|
{
|
||||||
|
return PyObject_GetIter(ro->seq);
|
||||||
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(reversed_doc,
|
PyDoc_STRVAR(reversed_doc,
|
||||||
"reverse(sequence) -> reverse iterator over values of the sequence\n"
|
"reverse(sequence) -> reverse iterator over values of the sequence\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Return a reverse iterator");
|
"Return a reverse iterator");
|
||||||
|
|
||||||
|
static PyMethodDef reversed_methods[] = {
|
||||||
|
{"__reversed__", (PyCFunction)reversed_reverse,
|
||||||
|
METH_NOARGS, reversed_doc},
|
||||||
|
};
|
||||||
|
|
||||||
PyTypeObject PyReversed_Type = {
|
PyTypeObject PyReversed_Type = {
|
||||||
PyObject_HEAD_INIT(&PyType_Type)
|
PyObject_HEAD_INIT(&PyType_Type)
|
||||||
0, /* ob_size */
|
0, /* ob_size */
|
||||||
|
@ -266,7 +277,7 @@ PyTypeObject PyReversed_Type = {
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
PyObject_SelfIter, /* tp_iter */
|
PyObject_SelfIter, /* tp_iter */
|
||||||
(iternextfunc)reversed_next, /* tp_iternext */
|
(iternextfunc)reversed_next, /* tp_iternext */
|
||||||
0, /* tp_methods */
|
reversed_methods, /* tp_methods */
|
||||||
0, /* tp_members */
|
0, /* tp_members */
|
||||||
0, /* tp_getset */
|
0, /* tp_getset */
|
||||||
0, /* tp_base */
|
0, /* tp_base */
|
||||||
|
|
Loading…
Reference in New Issue