Kaydet (Commit) 673827c6 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Issue #7410: deepcopy of itertools.count() erroneously reset the count.

üst af7d3bcc
...@@ -5,6 +5,8 @@ from weakref import proxy ...@@ -5,6 +5,8 @@ from weakref import proxy
import sys import sys
import operator import operator
import random import random
import copy
import pickle
maxsize = test_support.MAX_Py_ssize_t maxsize = test_support.MAX_Py_ssize_t
minsize = -maxsize-1 minsize = -maxsize-1
...@@ -215,6 +217,13 @@ class TestBasicOps(unittest.TestCase): ...@@ -215,6 +217,13 @@ class TestBasicOps(unittest.TestCase):
r2 = 'count(%r)'.__mod__(i).replace('L', '') r2 = 'count(%r)'.__mod__(i).replace('L', '')
self.assertEqual(r1, r2) 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): def test_cycle(self):
self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
self.assertEqual(list(cycle('')), []) self.assertEqual(list(cycle('')), [])
......
...@@ -33,6 +33,8 @@ Core and Builtins ...@@ -33,6 +33,8 @@ Core and Builtins
Library Library
------- -------
- Issue #7410: deepcopy of itertools.count() erroneously reset the count.
- Issue #7403: logging: Fixed possible race condition in lock creation. - Issue #7403: logging: Fixed possible race condition in lock creation.
- Issue #7341: Close the internal file object in the TarFile constructor in - Issue #7341: Close the internal file object in the TarFile constructor in
......
...@@ -2906,6 +2906,21 @@ count_repr(countobject *lz) ...@@ -2906,6 +2906,21 @@ count_repr(countobject *lz)
return result; 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, PyDoc_STRVAR(count_doc,
"count([firstval]) --> count object\n\ "count([firstval]) --> count object\n\
\n\ \n\
...@@ -2941,7 +2956,7 @@ static PyTypeObject count_type = { ...@@ -2941,7 +2956,7 @@ static PyTypeObject count_type = {
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */ PyObject_SelfIter, /* tp_iter */
(iternextfunc)count_next, /* tp_iternext */ (iternextfunc)count_next, /* tp_iternext */
0, /* tp_methods */ count_methods, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
0, /* tp_getset */ 0, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment