8000 gh-126618: fix repr(itertools.count(sys.maxsize)) by skirpichev · Pull Request #127048 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126618: fix repr(itertools.count(sys.maxsize)) #127048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,15 @@ def test_count(self):
self.assertEqual(next(c), -8)
self.assertEqual(repr(count(10.25)), 'count(10.25)')
self.assertEqual(repr(count(10.0)), 'count(10.0)')

self.assertEqual(repr(count(maxsize)), f'count({maxsize})')
c = count(maxsize - 1)
self.assertEqual(repr(c), f'count({maxsize - 1})')
next(c) # c is now at masize
self.assertEqual(repr(c), f'count({maxsize})')
next(c)
self.assertEqual(repr(c), f'count({maxsize + 1})')

self.assertEqual(type(next(count(10.0))), float)
for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
# Test repr
Expand Down Expand Up @@ -578,6 +587,20 @@ def test_count_with_step(self):
self.assertEqual(type(next(c)), int)
self.assertEqual(type(next(c)), float)

c = count(maxsize -2, 2)
self.assertEqual(repr(c), f'count({maxsize - 2}, 8000 2)')
next(c) # c is now at masize
self.assertEqual(repr(c), f'count({maxsize}, 2)')
next(c)
self.assertEqual(repr(c), f'count({maxsize + 2}, 2)')

c = count(maxsize + 1, -1)
self.assertEqual(repr(c), f'count({maxsize + 1}, -1)')
next(c) # c is now at masize
self.assertEqual(repr(c), f'count({maxsize}, -1)')
next(c)
self.assertEqual(repr(c), f'count({maxsize - 1}, -1)')

@threading_helper.requires_working_threading()
def test_count_threading(self, step=1):
# this test verifies multithreading consistency, which is
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the representation of :class:`itertools.count` objects when the count
value is :data:`sys.maxsize`.
9 changes: 3 additions & 6 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3235,7 +3235,7 @@ typedef struct {

fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified.

assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyLong(1));
assert(long_cnt == NULL && long_step==PyLong(1));
Advances with: cnt += 1
When count hits PY_SSIZE_T_MAX, switch to slow_mode.

Expand Down Expand Up @@ -3291,9 +3291,6 @@ 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 Expand Up @@ -3325,7 +3322,7 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
else
cnt = PY_SSIZE_T_MAX;

assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && fast_mode) ||
assert((long_cnt == NULL && fast_mode) ||
(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && !fast_mode));
assert(!fast_mode ||
(PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1));
Expand Down Expand Up @@ -3418,7 +3415,7 @@ count_next(countobject *lz)
static PyObject *
count_repr(countobject *lz)
{
if (lz->cnt != PY_SSIZE_T_MAX)
if (lz->long_cnt == NULL)
return PyUnicode_FromFormat("%s(%zd)",
_PyType_Name(Py_TYPE(lz)), lz->cnt);

Expand Down
Loading
0