Skip to content

Commit dc3a363

Browse files
committed
gh-158525: Avoid reacquiring dict lock when copying
1 parent e580c88 commit dc3a363

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid reacquiring the source dictionary lock when copying a sparse dictionary
2+
in the free-threaded build.

‎Objects/dictobject.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4575,7 +4575,14 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict)
45754575
}
45764576
if (copy == NULL)
45774577
return NULL;
4578-
if (dict_merge(copy, o, 1, NULL) < 0) {
4578+
int res;
4579+
if (!PyFrozenDict_Check(o) && Py_TYPE(mp)->tp_iter == dict_iter) {
4580+
res = dict_dict_merge((PyDictObject *)copy, mp, 1, NULL);
4581+
}
4582+
else {
4583+
res = dict_merge(copy, o, 1, NULL);
4584+
}
4585+
if (res < 0) {
45794586
Py_DECREF(copy);
45804587
return NULL;
45814588
}

0 commit comments

Comments
 (0)