gh-130824: Add tests for NULL in PyLong_*AndOverflow functions (GH-130828)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
This commit is contained in:
Peter Bierma 2025-03-05 03:58:39 -05:00 committed by GitHub
parent e53d105872
commit 90130807d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 5 deletions

View file

@ -211,9 +211,8 @@ class LongTests(unittest.TestCase):
self.assertEqual(func(min_val - 1), (-1, -1)) self.assertEqual(func(min_val - 1), (-1, -1))
self.assertEqual(func(max_val + 1), (-1, +1)) self.assertEqual(func(max_val + 1), (-1, +1))
self.assertRaises(SystemError, func, None)
# CRASHES func(1.0) self.assertRaises(TypeError, func, 1.0)
# CRASHES func(NULL)
def test_long_asint(self): def test_long_asint(self):
# Test PyLong_AsInt() # Test PyLong_AsInt()

View file

@ -625,7 +625,8 @@ pylong_aslongandoverflow(PyObject *module, PyObject *arg)
int overflow = UNINITIALIZED_INT; int overflow = UNINITIALIZED_INT;
long value = PyLong_AsLongAndOverflow(arg, &overflow); long value = PyLong_AsLongAndOverflow(arg, &overflow);
if (value == -1 && PyErr_Occurred()) { 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 NULL;
} }
return Py_BuildValue("li", value, overflow); return Py_BuildValue("li", value, overflow);
@ -671,7 +672,8 @@ pylong_aslonglongandoverflow(PyObject *module, PyObject *arg)
int overflow = UNINITIALIZED_INT; int overflow = UNINITIALIZED_INT;
long long value = PyLong_AsLongLongAndOverflow(arg, &overflow); long long value = PyLong_AsLongLongAndOverflow(arg, &overflow);
if (value == -1 && PyErr_Occurred()) { 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 NULL;
} }
return Py_BuildValue("Li", value, overflow); return Py_BuildValue("Li", value, overflow);