]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106168: Revert the "size before item" setting (#111683)
authorscoder <stefan_ml@behnel.de>
Fri, 3 Nov 2023 11:02:39 +0000 (12:02 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Nov 2023 11:02:39 +0000 (11:02 +0000)
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.

Doc/whatsnew/3.13.rst
Include/internal/pycore_list.h
Objects/listobject.c

index aa693c6f6d55cd806b7aeb6c5a1a04c21bf31451..f2c14415febab995f836604662f38aa27ddf9c97 100644 (file)
@@ -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
index 056be2c80c8ce6f4dc5753e66f72543151778dcc..55d67b32bc8a63ef7a1870dbe6fe53995dbcea9c 100644 (file)
@@ -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);
index ad1840b01226ece593e25b8a24676d9df9304da3..af006ef0f6185044f7139c08502f271148cbc99e 100644 (file)
@@ -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)