mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
Minor improvements to itertools.tee():
* tee object is no longer subclassable * independent iterators renamed to "itertools.tee_iterator" * fixed doc string typo and added entry in the module doc string
This commit is contained in:
parent
397b45d4ba
commit
f0c5aec85f
2 changed files with 21 additions and 9 deletions
|
@ -243,6 +243,18 @@ class TestBasicOps(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, tee, 3)
|
self.assertRaises(TypeError, tee, 3)
|
||||||
self.assertRaises(TypeError, tee, [1,2], 'x')
|
self.assertRaises(TypeError, tee, [1,2], 'x')
|
||||||
|
|
||||||
|
try:
|
||||||
|
class A(tee): pass
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.fail("tee constructor should not be subclassable")
|
||||||
|
|
||||||
|
# tee_iterator should not be instantiable
|
||||||
|
a, b = tee(xrange(10))
|
||||||
|
self.assertRaises(TypeError, type(a))
|
||||||
|
self.assert_(a is iter(a)) # tee_iterator should support __iter__
|
||||||
|
|
||||||
def test_StopIteration(self):
|
def test_StopIteration(self):
|
||||||
self.assertRaises(StopIteration, izip().next)
|
self.assertRaises(StopIteration, izip().next)
|
||||||
|
|
||||||
|
|
|
@ -113,12 +113,12 @@ ii_traverse(iiobject *ii, visitproc visit, void *arg)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(ii_doc, "Independent iterators linked to a tee() object.");
|
PyDoc_STRVAR(ii_doc, "Independent iterator created by tee(iterable).");
|
||||||
|
|
||||||
static PyTypeObject ii_type = {
|
static PyTypeObject ii_type = {
|
||||||
PyObject_HEAD_INIT(&PyType_Type)
|
PyObject_HEAD_INIT(&PyType_Type)
|
||||||
0, /* ob_size */
|
0, /* ob_size */
|
||||||
"itertools.independent_iterator", /* tp_name */
|
"itertools.tee_iterator", /* tp_name */
|
||||||
sizeof(iiobject), /* tp_basicsize */
|
sizeof(iiobject), /* tp_basicsize */
|
||||||
0, /* tp_itemsize */
|
0, /* tp_itemsize */
|
||||||
/* methods */
|
/* methods */
|
||||||
|
@ -173,7 +173,7 @@ tee_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
outbasket = PyList_New(0);
|
outbasket = PyList_New(0);
|
||||||
if (outbasket == NULL) goto fail;
|
if (outbasket == NULL) goto fail;
|
||||||
|
|
||||||
to = (teeobject *)type->tp_alloc(type, 0);
|
to = PyObject_GC_New(teeobject, &tee_type);
|
||||||
if (to == NULL) goto fail;
|
if (to == NULL) goto fail;
|
||||||
|
|
||||||
to->it = it;
|
to->it = it;
|
||||||
|
@ -181,6 +181,7 @@ tee_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
to->outbasket = outbasket;
|
to->outbasket = outbasket;
|
||||||
to->save_mode = 1;
|
to->save_mode = 1;
|
||||||
to->num_seen = 0;
|
to->num_seen = 0;
|
||||||
|
PyObject_GC_Track(to);
|
||||||
|
|
||||||
/* create independent iterators */
|
/* create independent iterators */
|
||||||
result = PyTuple_New(2);
|
result = PyTuple_New(2);
|
||||||
|
@ -212,7 +213,7 @@ tee_dealloc(teeobject *to)
|
||||||
Py_XDECREF(to->inbasket);
|
Py_XDECREF(to->inbasket);
|
||||||
Py_XDECREF(to->outbasket);
|
Py_XDECREF(to->outbasket);
|
||||||
Py_XDECREF(to->it);
|
Py_XDECREF(to->it);
|
||||||
to->ob_type->tp_free(to);
|
PyObject_GC_Del(to);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -241,7 +242,7 @@ tee_traverse(teeobject *to, visitproc visit, void *arg)
|
||||||
PyDoc_STRVAR(tee_doc,
|
PyDoc_STRVAR(tee_doc,
|
||||||
"tee(iterable) --> (it1, it2)\n\
|
"tee(iterable) --> (it1, it2)\n\
|
||||||
\n\
|
\n\
|
||||||
Split the iterable into to independent iterables.");
|
Split the iterable into two independent iterables.");
|
||||||
|
|
||||||
static PyTypeObject tee_type = {
|
static PyTypeObject tee_type = {
|
||||||
PyObject_HEAD_INIT(NULL)
|
PyObject_HEAD_INIT(NULL)
|
||||||
|
@ -262,11 +263,10 @@ static PyTypeObject tee_type = {
|
||||||
0, /* tp_hash */
|
0, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
0, /* tp_str */
|
0, /* tp_str */
|
||||||
PyObject_GenericGetAttr, /* tp_getattro */
|
0, /* tp_getattro */
|
||||||
0, /* tp_setattro */
|
0, /* tp_setattro */
|
||||||
0, /* tp_as_buffer */
|
0, /* tp_as_buffer */
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
||||||
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
||||||
tee_doc, /* tp_doc */
|
tee_doc, /* tp_doc */
|
||||||
(traverseproc)tee_traverse, /* tp_traverse */
|
(traverseproc)tee_traverse, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
|
@ -285,7 +285,6 @@ static PyTypeObject tee_type = {
|
||||||
0, /* tp_init */
|
0, /* tp_init */
|
||||||
0, /* tp_alloc */
|
0, /* tp_alloc */
|
||||||
tee_new, /* tp_new */
|
tee_new, /* tp_new */
|
||||||
PyObject_GC_Del, /* tp_free */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* cycle object **********************************************************/
|
/* cycle object **********************************************************/
|
||||||
|
@ -2092,6 +2091,7 @@ islice(seq, [start,] stop [, step]) --> elements from\n\
|
||||||
seq[start:stop:step]\n\
|
seq[start:stop:step]\n\
|
||||||
imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\
|
imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\
|
||||||
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
|
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
|
||||||
|
tee(it) --> (it1, it2) splits one iterator into two \n\
|
||||||
chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\
|
chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\
|
||||||
takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\
|
takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\
|
||||||
dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\
|
dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue