Skip to content

Commit

Permalink
[3.13] gh-126595: fix a crash when calling `itertools.count(sys.maxsi…
Browse files Browse the repository at this point in the history
…ze)` (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]>
  • Loading branch information
miss-islington and picnixz authored Nov 12, 2024
1 parent 2da063e commit 865f096
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ def test_count(self):
self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
self.assertRaises(TypeError, count, 2, 3, 4)
self.assertRaises(TypeError, count, 'a')
self.assertEqual(take(3, count(maxsize)),
[maxsize, maxsize + 1, maxsize + 2])
self.assertEqual(take(10, count(maxsize-5)),
list(range(maxsize-5, maxsize+5)))
self.assertEqual(take(10, count(-maxsize-5)),
Expand Down Expand Up @@ -658,6 +660,12 @@ def test_count_with_step(self):
self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3)))
self.assertEqual(take(3, count(10, maxsize+5)),
list(range(10, 10+3*(maxsize+5), maxsize+5)))
self.assertEqual(take(3, count(maxsize, 2)),
[maxsize, maxsize + 2, maxsize + 4])
self.assertEqual(take(3, count(maxsize, maxsize)),
[maxsize, 2 * maxsize, 3 * maxsize])
self.assertEqual(take(3, count(-maxsize, maxsize)),
[-maxsize, 0, maxsize])
self.assertEqual(take(3, count(2, 1.25)), [2, 3.25, 4.5])
self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j])
self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when instantiating :class:`itertools.count` with an initial
count of :data:`sys.maxsize` on debug builds. Patch by Bénédikt Tran.
3 changes: 3 additions & 0 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4075,6 +4075,9 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
PyErr_Clear();
fast_mode = 0;
}
else if (cnt == PY_SSIZE_T_MAX) {
fast_mode = 0;
}
}
} else {
cnt = 0;
Expand Down

0 comments on commit 865f096

Please sign in to comment.