]> granicus.if.org Git - python/commitdiff
Issue #7410: deepcopy of itertools.count was resetting the count.
authorRaymond Hettinger <python@rcn.com>
Mon, 30 Nov 2009 21:55:17 +0000 (21:55 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 30 Nov 2009 21:55:17 +0000 (21:55 +0000)
Lib/test/test_itertools.py
Misc/NEWS
Modules/itertoolsmodule.c

index 2f449da0bbb6ed7b3e1f8d419d63e53c889e2e3b..16e65cab65eb337da079b15766e4f7a552c57cb1 100644 (file)
@@ -7,6 +7,8 @@ from fractions import Fraction
 import sys
 import operator
 import random
+import copy
+import pickle
 from functools import reduce
 maxsize = support.MAX_Py_ssize_t
 minsize = -maxsize-1
@@ -352,6 +354,13 @@ class TestBasicOps(unittest.TestCase):
             r2 = 'count(%r)'.__mod__(i).replace('L', '')
             self.assertEqual(r1, r2)
 
+        # check copy, deepcopy, pickle
+        for value in -3, 3, maxsize-5, maxsize+5:
+            c = count(value)
+            self.assertEqual(next(copy.copy(c)), value)
+            self.assertEqual(next(copy.deepcopy(c)), value)
+            self.assertEqual(next(pickle.loads(pickle.dumps(c))), value)
+
     def test_count_with_stride(self):
         self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
         self.assertEqual(lzip('abc',count(start=2,step=3)),
index 6d7c4e367b62141f06b3eb40efcc5e8ccf6cde50..ddd10d7786aafb33f8feca212a65359ce0fdff5e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -52,6 +52,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #7410: deepcopy of itertools.count was resetting the count.
+
 - Issue #4486: When an exception has an explicit cause, do not print its
   implicit context too.  This affects the `traceback` module as well as
   built-in exception printing.
index 3264d1f7626deddf35e668b15884a0db9168bfd1..74396b68b866ce0340a0d63f9794cdc673a497e8 100644 (file)
@@ -3049,6 +3049,22 @@ count_repr(countobject *lz)
                                                                lz->long_cnt, lz->long_step);
 }
 
+static PyObject *
+count_reduce(countobject *lz)
+{
+       if (lz->cnt == PY_SSIZE_T_MAX)
+               return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->long_cnt, lz->long_step);
+       return Py_BuildValue("O(n)", Py_TYPE(lz), lz->cnt);
+}
+
+PyDoc_STRVAR(count_reduce_doc, "Return state information for pickling.");
+
+static PyMethodDef count_methods[] = {
+       {"__reduce__",  (PyCFunction)count_reduce,      METH_NOARGS,
+        count_reduce_doc},
+       {NULL,          NULL}   /* sentinel */
+};
+
 PyDoc_STRVAR(count_doc,
                         "count(start=0, step=1]) --> count object\n\
 \n\
@@ -3090,7 +3106,7 @@ static PyTypeObject count_type = {
        0,                              /* tp_weaklistoffset */
        PyObject_SelfIter,              /* tp_iter */
        (iternextfunc)count_next,       /* tp_iternext */
-       0,                              /* tp_methods */
+       count_methods,                          /* tp_methods */
        0,                              /* tp_members */
        0,                              /* tp_getset */
        0,                              /* tp_base */