Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-146056: Fix repr() for lists and tuples containing NULLs#146129

Merged
serhiy-storchaka merged 3 commits intopython:mainfrom
serhiy-storchaka:list-repr-null
Mar 19, 2026
Merged

gh-146056: Fix repr() for lists and tuples containing NULLs#146129
serhiy-storchaka merged 3 commits intopython:mainfrom
serhiy-storchaka:list-repr-null

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commentedMar 18, 2026
edited by bedevere-appbot
Loading

@vstinner
Copy link
Member

If you changePyUnicodeWriter_WriteRepr() to acceptNULL, please apply this change to update the doc and tests:

diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rstindex 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.pyindex 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.cindex 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.cindex 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);     }
serhiy-storchaka reacted with thumbs up emoji

@vstinner
Copy link
Member

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.pyindex 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
serhiy-storchaka reacted with thumbs up emoji

Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM. I was against this before knowing that PyObject_Repr() and Python 3.12repr(list) already supportNULL, then I changed my mind.

IMO we should fixrepr(list) in Python 3.13 and 3.14. But I don't think that we should backport thePyUnicodeWriter_WriteRepr() change.

Co-authored-by: Victor Stinner <vstinner@python.org>
@serhiy-storchaka
Copy link
MemberAuthor

I think this was a bug in the PyUnicodeWriter C API. When the user rewrite they code to using it, they can reasonably assume thatPyUnicodeWriter_WriteRepr() is equivalent toPyUnicodeWriter_WriteStr(PyObject_Repr()). This is a mine. It is better to fix it as fast as possible.

I have found few other bugs in the PyUnicodeWriter C API, working on them.

@serhiy-storchakaserhiy-storchaka merged commit0f2246b intopython:mainMar 19, 2026
50 checks passed
@serhiy-storchakaserhiy-storchaka deleted the list-repr-null branchMarch 19, 2026 07:59
@miss-islington-app
Copy link

Thanks@serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Sorry,@serhiy-storchaka, I could not cleanly backport this to3.14 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 0f2246b1553f401da5ade47e0fd1c80ad7a8dfa5 3.14

@miss-islington-app
Copy link

Sorry,@serhiy-storchaka, I could not cleanly backport this to3.13 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 0f2246b1553f401da5ade47e0fd1c80ad7a8dfa5 3.13

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull requestMar 19, 2026
pythonGH-146129)(cherry picked from commit0f2246b)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app
Copy link

GH-146155 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelMar 19, 2026
@serhiy-storchakaserhiy-storchaka removed the needs backport to 3.13bugs and security fixes labelMar 19, 2026
@vstinner
Copy link
Member

FYI I updatedPyUnicodeWriter_WriteRepr() in pythoncapi-compat (backport):python/pythoncapi-compat#170.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull requestMar 20, 2026
pythonGH-146129)(cherry picked from commit0f2246b)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app
Copy link

GH-146204 is a backport of this pull request to the3.14 branch.

1 similar comment
@bedevere-app
Copy link

GH-146204 is a backport of this pull request to the3.14 branch.

@bedevere-app
Copy link

GH-146155 is a backport of this pull request to the3.14 branch.

@bedevere-app
Copy link

GH-146204 is a backport of this pull request to the3.14 branch.

@serhiy-storchakaserhiy-storchaka changed the titlegh-146056: Fix list.__repr__() for lists containing NULLsgh-146056: Fix repr() for lists and tuples containing NULLsMar 20, 2026
serhiy-storchaka added a commit that referenced this pull requestMar 22, 2026
…H-146129) (GH-146155)(cherry picked from commit0f2246b)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull requestMar 22, 2026
…LLs (pythonGH-146129) (pythonGH-146155)(cherry picked from commit0f2246b)(cherry picked from commit7965133)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull requestMar 22, 2026
…GH-146129) (pythonGH-146155)(cherry picked from commit0f2246b)(cherry picked from commit7965133)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka added a commit that referenced this pull requestMar 22, 2026
…H-146155) (GH-146271)(cherry picked from commit0f2246b)(cherry picked from commit7965133)Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@vstinnervstinnervstinner approved these changes

@ZeroIntensityZeroIntensityAwaiting requested review from ZeroIntensityZeroIntensity is a code owner

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@serhiy-storchaka@vstinner

[8]ページ先頭

©2009-2026 Movatter.jp