Uh oh!
There was an error while loading. Please reload this page.
gh-146056: Fix repr() for lists and tuples containing NULLs - #146129
Conversation
vstinner
commented
Mar 18, 2026
If you change diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index 4845e0f3002..3fcfe9a41c9 100644
--- a/Doc/c-api/unicode.rst+++ b/Doc/c-api/unicode.rst@@ -1887,6 +1887,8 @@ object.
Call :c:func:`PyObject_Repr` on *obj* and write the output into *writer*.
+ If *obj* is ``NULL``, write ``<NULL>`` into *writer*.+
On success, return ``0``.
On error, set an exception, leave the writer unchanged, and return ``-1``.
diff --git a/Lib/test/test_capi/test_unicode.py b/Lib/test/test_capi/test_unicode.py
index 6a9c60f3a6d..55120448a8a 100644
--- a/Lib/test/test_capi/test_unicode.py+++ b/Lib/test/test_capi/test_unicode.py@@ -1779,6 +1779,13 @@ def test_basic(self):
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_utf8(self):
writer = self.create_writer(0)
writer.write_utf8(b"ascii", -1)
diff --git a/Modules/_testcapi/unicode.c b/Modules/_testcapi/unicode.c
index 203282dd53d..668adc5085b 100644
--- a/Modules/_testcapi/unicode.c+++ b/Modules/_testcapi/unicode.c@@ -449,6 +449,7 @@ writer_write_repr(PyObject *self_raw, PyObject *args)
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
+ NULLABLE(obj);
if (PyUnicodeWriter_WriteRepr(self->writer, obj) < 0) {
return NULL;
diff --git a/Objects/unicode_writer.c b/Objects/unicode_writer.c
index 2b944bf1ea8..cd2688e32df 100644
--- a/Objects/unicode_writer.c+++ b/Objects/unicode_writer.c@@ -383,6 +383,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);
} |
vstinner
commented
Mar 18, 2026
Please add a test on tuple as well: diff --git a/Lib/test/test_capi/test_tuple.py b/Lib/test/test_capi/test_tuple.py
index 0c27e81168f..51d26640865 100644
--- a/Lib/test/test_capi/test_tuple.py+++ b/Lib/test/test_capi/test_tuple.py@@ -73,6 +73,11 @@ def test_tuple_new(self):
self.assertRaises(SystemError, tuple_new, PY_SSIZE_T_MIN)
self.assertRaises(MemoryError, tuple_new, PY_SSIZE_T_MAX)
++ def test_uninitialized_tuple_repr(self):+ tup = _testlimitedcapi.tuple_new(3)+ self.assertEqual(repr(tup), '(<NULL>, <NULL>, <NULL>)')+
def test_tuple_fromarray(self):
# Test PyTuple_FromArray()
tuple_fromarray = _testcapi.tuple_fromarray |
Co-authored-by: Victor Stinner <vstinner@python.org>
99a95d7 to
a75c4c1Compare
vstinner
left a comment
There was a problem hiding this comment.
LGTM. I was against this before knowing that PyObject_Repr() and Python 3.12 repr(list) already support NULL, then I changed my mind.
IMO we should fix repr(list) in Python 3.13 and 3.14. But I don't think that we should backport the PyUnicodeWriter_WriteRepr() change.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka
commented
Mar 18, 2026
I think this was a bug in the PyUnicodeWriter C API. When the user rewrite they code to using it, they can reasonably assume that I have found few other bugs in the PyUnicodeWriter C API, working on them. |
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
pythonGH-146129) (cherry picked from commit 0f2246b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
GH-146155 is a backport of this pull request to the 3.14 branch. |
vstinner
commented
Mar 19, 2026
FYI I updated |
pythonGH-146129) (cherry picked from commit 0f2246b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
GH-146204 is a backport of this pull request to the 3.14 branch. |
1 similar comment
GH-146204 is a backport of this pull request to the 3.14 branch. |
GH-146155 is a backport of this pull request to the 3.14 branch. |
GH-146204 is a backport of this pull request to the 3.14 branch. |
…LLs (pythonGH-146129) (pythonGH-146155) (cherry picked from commit 0f2246b) (cherry picked from commit 7965133) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
…GH-146129) (pythonGH-146155) (cherry picked from commit 0f2246b) (cherry picked from commit 7965133) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
…onGH-146129) Co-authored-by: Victor Stinner <vstinner@python.org>
Uh oh!
There was an error while loading. Please reload this page.