From 21a1ef8c29e8a9ac41752182c89a5dfb93d66ff4 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 5 Mar 2025 03:58:39 -0500 Subject: [PATCH 1/2] gh-130824: Add tests for `NULL` in `PyLong_*AndOverflow` functions (GH-130828) (cherry picked from commit 90130807d9c5a55b2a64024f5dfbee4785b9a27c) Co-authored-by: Peter Bierma Co-authored-by: Sergey B Kirpichev --- Lib/test/test_capi/test_long.py | 5 ++--- Modules/_testlimitedcapi/long.c | 6 ++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_capi/test_long.py b/Lib/test/test_capi/test_long.py index b33edc0d0429f1..841de107a02821 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -210,9 +210,8 @@ def check_long_asintandoverflow(self, func, min_val, max_val): self.assertEqual(func(min_val - 1), (-1, -1)) self.assertEqual(func(max_val + 1), (-1, +1)) - - # CRASHES func(1.0) - # CRASHES func(NULL) + self.assertRaises(SystemError, func, None) + self.assertRaises(TypeError, func, 1.0) def test_long_asint(self): # Test PyLong_AsInt() diff --git a/Modules/_testlimitedcapi/long.c b/Modules/_testlimitedcapi/long.c index 5953009b6ef9b7..91c92e0b7eb01e 100644 --- a/Modules/_testlimitedcapi/long.c +++ b/Modules/_testlimitedcapi/long.c @@ -625,7 +625,8 @@ pylong_aslongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long value = PyLong_AsLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - assert(overflow == -1); + // overflow can be 0 if a separate exception occurred + assert(overflow == -1 || overflow == 0); return NULL; } return Py_BuildValue("li", value, overflow); @@ -671,7 +672,8 @@ pylong_aslonglongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long long value = PyLong_AsLongLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - assert(overflow == -1); + // overflow can be 0 if a separate exception occurred + assert(overflow == -1 || overflow == 0); return NULL; } return Py_BuildValue("Li", value, overflow); From a4861bfa9d2c31e72954813eef73fcee785d558d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 5 Mar 2025 12:50:53 +0200 Subject: [PATCH 2/2] gh-130824: Clean up test wrappers for PyLong_*AndOverflow functions (GH-130871) --- Modules/_testlimitedcapi/long.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Modules/_testlimitedcapi/long.c b/Modules/_testlimitedcapi/long.c index 91c92e0b7eb01e..e2af09d0840bba 100644 --- a/Modules/_testlimitedcapi/long.c +++ b/Modules/_testlimitedcapi/long.c @@ -625,8 +625,7 @@ pylong_aslongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long value = PyLong_AsLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - // overflow can be 0 if a separate exception occurred - assert(overflow == -1 || overflow == 0); + assert(overflow == 0); return NULL; } return Py_BuildValue("li", value, overflow); @@ -672,8 +671,7 @@ pylong_aslonglongandoverflow(PyObject *module, PyObject *arg) int overflow = UNINITIALIZED_INT; long long value = PyLong_AsLongLongAndOverflow(arg, &overflow); if (value == -1 && PyErr_Occurred()) { - // overflow can be 0 if a separate exception occurred - assert(overflow == -1 || overflow == 0); + assert(overflow == 0); return NULL; } return Py_BuildValue("Li", value, overflow);