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

[3.14] gh-146056: Fix repr() for lists and tuples containing NULLs (GH-146129)#146155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletionsDoc/c-api/file.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)

Expand Down
8 changes: 8 additions & 0 deletionsDoc/c-api/object.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.
Expand All@@ -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)


Expand All@@ -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.
Expand All@@ -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)

Expand Down
8 changes: 6 additions & 2 deletionsDoc/c-api/unicode.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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*.
Expand DownExpand Up@@ -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*.
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to not change PyUnicodeWriter_WriteRepr() in a Python 3.14.x bugfix release. Can you leave it unchanged? Instead, modify list_repr() and tuple_repr() to write<NULL> string explicitly.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

We need to modify not only list_repr() and tuple_repr(), but all code that usesPyUnicodeWriter_WriteRepr(). And what about the user code? We leave it with a mine which explodes in rare and most likely not tested case.

Copy link
Member

Choose a reason for hiding this comment

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

In the Python code base, if I read the code correctly, onlytuple_repr() can callPyUnicodeWriter_WriteRepr() withNULL. For example,list_repr() cannot passNULL, becauseitem = Py_NewRef(v->ob_item[i]) crash if the item isNULL.

And what about the user code? We leave it with a mine which explodes in rare and most likely not tested case.

My concern is that if you develop on (the future) Python 3.14.4 withPyUnicodeWriter_WriteRepr() which acceptsNULL, you can get crashes if an user uses Python 3.14.0 which doesn't acceptNULL. I don't think that it's a good idea to change the API in a 3.14.x bugfix release.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Python 3.14.0 is broken. It crashes when you callrepr() for uninitialized list or tuple, and it can crash in other cases. Does it mean that we should not backport the bugfix? I think that it is the nature of bugfix releases that they make the code which failed in earlier releases to pass.

The documentation says thatPyUnicodeWriter_WriteRepr() callsPyObject_Repr() onobj and writes the output intowriter.PyObject_Repr() works with NULL.

Copy link
Member

Choose a reason for hiding this comment

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

If you insist to change PyUnicodeWriter_WriteRepr() API, please add aversionchanged markup to document that the API changed.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

If we don't fix it in 3.14, we will need a versionchanged for 3.15. But we rarely add versionchanged for bugfixes, especially if the feature was added in the same version.

Copy link
Member

Choose a reason for hiding this comment

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

I'm suggesting to add aversionchanged markup in the 3.14 change to say that the API changed in Python 3.14.x.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Done. Created also a PR for adding in in main.


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*.
Expand Down
4 changes: 4 additions & 0 deletionsLib/test/test_capi/test_list.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -350,6 +350,10 @@ def test_list_extend(self):
# CRASHES list_extend(NULL, [])
# CRASHES list_extend([], NULL)

deftest_uninitialized_list_repr(self):
lst=_testlimitedcapi.list_new(3)
self.assertEqual(repr(lst),'[<NULL>, <NULL>, <NULL>]')


if__name__=="__main__":
unittest.main()
4 changes: 4 additions & 0 deletionsLib/test/test_capi/test_tuple.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -292,6 +292,10 @@ def my_iter():
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()
7 changes: 7 additions & 0 deletionsLib/test/test_capi/test_unicode.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1767,6 +1767,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_write_char(self):
writer = self.create_writer(0)
writer.write_char(0)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
:c:func:`PyUnicodeWriter_WriteRepr` now supports ``NULL`` argument.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Fix :func:`repr` for lists and tuples containing ``NULL``\ s.
2 changes: 1 addition & 1 deletionObjects/listobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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) {
Expand Down
4 changes: 4 additions & 0 deletionsObjects/unicodeobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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);
}
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp