Skip to content

Commit 865f096

Browse files
[3.13] gh-126595: fix a crash when calling itertools.count(sys.maxsize) (GH-126617) (#126739)
gh-126595: fix a crash when calling `itertools.count(sys.maxsize)` (GH-126617) (cherry picked from commit 6e3bb8a) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 2da063e commit 865f096

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/test/test_itertools.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ def test_count(self):
603603
self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
604604
self.assertRaises(TypeError, count, 2, 3, 4)
605605
self.assertRaises(TypeError, count, 'a')
606+
self.assertEqual(take(3, count(maxsize)),
607+
[maxsize, maxsize + 1, maxsize + 2])
606608
self.assertEqual(take(10, count(maxsize-5)),
607609
list(range(maxsize-5, maxsize+5)))
608610
self.assertEqual(take(10, count(-maxsize-5)),
@@ -658,6 +660,12 @@ def test_count_with_step(self):
658660
self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3)))
659661
self.assertEqual(take(3, count(10, maxsize+5)),
660662
list(range(10, 10+3*(maxsize+5), maxsize+5)))
663+
self.assertEqual(take(3, count(maxsize, 2)),
664+
[maxsize, maxsize + 2, maxsize + 4])
665+
self.assertEqual(take(3, count(maxsize, maxsize)),
666+
[maxsize, 2 * maxsize, 3 * maxsize])
667+
self.assertEqual(take(3, count(-maxsize, maxsize)),
668+
[-maxsize, 0, maxsize])
661669
self.assertEqual(take(3, count(2, 1.25)), [2, 3.25, 4.5])
662670
self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j])
663671
self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when instantiating :class:`itertools.count` with an initial
2+
count of :data:`sys.maxsize` on debug builds. Patch by Bénédikt Tran.

Modules/itertoolsmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,9 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
40754075
PyErr_Clear();
40764076
fast_mode = 0;
40774077
}
4078+
else if (cnt == PY_SSIZE_T_MAX) {
4079+
fast_mode = 0;
4080+
}
40784081
}
40794082
} else {
40804083
cnt = 0;

0 commit comments

Comments
 (0)