Skip to content

Commit 376e358

Browse files
committed
More reliable test
1 parent 9693afe commit 376e358

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Lib/test/test_capi/test_long.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ def test_long_asnativebytes(self):
428428
import math
429429
from _testcapi import (
430430
pylong_asnativebytes as asnativebytes,
431-
INT_MAX,
432-
SIZE_MAX,
431+
pylong_asnativebytes_too_big_n,
432+
SIZE_MAX
433433
)
434434

435435
# Abbreviate sizeof(Py_ssize_t) to SZ because we use it a lot
@@ -528,11 +528,11 @@ def test_long_asnativebytes(self):
528528
asnativebytes(1, buffer, 0, 2)
529529
with self.assertRaises(TypeError):
530530
asnativebytes('not a number', buffer, 0, -1)
531+
532+
# We pass any number we like, but the function will pass an n_bytes
533+
# that is too big to make sure we fail
531534
with self.assertRaises(SystemError):
532-
# n_bytes is taken as size_t and clamped to 'int'. With the sign
533-
# change, we should always be able to pass INT_MAX+1 and see this
534-
# failure.
535-
asnativebytes(1, buffer, INT_MAX + 1, -1)
535+
pylong_asnativebytes_too_big_n(100)
536536

537537
def test_long_fromnativebytes(self):
538538
import math

Modules/_testcapi/long.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ pylong_asnativebytes(PyObject *module, PyObject *args)
802802
return res >= 0 ? PyLong_FromLong(res) : NULL;
803803
}
804804

805+
static PyObject *
806+
pylong_asnativebytes_too_big_n(PyObject *module, PyObject *v)
807+
{
808+
int res = PyLong_AsNativeBytes(v, NULL, (size_t)INT_MAX + 1, -1);
809+
return res >= 0 ? PyLong_FromLong(res) : NULL;
810+
}
811+
805812
static PyObject *
806813
pylong_fromnativebytes(PyObject *module, PyObject *args)
807814
{
@@ -852,6 +859,7 @@ static PyMethodDef test_methods[] = {
852859
{"pylong_asdouble", pylong_asdouble, METH_O},
853860
{"pylong_asvoidptr", pylong_asvoidptr, METH_O},
854861
{"pylong_asnativebytes", pylong_asnativebytes, METH_VARARGS},
862+
{"pylong_asnativebytes_too_big_n", pylong_asnativebytes_too_big_n, METH_O},
855863
{"pylong_fromnativebytes", pylong_fromnativebytes, METH_VARARGS},
856864
{NULL},
857865
};

0 commit comments

Comments
 (0)