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

index dbc941fd77ea709f234e3924aafb7a504aec995a..2ba1e8ee4145ee41a5b7457c1f9bc9cf523b0049 100644 (file)
@@ -5,6 +5,8 @@ from weakref import proxy
 import sys
 import operator
 import random
+import copy
+import pickle
 maxsize = test_support.MAX_Py_ssize_t
 minsize = -maxsize-1
 
@@ -215,6 +217,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, sys.maxint-5, sys.maxint+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_cycle(self):
         self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
         self.assertEqual(list(cycle('')), [])
index ca36adcdb7965a563a95bfff42cb0ccd0eac8f8a..8df76747e871c6c37e515093aa3c284e007b26df 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -33,6 +33,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #7410: deepcopy of itertools.count() erroneously reset the count.
+
 - Issue #7403: logging: Fixed possible race condition in lock creation.
 
 - Issue #7341: Close the internal file object in the TarFile constructor in
index dc8a92acfd48eec3205ef969db734af22d53ad7d..046548df2c74475927601c2b8d7f9382c19e90df 100644 (file)
@@ -2906,6 +2906,21 @@ count_repr(countobject *lz)
        return result;
 }
 
+static PyObject *
+count_reduce(countobject *lz)
+{
+       if (lz->cnt == PY_SSIZE_T_MAX)
+               return Py_BuildValue("O(O)", Py_TYPE(lz), lz->long_cnt);
+       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},
+};
+
 PyDoc_STRVAR(count_doc,
 "count([firstval]) --> count object\n\
 \n\
@@ -2941,7 +2956,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 */