From d8a39639bb34bea896880d10edf448b456e9fb73 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 5 Mar 2025 03:58:39 -0500 Subject: [PATCH 1/2] [3.12] 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/_testcapi/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 18507ed7c3bfec5..05b3a7ebd63d648 100644 --- a/Lib/test/test_capi/test_long.py +++ b/Lib/test/test_capi/test_long.py @@ -208,9 +208,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_aslong(self): # Test PyLong_AsLong() and PyLong_FromLong() diff --git a/Modules/_testcapi/long.c b/Modules/_testcapi/long.c index 9c5a0e386759e73..ac20be8d1e37f51 100644 --- a/Modules/_testcapi/long.c +++ b/Modules/_testcapi/long.c @@ -629,7 +629,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); @@ -675,7 +676,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 21942c9337017dccd00e3299fe9acc6584a6cde5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 5 Mar 2025 11:52:36 +0100 Subject: [PATCH 2/2] gh-130824: Clean up test wrappers for PyLong_*AndOverflow functions --- Modules/_testcapi/long.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Modules/_testcapi/long.c b/Modules/_testcapi/long.c index ac20be8d1e37f51..16eb437ffea1782 100644 --- a/Modules/_testcapi/long.c +++ b/Modules/_testcapi/long.c @@ -629,8 +629,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); @@ -676,8 +675,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);