Skip to content

Commit 9013080

Browse files
gh-130824: Add tests for NULL in PyLong_*AndOverflow functions (GH-130828)
Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent e53d105 commit 9013080

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

Lib/test/test_capi/test_long.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ def check_long_asintandoverflow(self, func, min_val, max_val):
211211

212212
self.assertEqual(func(min_val - 1), (-1, -1))
213213
self.assertEqual(func(max_val + 1), (-1, +1))
214-
215-
# CRASHES func(1.0)
216-
# CRASHES func(NULL)
214+
self.assertRaises(SystemError, func, None)
215+
self.assertRaises(TypeError, func, 1.0)
217216

218217
def test_long_asint(self):
219218
# Test PyLong_AsInt()

Modules/_testlimitedcapi/long.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ pylong_aslongandoverflow(PyObject *module, PyObject *arg)
625625
int overflow = UNINITIALIZED_INT;
626626
long value = PyLong_AsLongAndOverflow(arg, &overflow);
627627
if (value == -1 && PyErr_Occurred()) {
628-
assert(overflow == -1);
628+
// overflow can be 0 if a separate exception occurred
629+
assert(overflow == -1 || overflow == 0);
629630
return NULL;
630631
}
631632
return Py_BuildValue("li", value, overflow);
@@ -671,7 +672,8 @@ pylong_aslonglongandoverflow(PyObject *module, PyObject *arg)
671672
int overflow = UNINITIALIZED_INT;
672673
long long value = PyLong_AsLongLongAndOverflow(arg, &overflow);
673674
if (value == -1 && PyErr_Occurred()) {
674-
assert(overflow == -1);
675+
// overflow can be 0 if a separate exception occurred
676+
assert(overflow == -1 || overflow == 0);
675677
return NULL;
676678
}
677679
return Py_BuildValue("Li", value, overflow);

0 commit comments

Comments
 (0)