Skip to content

Commit

Permalink
pythongh-126595: fix a crash when calling `itertools.count(sys.maxsiz…
Browse files Browse the repository at this point in the history
…e)` (pythonGH-126617)

(cherry picked from commit 6e3bb8a)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
  • Loading branch information
picnixz authored and miss-islington committed Nov 12, 2024
1 parent 2da063e commit 8e596cb
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 8e596cb

Please sign in to comment.