diff -r 001514146c21 Lib/test/test_itertools.py --- a/Lib/test/test_itertools.py Mon Dec 07 11:32:00 2015 +0200 +++ b/Lib/test/test_itertools.py Mon Dec 07 15:18:08 2015 +0200 @@ -1448,6 +1448,16 @@ class TestExamples(unittest.TestCase): self.assertEqual(list(copy.deepcopy(it)), accumulated[1:]) self.assertEqual(list(copy.copy(it)), accumulated[1:]) + def test_accumulate_reducible_none(self): + # Issue #25718: total is None + it = accumulate([None, None, None], operator.is_) + self.assertEqual(next(it), None) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + it_copy = pickle.loads(pickle.dumps(it, proto)) + self.assertEqual(list(it_copy), [True, False]) + self.assertEqual(list(copy.deepcopy(it)), [True, False]) + self.assertEqual(list(copy.copy(it)), [True, False]) + def test_chain(self): self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF') diff -r 001514146c21 Modules/itertoolsmodule.c --- a/Modules/itertoolsmodule.c Mon Dec 07 11:32:00 2015 +0200 +++ b/Modules/itertoolsmodule.c Mon Dec 07 15:18:08 2015 +0200 @@ -3488,6 +3488,23 @@ accumulate_next(accumulateobject *lz) static PyObject * accumulate_reduce(accumulateobject *lz) { + if (lz->total == Py_None) { + PyObject *it; + + if (PyType_Ready(&chain_type) < 0) + return NULL; + if (PyType_Ready(&islice_type) < 0) + return NULL; + it = PyObject_CallFunction((PyObject *)&chain_type, "(O)O", + lz->total, lz->it); + if (it == NULL) + return NULL; + it = PyObject_CallFunction((PyObject *)Py_TYPE(lz), "NO", + it, lz->binop?lz->binop:Py_None); + if (it == NULL) + return NULL; + return Py_BuildValue("O(NiO)", &islice_type, it, 1, Py_None); + } return Py_BuildValue("O(OO)O", Py_TYPE(lz), lz->it, lz->binop?lz->binop:Py_None, lz->total?lz->total:Py_None);