Skip to content

Commit

Permalink
pythongh-106168: Revert the "size before item" setting (python#111683)
Browse files Browse the repository at this point in the history
pythongh-106168: Update the size only after setting the item, to avoid temporary inconsistencies.
Also remove the "what's new" sentence regarding the size setting since tuples cannot grow after allocation.
  • Loading branch information
scoder authored and aisk committed Feb 11, 2024
1 parent a248d0d commit 1193f1d
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Doc/whatsnew/3.13.rst
Expand Up @@ -1049,8 +1049,6 @@ New Features
* If Python is built in :ref:`debug mode <debug-build>` or :option:`with
assertions <--with-assertions>`, :c:func:`PyTuple_SET_ITEM` and
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion.
If the assertion fails in :c:func:`PyTuple_SET_ITEM`, make sure that the
tuple size is set before.
(Contributed by Victor Stinner in :gh:`106168`.)

* Add :c:func:`PyModule_Add` function: similar to
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_list.h
Expand Up @@ -51,8 +51,8 @@ _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
Py_ssize_t allocated = self->allocated;
assert((size_t)len + 1 < PY_SSIZE_T_MAX);
if (allocated > len) {
Py_SET_SIZE(self, len + 1);
PyList_SET_ITEM(self, len, newitem);
Py_SET_SIZE(self, len + 1);
return 0;
}
return _PyList_AppendTakeRefListResize(self, newitem);
Expand Down
2 changes: 1 addition & 1 deletion Objects/listobject.c
Expand Up @@ -956,8 +956,8 @@ list_extend(PyListObject *self, PyObject *iterable)
if (Py_SIZE(self) < self->allocated) {
/* steals ref */
Py_ssize_t len = Py_SIZE(self);
Py_SET_SIZE(self, len + 1);
PyList_SET_ITEM(self, len, item);
Py_SET_SIZE(self, len + 1);
}
else {
if (_PyList_AppendTakeRef(self, item) < 0)
Expand Down

0 comments on commit 1193f1d

Please sign in to comment.