]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-146056: Fix repr() for lists and tuples containing NULLs (GH-146129) (GH...
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 22 Mar 2026 07:25:02 +0000 (09:25 +0200)
committerGitHub <noreply@github.com>
Sun, 22 Mar 2026 07:25:02 +0000 (09:25 +0200)
(cherry picked from commit 0f2246b1553f401da5ade47e0fd1c80ad7a8dfa5)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Doc/c-api/file.rst
Doc/c-api/object.rst
Doc/c-api/unicode.rst
Lib/test/test_capi/test_list.py
Lib/test/test_capi/test_tuple.py
Lib/test/test_capi/test_unicode.py
Misc/NEWS.d/next/C_API/2026-03-18-20-18-59.gh-issue-146056.nnZIgp.rst [new file with mode: 0644]
Misc/NEWS.d/next/Core_and_Builtins/2026-03-18-18-52-00.gh-issue-146056.r1tVSo.rst [new file with mode: 0644]
Objects/listobject.c
Objects/unicodeobject.c

index 0580e4c8f79db082aae57b785d8609b885683227..d89072ab24e241d9eebc40d2f8096e1f61bd33f0 100644 (file)
@@ -123,9 +123,12 @@ the :mod:`io` APIs instead.
 
    Write object *obj* to file object *p*.  The only supported flag for *flags* is
    :c:macro:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
-   instead of the :func:`repr`.  Return ``0`` on success or ``-1`` on failure; the
-   appropriate exception will be set.
+   instead of the :func:`repr`.
+
+   If *obj* is ``NULL``, write the string ``"<NULL>"``.
 
+   Return ``0`` on success or ``-1`` on failure; the
+   appropriate exception will be set.
 
 .. c:function:: int PyFile_WriteString(const char *s, PyObject *p)
 
index 228b3c8aa308440985cc1fb8000cbe2e96228fc5..44e1220d109d051716c6572062c527498fc12c3e 100644 (file)
@@ -319,6 +319,8 @@ Object Protocol
    representation on success, ``NULL`` on failure.  This is the equivalent of the
    Python expression ``repr(o)``.  Called by the :func:`repr` built-in function.
 
+   If argument is ``NULL``, return the string ``'<NULL>'``.
+
    .. versionchanged:: 3.4
       This function now includes a debug assertion to help ensure that it
       does not silently discard an active exception.
@@ -333,6 +335,8 @@ Object Protocol
    a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
    Called by the :func:`ascii` built-in function.
 
+   If argument is ``NULL``, return the string ``'<NULL>'``.
+
    .. index:: string; PyObject_Str (C function)
 
 
@@ -343,6 +347,8 @@ Object Protocol
    Python expression ``str(o)``.  Called by the :func:`str` built-in function
    and, therefore, by the :func:`print` function.
 
+   If argument is ``NULL``, return the string ``'<NULL>'``.
+
    .. versionchanged:: 3.4
       This function now includes a debug assertion to help ensure that it
       does not silently discard an active exception.
@@ -358,6 +364,8 @@ Object Protocol
    a TypeError is raised when *o* is an integer instead of a zero-initialized
    bytes object.
 
+   If argument is ``NULL``, return the :class:`bytes` object ``b'<NULL>'``.
+
 
 .. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
 
index b7d3aaf3227bf0d998f2224f559a189e0c925414..53a13e80944c70382d7fd05023cc4caff4d324b8 100644 (file)
@@ -1842,8 +1842,6 @@ object.
    On success, return ``0``.
    On error, set an exception, leave the writer unchanged, and return ``-1``.
 
-   .. versionadded:: 3.14
-
 .. c:function:: int PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer, const wchar_t *str, Py_ssize_t size)
 
    Write the wide string *str* into *writer*.
@@ -1874,9 +1872,15 @@ object.
 
    Call :c:func:`PyObject_Repr` on *obj* and write the output into *writer*.
 
+   If *obj* is ``NULL``, write the string ``"<NULL>"`` into *writer*.
+
    On success, return ``0``.
    On error, set an exception, leave the writer unchanged, and return ``-1``.
 
+   .. versionchanged:: 3.14.4
+
+      Added support for ``NULL``.
+
 .. c:function:: int PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str, Py_ssize_t start, Py_ssize_t end)
 
    Write the substring ``str[start:end]`` into *writer*.
index 67ed5d0b4f8722be61ab0bb55dae23968738c708..b95b8ba960bd8b27a95706c7eba8062faefd0948 100644 (file)
@@ -350,6 +350,10 @@ class CAPITest(unittest.TestCase):
         # CRASHES list_extend(NULL, [])
         # CRASHES list_extend([], NULL)
 
+    def test_uninitialized_list_repr(self):
+        lst = _testlimitedcapi.list_new(3)
+        self.assertEqual(repr(lst), '[<NULL>, <NULL>, <NULL>]')
+
 
 if __name__ == "__main__":
     unittest.main()
index be6c91558312066b93902e9870eb72cbcd510b44..3683c008dbcaaf58bcefebc238c22064e48030ce 100644 (file)
@@ -292,6 +292,10 @@ class CAPITest(unittest.TestCase):
         self.assertEqual(tuple(my_iter()), (TAG, *range(10)))
         self.assertEqual(tuples, [])
 
+    def test_uninitialized_tuple_repr(self):
+        tup = _testlimitedcapi.tuple_new(3)
+        self.assertEqual(repr(tup), '(<NULL>, <NULL>, <NULL>)')
+
 
 if __name__ == "__main__":
     unittest.main()
index be19146cc657c8556d1dde3f75a0e3097299eeb6..7e5f4c9dac94f9bf52c32321b862e8b9c7b7833b 100644 (file)
@@ -1767,6 +1767,13 @@ class PyUnicodeWriterTest(unittest.TestCase):
         self.assertEqual(writer.finish(),
                          "var=long value 'repr'")
 
+    def test_repr_null(self):
+        writer = self.create_writer(0)
+        writer.write_utf8(b'var=', -1)
+        writer.write_repr(NULL)
+        self.assertEqual(writer.finish(),
+                         "var=<NULL>")
+
     def test_write_char(self):
         writer = self.create_writer(0)
         writer.write_char(0)
diff --git a/Misc/NEWS.d/next/C_API/2026-03-18-20-18-59.gh-issue-146056.nnZIgp.rst b/Misc/NEWS.d/next/C_API/2026-03-18-20-18-59.gh-issue-146056.nnZIgp.rst
new file mode 100644 (file)
index 0000000..7c5fc7a
--- /dev/null
@@ -0,0 +1 @@
+:c:func:`PyUnicodeWriter_WriteRepr` now supports ``NULL`` argument.
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-18-18-52-00.gh-issue-146056.r1tVSo.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-18-18-52-00.gh-issue-146056.r1tVSo.rst
new file mode 100644 (file)
index 0000000..ab6eab2
--- /dev/null
@@ -0,0 +1 @@
+Fix :func:`repr` for lists and tuples containing ``NULL``\ s.
index 98c90665be5721aa03847240488a9abc3040e5fb..6407d0ac5833fe01433b5b9e3f2199d2df0e6bf9 100644 (file)
@@ -595,7 +595,7 @@ list_repr_impl(PyListObject *v)
        so must refetch the list size on each iteration. */
     for (Py_ssize_t i = 0; i < Py_SIZE(v); ++i) {
         /* Hold a strong reference since repr(item) can mutate the list */
-        item = Py_NewRef(v->ob_item[i]);
+        item = Py_XNewRef(v->ob_item[i]);
 
         if (i > 0) {
             if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
index 4a457c4ac9ff3bf77421bb470d75cf0f1022fcfb..aef89c15b30a21b8d3e06c37f2286056f1c048e6 100644 (file)
@@ -13976,6 +13976,10 @@ PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
 int
 PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)
 {
+    if (obj == NULL) {
+        return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter*)writer, "<NULL>", 6);
+    }
+
     if (Py_TYPE(obj) == &PyLong_Type) {
         return _PyLong_FormatWriter((_PyUnicodeWriter*)writer, obj, 10, 0);
     }