Skip to content

Commit 64cfb01

Browse files
authored
Merge branch 'main' into main
2 parents 307fbf1 + becd7a9 commit 64cfb01

File tree

14 files changed

+44
-6
lines changed

14 files changed

+44
-6
lines changed

Doc/c-api/file.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ the :mod:`io` APIs instead.
123123
124124
Write object *obj* to file object *p*. The only supported flag for *flags* is
125125
:c:macro:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
126-
instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the
127-
appropriate exception will be set.
126+
instead of the :func:`repr`.
127+
128+
If *obj* is ``NULL``, write the string ``"<NULL>"``.
128129
130+
Return ``0`` on success or ``-1`` on failure; the
131+
appropriate exception will be set.
129132
130133
.. c:function:: int PyFile_WriteString(const char *s, PyObject *p)
131134

Doc/c-api/object.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ Object Protocol
363363
representation on success, ``NULL`` on failure. This is the equivalent of the
364364
Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
365365
366+
If argument is ``NULL``, return the string ``'<NULL>'``.
367+
366368
.. versionchanged:: 3.4
367369
This function now includes a debug assertion to help ensure that it
368370
does not silently discard an active exception.
@@ -377,6 +379,8 @@ Object Protocol
377379
a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
378380
Called by the :func:`ascii` built-in function.
379381
382+
If argument is ``NULL``, return the string ``'<NULL>'``.
383+
380384
.. index:: string; PyObject_Str (C function)
381385
382386
@@ -387,6 +391,8 @@ Object Protocol
387391
Python expression ``str(o)``. Called by the :func:`str` built-in function
388392
and, therefore, by the :func:`print` function.
389393
394+
If argument is ``NULL``, return the string ``'<NULL>'``.
395+
390396
.. versionchanged:: 3.4
391397
This function now includes a debug assertion to help ensure that it
392398
does not silently discard an active exception.
@@ -402,6 +408,8 @@ Object Protocol
402408
a TypeError is raised when *o* is an integer instead of a zero-initialized
403409
bytes object.
404410
411+
If argument is ``NULL``, return the :class:`bytes` object ``b'<NULL>'``.
412+
405413
406414
.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
407415

Doc/c-api/unicode.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ object.
18671867
On success, return ``0``.
18681868
On error, set an exception, leave the writer unchanged, and return ``-1``.
18691869
1870-
.. c:function:: int PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *writer, Py_UCS4 *str, Py_ssize_t size)
1870+
.. c:function:: int PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *writer, const Py_UCS4 *str, Py_ssize_t size)
18711871
18721872
Writer the UCS4 string *str* into *writer*.
18731873
@@ -1887,6 +1887,8 @@ object.
18871887
18881888
Call :c:func:`PyObject_Repr` on *obj* and write the output into *writer*.
18891889
1890+
If *obj* is ``NULL``, write the string ``"<NULL>"`` into *writer*.
1891+
18901892
On success, return ``0``.
18911893
On error, set an exception, leave the writer unchanged, and return ``-1``.
18921894

Include/cpython/unicodeobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ PyAPI_FUNC(int) PyUnicodeWriter_WriteWideChar(
496496
Py_ssize_t size);
497497
PyAPI_FUNC(int) PyUnicodeWriter_WriteUCS4(
498498
PyUnicodeWriter *writer,
499-
Py_UCS4 *str,
499+
const Py_UCS4 *str,
500500
Py_ssize_t size);
501501

502502
PyAPI_FUNC(int) PyUnicodeWriter_WriteStr(

Lib/test/test_capi/test_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ def test_list_extend(self):
350350
# CRASHES list_extend(NULL, [])
351351
# CRASHES list_extend([], NULL)
352352

353+
def test_uninitialized_list_repr(self):
354+
lst = _testlimitedcapi.list_new(3)
355+
self.assertEqual(repr(lst), '[<NULL>, <NULL>, <NULL>]')
356+
353357

354358
if __name__ == "__main__":
355359
unittest.main()

Lib/test/test_capi/test_tuple.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def test_tuple_new(self):
7373
self.assertRaises(SystemError, tuple_new, PY_SSIZE_T_MIN)
7474
self.assertRaises(MemoryError, tuple_new, PY_SSIZE_T_MAX)
7575

76+
7677
def test_tuple_fromarray(self):
7778
# Test PyTuple_FromArray()
7879
tuple_fromarray = _testcapi.tuple_fromarray
@@ -357,6 +358,10 @@ def my_iter():
357358
self.assertEqual(tuple(my_iter()), (TAG, *range(10)))
358359
self.assertEqual(tuples, [])
359360

361+
def test_uninitialized_tuple_repr(self):
362+
tup = _testlimitedcapi.tuple_new(3)
363+
self.assertEqual(repr(tup), '(<NULL>, <NULL>, <NULL>)')
364+
360365

361366
if __name__ == "__main__":
362367
unittest.main()

Lib/test/test_capi/test_unicode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,13 @@ def test_basic(self):
17791779
self.assertEqual(writer.finish(),
17801780
"var=long value 'repr'")
17811781

1782+
def test_repr_null(self):
1783+
writer = self.create_writer(0)
1784+
writer.write_utf8(b'var=', -1)
1785+
writer.write_repr(NULL)
1786+
self.assertEqual(writer.finish(),
1787+
"var=<NULL>")
1788+
17821789
def test_utf8(self):
17831790
writer = self.create_writer(0)
17841791
writer.write_utf8(b"ascii", -1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:c:func:`PyUnicodeWriter_WriteRepr` now supports ``NULL`` argument.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:c:func:`PyUnicodeWriter_WriteUCS4` now accepts a pointer to a constant buffer
2+
of ``Py_UCS4``.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :meth:`!list.__repr__` for lists containing ``NULL``\ s.

0 commit comments

Comments
 (0)