Skip to content

Commit 930ba0c

Browse files
skirpichevpicnixz
andauthored
gh-126618: fix repr(itertools.count(sys.maxsize)) (#127048)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent c112de1 commit 930ba0c

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

Lib/test/test_itertools.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,15 @@ def test_count(self):
518518
self.assertEqual(next(c), -8)
519519
self.assertEqual(repr(count(10.25)), 'count(10.25)')
520520
self.assertEqual(repr(count(10.0)), 'count(10.0)')
521+
522+
self.assertEqual(repr(count(maxsize)), f'count({maxsize})')
523+
c = count(maxsize - 1)
524+
self.assertEqual(repr(c), f'count({maxsize - 1})')
525+
next(c) # c is now at masize
526+
self.assertEqual(repr(c), f'count({maxsize})')
527+
next(c)
528+
self.assertEqual(repr(c), f'count({maxsize + 1})')
529+
521530
self.assertEqual(type(next(count(10.0))), float)
522531
for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
523532
# Test repr
@@ -578,6 +587,20 @@ def test_count_with_step(self):
578587
self.assertEqual(type(next(c)), int)
579588
self.assertEqual(type(next(c)), float)
580589

590+
c = count(maxsize -2, 2)
591+
self.assertEqual(repr(c), f'count({maxsize - 2}, 2)')
592+
next(c) # c is now at masize
593+
self.assertEqual(repr(c), f'count({maxsize}, 2)')
594+
next(c)
595+
self.assertEqual(repr(c), f'count({maxsize + 2}, 2)')
596+
597+
c = count(maxsize + 1, -1)
598+
self.assertEqual(repr(c), f'count({maxsize + 1}, -1)')
599+
next(c) # c is now at masize
600+
self.assertEqual(repr(c), f'count({maxsize}, -1)')
601+
next(c)
602+
self.assertEqual(repr(c), f'count({maxsize - 1}, -1)')
603+
581604
@threading_helper.requires_working_threading()
582605
def test_count_threading(self, step=1):
583606
# this test verifies multithreading consistency, which is
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the representation of :class:`itertools.count` objects when the count
2+
value is :data:`sys.maxsize`.

Modules/itertoolsmodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,7 +3235,7 @@ typedef struct {
32353235
32363236
fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified.
32373237
3238-
assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyLong(1));
3238+
assert(long_cnt == NULL && long_step==PyLong(1));
32393239
Advances with: cnt += 1
32403240
When count hits PY_SSIZE_T_MAX, switch to slow_mode.
32413241
@@ -3291,9 +3291,6 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
32913291
PyErr_Clear();
32923292
fast_mode = 0;
32933293
}
3294-
else if (cnt == PY_SSIZE_T_MAX) {
3295-
fast_mode = 0;
3296-
}
32973294
}
32983295
} else {
32993296
cnt = 0;
@@ -3325,7 +3322,7 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
33253322
else
33263323
cnt = PY_SSIZE_T_MAX;
33273324

3328-
assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && fast_mode) ||
3325+
assert((long_cnt == NULL && fast_mode) ||
33293326
(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && !fast_mode));
33303327
assert(!fast_mode ||
33313328
(PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1));
@@ -3418,7 +3415,7 @@ count_next(countobject *lz)
34183415
static PyObject *
34193416
count_repr(countobject *lz)
34203417
{
3421-
if (lz->cnt != PY_SSIZE_T_MAX)
3418+
if (lz->long_cnt == NULL)
34223419
return PyUnicode_FromFormat("%s(%zd)",
34233420
_PyType_Name(Py_TYPE(lz)), lz->cnt);
34243421

0 commit comments

Comments
 (0)