8000 [3.9] bpo-44984: Rewrite test_null_strings in _testcapi (GH-27904) (G… · python/cpython@b0df288 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0df288

Browse files
[3.9] bpo-44984: Rewrite test_null_strings in _testcapi (GH-27904) (GH-27910)
Test also PyObject_Repr(NULL) and PyObject_Bytes(NULL).. (cherry picked from commit 4d68917) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 7543e7f commit b0df288

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
@@ -537,6 +537,18 @@ def test_pynumber_tobase(self):
537537
self.assertRaises(TypeError, pynumber_tobase, '123', 10)
538538
self.assertRaises(SystemError, pynumber_tobase, 123, 0)
539539

540+
def test_pyobject_repr_from_null(self):
541+
s = _testcapi.pyobject_repr_from_null()
542+
self.assertEqual(s, '<NULL>')
543+
544+
def test_pyobject_str_from_null(self):
545+
s = _testcapi.pyobject_str_from_null()
546+
self.assertEqual(s, '<NULL>')
547+
548+
def test_pyobject_bytes_from_null(self):
549+
s = _testcapi.pyobject_bytes_from_null()
550+
self.assertEqual(s, b'<NULL>')
551+
540552

541553
class TestPendingCalls(unittest.TestCase):
542554

Modules/_testcapimodule.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,16 +2215,22 @@ test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored))
22152215
Py_RETURN_NONE;
22162216
}
22172217

2218-
/* Example passing NULLs to PyObject_Str(NULL). */
2218+
static PyObject *
2219+
pyobject_repr_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2220+
{
2221+
return PyObject_Repr(NULL);
2222+
}
22192223

22202224
static PyObject *
2221-
test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored))
2225+
pyobject_str_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
22222226
{
2223-
PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
2224-
PyObject *tuple = PyTuple_Pack(2, o1, o2);
2225-
Py_XDECREF(o1);
2226-
Py_XDECREF(o2);
2227-
return tuple;
2227+
return PyObject_Str(NULL);
2228+
}
2229+
2230+
static PyObject *
2231+
pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2232+
{
2233+
return PyObject_Bytes(NULL);
22282234
}
22292235

22302236
static PyObject *
@@ -5371,7 +5377,9 @@ static PyMethodDef TestMethods[] = {
53715377
{"test_k_code", test_k_code, METH_NOARGS},
53725378
{"test_empty_argparse", test_empty_argparse, METH_NOARGS},
53735379
{"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
5374-
{"test_null_strings", test_null_strings, METH_NOARGS},
5380+
{"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS},
5381+
{"pyobject_str_from_null", pyobject_str_from_null, METH_NOARGS},
5382+
{"pyobject_bytes_from_null", pyobject_bytes_from_null, METH_NOARGS},
53755383
{"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
53765384
{"test_with_docstring", test_with_docstring, METH_NOARGS,
53775385
PyDoc_STR("This is a pretty normal docstring.")},

0 commit comments

Comments
 (0)
0