]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112087: Make `list.extend(dict)` behave atomically (#117438)
authorSam Gross <colesbury@gmail.com>
Tue, 2 Apr 2024 14:45:00 +0000 (10:45 -0400)
committerGitHub <noreply@github.com>
Tue, 2 Apr 2024 14:45:00 +0000 (10:45 -0400)
Add a special case for `list.extend(dict)` and `list(dict)` so that those
patterns behave atomically with respect to modifications to the list or
dictionary.

This is required by multiprocessing, which assumes that
`list(_finalizer_registry)` is atomic.

Objects/listobject.c

index 470ad8eb8135db1677e676f4802c9fe70279bf68..472c471d9968a46860b1afabacf8e9e459e4f0f3 100644 (file)
@@ -1374,6 +1374,11 @@ _list_extend(PyListObject *self, PyObject *iterable)
         res = list_extend_set(self, (PySetObject *)iterable);
         Py_END_CRITICAL_SECTION2();
     }
+    else if (PyDict_CheckExact(iterable)) {
+        Py_BEGIN_CRITICAL_SECTION2(self, iterable);
+        res = list_extend_dict(self, (PyDictObject *)iterable, 0 /*keys*/);
+        Py_END_CRITICAL_SECTION2();
+    }
     else if (Py_IS_TYPE(iterable, &PyDictKeys_Type)) {
         PyDictObject *dict = ((_PyDictViewObject *)iterable)->dv_dict;
         Py_BEGIN_CRITICAL_SECTION2(self, dict);