Skip to content

Commit 46710ca

Browse files
authored
[3.12] gh-126595: fix a crash when calling itertools.count(sys.maxsize) (GH-126617) (#126740)
gh-126595: fix a crash when calling `itertools.count(sys.maxsize)` (#126617)
1 parent b13b84a commit 46710ca

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
@@ -599,6 +599,8 @@ def test_count(self):
599599
self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
600600
self.assertRaises(TypeError, count, 2, 3, 4)
601601
self.assertRaises(TypeError, count, 'a')
602+
self.assertEqual(take(3, count(maxsize)),
603+
[maxsize, maxsize + 1, maxsize + 2])
602604
self.assertEqual(take(10, count(maxsize-5)),
603605
list(range(maxsize-5, maxsize+5)))
604606
self.assertEqual(take(10, count(-maxsize-5)),
@@ -654,6 +656,12 @@ def test_count_with_stride(self):
654656
self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3)))
655657
self.assertEqual(take(3, count(10, maxsize+5)),
656658
list(range(10, 10+3*(maxsize+5), maxsize+5)))
659+
self.assertEqual(take(3, count(maxsize, 2)),
660+
[maxsize, maxsize + 2, maxsize + 4])
661+
self.assertEqual(take(3, count(maxsize, maxsize)),
662+
[maxsize, 2 * maxsize, 3 * maxsize])
663+
self.assertEqual(take(3, count(-maxsize, maxsize)),
664+
[-maxsize, 0, maxsize])
657665
self.assertEqual(take(3, count(2, 1.25)), [2, 3.25, 4.5])
658666
self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j])
659667
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
@@ -4037,6 +4037,9 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
40374037
PyErr_Clear();
40384038
fast_mode = 0;
40394039
}
4040+
else if (cnt == PY_SSIZE_T_MAX) {
4041+
fast_mode = 0;
4042+
}
40404043
}
40414044
} else {
40424045
cnt = 0;

0 commit comments

Comments
 (0)