GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146056: Fix repr() for lists and tuples containing NULLs by serhiy-storchaka · Pull Request #146129 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Mar 18, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: Crash in repr() for lists containing NULLs #146056

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
pythongh-146056: Fix list.__repr__() for lists containing NULLs
5a1e0ad
@serhiy-storchaka
serhiy-storchaka requested a review from vstinner March 18, 2026 16:54
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Mar 18, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Mar 18, 2026
Crash in repr() for lists containing NULLs #146056
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 18, 2026
@vstinner

vstinner commented Mar 18, 2026

Copy link
Copy Markdown
Member

If you change PyUnicodeWriter_WriteRepr() to accept NULL, please apply this change to update the doc and tests:

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);
     }
serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Mar 18, 2026

Copy link
Copy Markdown
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.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
serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka requested a review from ZeroIntensity as a code owner March 18, 2026 18:42
@serhiy-storchaka @vstinner
Add more tests and update docs.
a75c4c1
Co-authored-by: Victor Stinner <[email protected]>
@serhiy-storchaka
serhiy-storchaka force-pushed the list-repr-null branch from 99a95d7 to a75c4c1 Compare March 18, 2026 18:43
vstinner
vstinner approved these changes Mar 18, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/c-api/file.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/object.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/object.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/object.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/object.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/unicode.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Mar 18, 2026
@serhiy-storchaka @vstinner
Apply suggestions from code review
9190ea4
Co-authored-by: Victor Stinner <[email protected]>
@serhiy-storchaka

serhiy-storchaka commented Mar 18, 2026

Copy link
Copy Markdown
Member Author

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 PyUnicodeWriter_WriteRepr() is equivalent to PyUnicodeWriter_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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka merged commit 0f2246b into python:main Mar 19, 2026
50 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka deleted the list-repr-null branch March 19, 2026 07:59
@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 19, 2026
@miss-islington-app

miss-islington-app Bot commented Mar 19, 2026

Copy link
Copy Markdown

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!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Mar 19, 2026

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 0f2246b1553f401da5ade47e0fd1c80ad7a8dfa5 3.14
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned serhiy-storchaka Mar 19, 2026
@miss-islington-app

miss-islington-app Bot commented Mar 19, 2026

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 0f2246b1553f401da5ade47e0fd1c80ad7a8dfa5 3.13
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Mar 19, 2026
@serhiy-storchaka @vstinner
[3.14] pythongh-146056: Fix list.__repr__() for lists containing NULLs (
3af9bbb
pythonGH-146129)

(cherry picked from commit 0f2246b)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@bedevere-app

bedevere-app Bot commented Mar 19, 2026

Copy link
Copy Markdown

GH-146155 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Mar 19, 2026
@serhiy-storchaka serhiy-storchaka removed the needs backport to 3.13 only security fixes label Mar 19, 2026
@vstinner vstinner mentioned this pull request Mar 19, 2026
gh-146056: Fix TreeBuilder stack in xml.etree #146062
Closed
@vstinner

vstinner commented Mar 19, 2026

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Mar 20, 2026
@serhiy-storchaka @vstinner
[3.14] pythongh-146056: Fix list.__repr__() for lists containing NULLs (
fec7f55
pythonGH-146129)

(cherry picked from commit 0f2246b)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@bedevere-app

bedevere-app Bot commented Mar 20, 2026

Copy link
Copy Markdown

GH-146204 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

1 similar comment
@bedevere-app

bedevere-app Bot commented Mar 20, 2026

Copy link
Copy Markdown

GH-146204 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Mar 20, 2026

Copy link
Copy Markdown

GH-146155 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Mar 20, 2026

Copy link
Copy Markdown

GH-146204 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka changed the title gh-146056: Fix list.__repr__() for lists containing NULLs gh-146056: Fix repr() for lists and tuples containing NULLs Mar 20, 2026
serhiy-storchaka added a commit that referenced this pull request Mar 22, 2026
@serhiy-storchaka @vstinner
[3.14] gh-146056: Fix repr() for lists and tuples containing NULLs (G…
7965133
…H-146129) (GH-146155)

(cherry picked from commit 0f2246b)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Mar 22, 2026
@serhiy-storchaka @vstinner
[3.13] pythongh-146056: Fix repr() for lists and tuples containing NU…
94d687d
…LLs (pythonGH-146129) (pythonGH-146155)

(cherry picked from commit 0f2246b)
(cherry picked from commit 7965133)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Mar 22, 2026
@serhiy-storchaka @vstinner
[3.13] pythongh-146056: Fix repr() for lists containing NULLs (python…
bb5d229
…GH-146129) (pythonGH-146155)

(cherry picked from commit 0f2246b)
(cherry picked from commit 7965133)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
serhiy-storchaka added a commit that referenced this pull request Mar 22, 2026
@serhiy-storchaka @vstinner
[3.13] gh-146056: Fix repr() for lists containing NULLs (GH-146129) (G…
fab8267
…H-146155) (GH-146271)

(cherry picked from commit 0f2246b)
(cherry picked from commit 7965133)

Co-authored-by: Victor Stinner <[email protected]>
@pablogsal pablogsal mentioned this pull request Apr 4, 2026
gh-130273: Fix traceback color output with unicode characters #142529
Merged
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@serhiy-storchaka @vstinner @ljfp
pythongh-146056: Fix list.__repr__() for lists containing NULLs (pyth…
e573679
…onGH-146129)

Co-authored-by: Victor Stinner <[email protected]>
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@serhiy-storchaka @vstinner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.