Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5608411

Browse files
Issue #25718: Fixed pickling and copying the accumulate() iterator with total is None.
2 parents b6bfce6 + d551625 commit 5608411

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lib/test/test_itertools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,16 @@ def test_accumulate_reducible(self):
14481448
self.assertEqual(list(copy.deepcopy(it)), accumulated[1:])
14491449
self.assertEqual(list(copy.copy(it)), accumulated[1:])
14501450

1451+
def test_accumulate_reducible_none(self):
1452+
# Issue #25718: total is None
1453+
it = accumulate([None, None, None], operator.is_)
1454+
self.assertEqual(next(it), None)
1455+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1456+
it_copy = pickle.loads(pickle.dumps(it, proto))
1457+
self.assertEqual(list(it_copy), [True, False])
1458+
self.assertEqual(list(copy.deepcopy(it)), [True, False])
1459+
self.assertEqual(list(copy.copy(it)), [True, False])
1460+
14511461
def test_chain(self):
14521462
self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF')
14531463

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ Core and Builtins
201201
Library
202202
-------
203203

204+
- Issue #25718: Fixed pickling and copying the accumulate() iterator with
205+
total is None.
206+
204207
- Issue #26475: Fixed debugging output for regular expressions with the (?x)
205208
flag.
206209

Modules/itertoolsmodule.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,6 +3464,23 @@ accumulate_next(accumulateobject *lz)
34643464
static PyObject *
34653465
accumulate_reduce(accumulateobject *lz)
34663466
{
3467+
if (lz->total == Py_None) {
3468+
PyObject *it;
3469+
3470+
if (PyType_Ready(&chain_type) < 0)
3471+
return NULL;
3472+
if (PyType_Ready(&islice_type) < 0)
3473+
return NULL;
3474+
it = PyObject_CallFunction((PyObject *)&chain_type, "(O)O",
3475+
lz->total, lz->it);
3476+
if (it == NULL)
3477+
return NULL;
3478+
it = PyObject_CallFunction((PyObject *)Py_TYPE(lz), "NO",
3479+
it, lz->binop ? lz->binop : Py_None);
3480+
if (it == NULL)
3481+
return NULL;
3482+
return Py_BuildValue("O(NiO)", &islice_type, it, 1, Py_None);
3483+
}
34673484
return Py_BuildValue("O(OO)O", Py_TYPE(lz),
34683485
lz->it, lz->binop?lz->binop:Py_None,
34693486
lz->total?lz->total:Py_None);

0 commit comments

Comments
 (0)