From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 23 Aug 2021 19:30:21 +0000 (-0700) Subject: bpo-44984: Rewrite test_null_strings in _testcapi (GH-27904) X-Git-Tag: v3.10.0rc2~62 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6ff7fefc914541ec5afdd3c8193da80818cb0817;p=thirdparty%2FPython%2Fcpython.git bpo-44984: Rewrite test_null_strings in _testcapi (GH-27904) Test also PyObject_Repr(NULL) and PyObject_Bytes(NULL). (cherry picked from commit 4d689173861257c69adc6dc1dca4171946ddb57f) Co-authored-by: Serhiy Storchaka --- diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index f9c5aca728b4..5f78c337028a 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -619,6 +619,18 @@ class CAPITest(unittest.TestCase): ''') self.check_fatal_error(code, expected) + def test_pyobject_repr_from_null(self): + s = _testcapi.pyobject_repr_from_null() + self.assertEqual(s, '') + + def test_pyobject_str_from_null(self): + s = _testcapi.pyobject_str_from_null() + self.assertEqual(s, '') + + def test_pyobject_bytes_from_null(self): + s = _testcapi.pyobject_bytes_from_null() + self.assertEqual(s, b'') + class TestPendingCalls(unittest.TestCase): diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index ef1fb71cc904..103bd4062292 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2342,16 +2342,22 @@ test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored)) Py_RETURN_NONE; } -/* Example passing NULLs to PyObject_Str(NULL). */ +static PyObject * +pyobject_repr_from_null(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return PyObject_Repr(NULL); +} static PyObject * -test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored)) +pyobject_str_from_null(PyObject *self, PyObject *Py_UNUSED(ignored)) { - PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); - PyObject *tuple = PyTuple_Pack(2, o1, o2); - Py_XDECREF(o1); - Py_XDECREF(o2); - return tuple; + return PyObject_Str(NULL); +} + +static PyObject * +pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return PyObject_Bytes(NULL); } static PyObject * @@ -5653,7 +5659,9 @@ static PyMethodDef TestMethods[] = { {"test_k_code", test_k_code, METH_NOARGS}, {"test_empty_argparse", test_empty_argparse, METH_NOARGS}, {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, - {"test_null_strings", test_null_strings, METH_NOARGS}, + {"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS}, + {"pyobject_str_from_null", pyobject_str_from_null, METH_NOARGS}, + {"pyobject_bytes_from_null", pyobject_bytes_from_null, METH_NOARGS}, {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, {"test_with_docstring", test_with_docstring, METH_NOARGS, PyDoc_STR("This is a pretty normal docstring.")},