[Python-checkins] r76600 - in python/trunk: Lib/test/test_itertools.py Modules/itertoolsmodule.c

raymond.hettinger python-checkins at python.org
Mon Nov 30 20:44:40 CET 2009


Author: raymond.hettinger
Date: Mon Nov 30 20:44:40 2009
New Revision: 76600

Log:
Issue 7410: deepcopy of itertools.count resets the count



Modified:
   python/trunk/Lib/test/test_itertools.py
   python/trunk/Modules/itertoolsmodule.c

Modified: python/trunk/Lib/test/test_itertools.py
==============================================================================
--- python/trunk/Lib/test/test_itertools.py	(original)
+++ python/trunk/Lib/test/test_itertools.py	Mon Nov 30 20:44:40 2009
@@ -7,6 +7,8 @@
 import sys
 import operator
 import random
+import copy
+import pickle
 maxsize = test_support.MAX_Py_ssize_t
 minsize = -maxsize-1
 
@@ -346,6 +348,13 @@
             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_count_with_stride(self):
         self.assertEqual(zip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
         self.assertEqual(zip('abc',count(start=2,step=3)),

Modified: python/trunk/Modules/itertoolsmodule.c
==============================================================================
--- python/trunk/Modules/itertoolsmodule.c	(original)
+++ python/trunk/Modules/itertoolsmodule.c	Mon Nov 30 20:44:40 2009
@@ -3375,6 +3375,21 @@
 	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(start=0, step=1]) --> count object\n\
 \n\
@@ -3416,7 +3431,7 @@
 	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 */


More information about the Python-checkins mailing list