From d31f7efdd13bac409f32d33bab63ad53dccc97de Mon Sep 17 00:00:00 2001 From: emirplus Date: Fri, 23 Oct 2026 19:38:24 +0300 Subject: [PATCH 1/2] gh-160050: Use asymptotically faster divmod in l_mod for large integers --- Lib/test/test_int.py | 8 ++++++++ .../2026-10-23-19-37-00.gh-issue-160050.aB3x9Q.rst | 1 + Objects/longobject.c | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-19-37-00.gh-issue-160050.aB3x9Q.rst diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index fa675f626cbe109..222799f76f913b4 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -778,6 +778,13 @@ def test_pylong_int_divmod(self): a, b = divmod(n*3 + 1, n) assert a == 3 and b == 1 + def test_pylong_int_mod(self): + n = (1 << 100_000) + self.assertEqual((n * 3 + 1) % n, 1) + self.assertEqual((-n * 3 - 1) % n, n - 1) + self.assertEqual((n * 3 + 1) % -n, 1 - n) + self.assertEqual((-n * 3 - 1) % -n, -1) + @support.cpython_only # tests implementation details of CPython. @unittest.skipUnless(_pylong, "_pylong module required") def test_pylong_int_divmod_crash(self): @@ -789,6 +796,7 @@ def test_pylong_int_divmod_crash(self): with mock.patch.object(_pylong, "int_divmod", wraps=bad_int_divmod): msg = r"tuple of length 2 is required from int_divmod\(\)" self.assertRaisesRegex(ValueError, msg, divmod, a, b) + self.assertRaisesRegex(ValueError, msg, lambda: a % b) def test_pylong_str_to_int(self): v1 = 1 << 100_000 diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-19-37-00.gh-issue-160050.aB3x9Q.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-19-37-00.gh-issue-160050.aB3x9Q.rst new file mode 100644 index 000000000000000..9089e4bc4dbb14b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-23-19-37-00.gh-issue-160050.aB3x9Q.rst @@ -0,0 +1 @@ +Use asymptotically faster :func:`divmod` implementation for integer modulo of large integers. diff --git a/Objects/longobject.c b/Objects/longobject.c index f5526213944011c..1c9b7e7ac49c39d 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4564,6 +4564,13 @@ l_mod(PyLongObject *v, PyLongObject *w, PyLongObject **pmod) *pmod = (PyLongObject *)fast_mod(v, w); return -(*pmod == NULL); } +#if WITH_PYLONG_MODULE + Py_ssize_t size_v = _PyLong_DigitCount(v); /* digits in numerator */ + Py_ssize_t size_w = _PyLong_DigitCount(w); /* digits in denominator */ + if (size_w > 300 && (size_v - size_w) > 150) { + return pylong_int_divmod(v, w, NULL, pmod); + } +#endif if (long_rem(v, w, &mod) < 0) return -1; if ((_PyLong_IsNegative(mod) && _PyLong_IsPositive(w)) || From 7faf1993dbe84debf62b29cd51de04bc6f7a4653 Mon Sep 17 00:00:00 2001 From: emirplus Date: Sun, 25 Oct 2026 12:00:37 +0300 Subject: [PATCH 2/2] gh-160050: Add comment referencing l_divmod in l_mod --- Objects/longobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/longobject.c b/Objects/longobject.c index 1c9b7e7ac49c39d..4cdd3917a9dc996 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4568,6 +4568,7 @@ l_mod(PyLongObject *v, PyLongObject *w, PyLongObject **pmod) Py_ssize_t size_v = _PyLong_DigitCount(v); /* digits in numerator */ Py_ssize_t size_w = _PyLong_DigitCount(w); /* digits in denominator */ if (size_w > 300 && (size_v - size_w) > 150) { + /* Switch to _pylong.int_divmod(). See comments in l_divmod(). */ return pylong_int_divmod(v, w, NULL, pmod); } #endif