gh-106168: Revert the "size before item" setting (#111683)

gh-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.
This commit is contained in:
scoder 2023-11-03 12:02:39 +01:00 committed by GitHub
parent d49aba5a7a
commit 24ddaee5ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 2 additions and 4 deletions

View File

@ -1049,8 +1049,6 @@ New Features
* If Python is built in :ref:`debug mode <debug-build>` or :option:`with * If Python is built in :ref:`debug mode <debug-build>` or :option:`with
assertions <--with-assertions>`, :c:func:`PyTuple_SET_ITEM` and assertions <--with-assertions>`, :c:func:`PyTuple_SET_ITEM` and
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion. :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`.) (Contributed by Victor Stinner in :gh:`106168`.)
* Add :c:func:`PyModule_Add` function: similar to * Add :c:func:`PyModule_Add` function: similar to

View File

@ -51,8 +51,8 @@ _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
Py_ssize_t allocated = self->allocated; Py_ssize_t allocated = self->allocated;
assert((size_t)len + 1 < PY_SSIZE_T_MAX); assert((size_t)len + 1 < PY_SSIZE_T_MAX);
if (allocated > len) { if (allocated > len) {
Py_SET_SIZE(self, len + 1);
PyList_SET_ITEM(self, len, newitem); PyList_SET_ITEM(self, len, newitem);
Py_SET_SIZE(self, len + 1);
return 0; return 0;
} }
return _PyList_AppendTakeRefListResize(self, newitem); return _PyList_AppendTakeRefListResize(self, newitem);

View File

@ -956,8 +956,8 @@ list_extend(PyListObject *self, PyObject *iterable)
if (Py_SIZE(self) < self->allocated) { if (Py_SIZE(self) < self->allocated) {
/* steals ref */ /* steals ref */
Py_ssize_t len = Py_SIZE(self); Py_ssize_t len = Py_SIZE(self);
Py_SET_SIZE(self, len + 1);
PyList_SET_ITEM(self, len, item); PyList_SET_ITEM(self, len, item);
Py_SET_SIZE(self, len + 1);
} }
else { else {
if (_PyList_AppendTakeRef(self, item) < 0) if (_PyList_AppendTakeRef(self, item) < 0)