10000 [3.13] gh-126618: fix repr(itertools.count(sys.maxsize)) (GH-127048) … · python/cpython@4bafce0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4bafce0

Browse files
miss-islingtonskirpichevpicnixz
authored
[3.13] gh-126618: fix repr(itertools.count(sys.maxsize)) (GH-127048) (#127508)
gh-126618: fix repr(itertools.count(sys.maxsize)) (GH-127048) (cherry picked from commit 930ba0c) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 943e57e commit 4bafce0

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
@@ -627,6 +627,15 @@ def test_count(self):
627627
self.assertEqual(next(c), -8)
628628
self.assertEqual(repr(count(10.25)), 'count(10.25)')
629629
self.assertEqual(repr(count(10.0)), 'count(10.0)')
630+
631+
self.assertEqual(repr(count(maxsize)), f'count({maxsize})')
632+
c = count(maxsize - 1)
633+
self.assertEqual(repr(c), f'count({maxsize - 1})')
634+
next(c) # c is now at masize
635+
self.assertEqual(repr(c), f'count({maxsize})')
636+
next(c)
637+
self.assertEqual(repr(c), f'count({maxsize + 1})')
638+
630639
self.assertEqual(type(next(count(10.0))), float)
631640
for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
632641
# Test repr
@@ -707,6 +716,20 @@ def test_count_with_step(self):
707716
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
708717
self.pickletest(proto, count(i, j))
709718

719+
c = count(maxsize -2, 2)
720+
self.assertEqual(repr(c), f'count({maxsize - 2}, 2)')
721+
next(c) # c is now at masize
722+
self.assertEqual(repr(c), f'count({maxsize}, 2)')
723+
next(c)
724+
self.assertEqual(repr(c), f'count({maxsize + 2}, 2)')
725+
726+
c = count(maxsize + 1, -1)
727+
self.assertEqual(repr(c), f'count({maxsize + 1}, -1)')
728+
next(c) # c is now at masize
729+
self.assertEqual(repr(c), f'count({maxsize}, -1)')
730+
next(c)
731+
self.assertEqual(repr(c), f'count({maxsize - 1}, -1)')
732+
710733
@threading_helper.requires_working_threading()
711734
def test_count_threading(self, step=1):
712735
# 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
@@ -4019,7 +4019,7 @@ typedef struct {
40194019
40204020
fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified.
40214021
4022-
assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyLong(1));
4022+
assert(long_cnt == NULL && long_step==PyLong(1));
40234023
Advances with: cnt += 1
40244024
When count hits PY_SSIZE_T_MAX, switch to slow_mode.
40254025
@@ -4075,9 +4075,6 @@ 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-
}
40814078
}
40824079
} else {
40834080
cnt = 0;
@@ -4109,7 +4106,7 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
41094106
else
41104107
cnt = PY_SSIZE_T_MAX;
41114108

4112-
assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && fast_mode) ||
4109+
assert((long_cnt == NULL && fast_mode) ||
41134110
(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && !fast_mode));
41144111
assert(!fast_mode ||
41154112
(PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1));
@@ -4202,7 +4199,7 @@ count_next(countobject *lz)
42024199
static PyObject *
42034200
count_repr(countobject *lz)
42044201
{
4205-
if (lz->cnt != PY_SSIZE_T_MAX)
4202+
if (lz->long_cnt == NULL)
42064203
return PyUnicode_FromFormat("%s(%zd)",
42074204
_PyType_Name(Py_TYPE(lz)), lz->cnt);
42084205

0 commit comments

Comments
 (0)
0