Skip to content

Commit 4d68917

Browse files
bpo-44984: Rewrite test_null_strings in _testcapi (GH-27904)
Test also PyObject_Repr(NULL) and PyObject_Bytes(NULL).
1 parent 1a995b0 commit 4d68917

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

Lib/test/test_capi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,18 @@ def test_fatal_error(self):
623623
''')
624624
self.check_fatal_error(code, expected)
625625

626+
def test_pyobject_repr_from_null(self):
627+
s = _testcapi.pyobject_repr_from_null()
628+
self.assertEqual(s, '<NULL>')
629+
630+
def test_pyobject_str_from_null(self):
631+
s = _testcapi.pyobject_str_from_null()
632+
self.assertEqual(s, '<NULL>')
633+
634+
def test_pyobject_bytes_from_null(self):
635+
s = _testcapi.pyobject_bytes_from_null()
636+
self.assertEqual(s, b'<NULL>')
637+
626638

627639
class TestPendingCalls(unittest.TestCase):
628640

Modules/_testcapimodule.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,16 +2376,22 @@ test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored))
23762376
Py_RETURN_NONE;
23772377
}
23782378

2379-
/* Example passing NULLs to PyObject_Str(NULL). */
2379+
static PyObject *
2380+
pyobject_repr_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2381+
{
2382+
return PyObject_Repr(NULL);
2383+
}
23802384

23812385
static PyObject *
2382-
test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored))
2386+
pyobject_str_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
23832387
{
2384-
PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
2385-
PyObject *tuple = PyTuple_Pack(2, o1, o2);
2386-
Py_XDECREF(o1);
2387-
Py_XDECREF(o2);
2388-
return tuple;
2388+
return PyObject_Str(NULL);
2389+
}
2390+
2391+
static PyObject *
2392+
pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2393+
{
2394+
return PyObject_Bytes(NULL);
23892395
}
23902396

23912397
static PyObject *
@@ -5702,7 +5708,9 @@ static PyMethodDef TestMethods[] = {
57025708
{"test_k_code", test_k_code, METH_NOARGS},
57035709
{"test_empty_argparse", test_empty_argparse, METH_NOARGS},
57045710
{"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
5705-
{"test_null_strings", test_null_strings, METH_NOARGS},
5711+
{"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS},
5712+
{"pyobject_str_from_null", pyobject_str_from_null, METH_NOARGS},
5713+
{"pyobject_bytes_from_null", pyobject_bytes_from_null, METH_NOARGS},
57065714
{"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
57075715
{"test_with_docstring", test_with_docstring, METH_NOARGS,
57085716
PyDoc_STR("This is a pretty normal docstring.")},

0 commit comments

Comments
 (0)