]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-126618: fix repr(itertools.count(sys.maxsize)) (GH-127048) (#127508)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Dec 2024 13:38:13 +0000 (14:38 +0100)
committerGitHub <noreply@github.com>
Mon, 2 Dec 2024 13:38:13 +0000 (13:38 +0000)
gh-126618: fix repr(itertools.count(sys.maxsize)) (GH-127048)
(cherry picked from commit 930ba0ce605eee9e3b992fa368b00a3f2b7dc4c1)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/test/test_itertools.py
Misc/NEWS.d/next/Library/2024-11-20-08-54-11.gh-issue-126618.ef_53g.rst [new file with mode: 0644]
Modules/itertoolsmodule.c

index 2ac0400198864a6ed68ee639611367646b0e1800..ab382cf4dd13e1175065062f635ea22556ab7200 100644 (file)
@@ -627,6 +627,15 @@ class TestBasicOps(unittest.TestCase):
         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
@@ -707,6 +716,20 @@ class TestBasicOps(unittest.TestCase):
                 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                     self.pickletest(proto, count(i, j))
 
+        c = count(maxsize -2, 2)
+        self.assertEqual(repr(c), f'count({maxsize - 2}, 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
diff --git a/Misc/NEWS.d/next/Library/2024-11-20-08-54-11.gh-issue-126618.ef_53g.rst b/Misc/NEWS.d/next/Library/2024-11-20-08-54-11.gh-issue-126618.ef_53g.rst
new file mode 100644 (file)
index 0000000..7a0a7b7
--- /dev/null
@@ -0,0 +1,2 @@
+Fix the representation of :class:`itertools.count` objects when the count
+value is :data:`sys.maxsize`.
index 9e2c668ed9cabb9b359078fbaf01ee0af7932361..8ed7683be28e6ceb342f77d047e27b49deea1405 100644 (file)
@@ -4019,7 +4019,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.
 
@@ -4075,9 +4075,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;
@@ -4109,7 +4106,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));
@@ -4202,7 +4199,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);