Skip to content

Commit 5a1e0ad

Browse files
gh-146056: Fix list.__repr__() for lists containing NULLs
1 parent 70c7e04 commit 5a1e0ad

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

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_incomplete_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()
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.

Objects/listobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ list_repr_impl(PyListObject *v)
601601
so must refetch the list size on each iteration. */
602602
for (Py_ssize_t i = 0; i < Py_SIZE(v); ++i) {
603603
/* Hold a strong reference since repr(item) can mutate the list */
604-
item = Py_NewRef(v->ob_item[i]);
604+
item = Py_XNewRef(v->ob_item[i]);
605605

606606
if (i > 0) {
607607
if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {

Objects/unicode_writer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
383383
int
384384
PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)
385385
{
386+
if (obj == NULL) {
387+
return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter*)writer, "<NULL>", 6);
388+
}
389+
386390
if (Py_TYPE(obj) == &PyLong_Type) {
387391
return _PyLong_FormatWriter((_PyUnicodeWriter*)writer, obj, 10, 0);
388392
}

0 commit comments

Comments
 (0)