Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,20 @@ def test_copy(self):
self.assertEqual(d2, frozendict(x=1, y=2))
self.assertEqual(type(d2), frozendict)

def test_copy_iter_override(self):
class CustomReversedDict(frozendict):
def keys(self):
return reversed(list(frozendict.keys(self)))

__iter__ = keys

pairs = [('a', 1), ('b', 2), ('c', 3)]
d = CustomReversedDict(pairs)
d2 = d.copy()
self.assertIsNot(d2, d)
self.assertEqual(type(d2), frozendict)
self.assertEqual(list(d2.items()), pairs[::-1])

def test_merge(self):
# test "a | b" operator
self.assertEqual(frozendict(x=1) | frozendict(y=2),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix slow ``dict.copy()`` for dicts with many deleted entries in the free-threaded build.
13 changes: 11 additions & 2 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4209,7 +4209,9 @@ static int
dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **dupkey)
{
assert(can_modify_dict(mp));
ASSERT_DICT_LOCKED(other);
if (!PyFrozenDict_Check(other)) {
ASSERT_DICT_LOCKED(other);
}

if (other == mp || other->ma_used == 0)
/* a.update(a) or a.update({}); nothing to do */
Expand Down Expand Up @@ -4575,7 +4577,14 @@ copy_lock_held_untracked(PyObject *o, int as_frozendict)
}
if (copy == NULL)
return NULL;
if (dict_merge(copy, o, 1, NULL) < 0) {
int res;
if (Py_TYPE(mp)->tp_iter == dict_iter) {
res = dict_dict_merge((PyDictObject *)copy, mp, 1, NULL);
}
else {
res = dict_merge(copy, o, 1, NULL);
}
if (res < 0) {
Py_DECREF(copy);
return NULL;
}
Expand Down
Loading