Skip to content

Commit 04e06e5

Browse files
committed
gh-161524: Notify dict watchers after preparing copied keys
1 parent 56199e0 commit 04e06e5

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

‎Lib/test/test_capi/test_watchers.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ def test_clone(self):
6767
self.assert_events(["clone"])
6868
self.assertEqual(d, d2)
6969

70+
def test_no_clone_event_on_allocation_failure(self):
71+
source = {1: 1, 2: 2}
72+
del source[1]
73+
d = {}
74+
with self.watcher() as wid:
75+
self.watch(wid, d)
76+
with self.assertRaises(MemoryError):
77+
_testcapi.call_with_nomemory(0, 1, d.update, source)
78+
self.assert_events([])
79+
self.assertEqual(d, {})
80+
7081
def test_no_event_if_not_watched(self):
7182
d = {}
7283
with self.watcher() as wid:

‎Lib/test/test_dict.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def __hash__(self):
479479
with self.subTest(key_type=type(keys[0])):
480480
d = dict(zip(keys, range(100)))
481481
for i, key in enumerate(keys):
482-
if i not in (1, 50, 98):
482+
if i not in (1, 51, 98):
483483
del d[key]
484484
expected = list(d.items())
485485
if isinstance(keys[0], Key):

‎Objects/dictobject.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4297,7 +4297,7 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
42974297
}
42984298

42994299

4300-
/* Clone only live entries from a combined table, preserving insertion order.
4300+
/* Copy only live entries from a combined table, preserving insertion order.
43014301
The source is locked while its keys and values are referenced. */
43024302
static PyDictKeysObject *
43034303
dict_copy_keys(PyDictObject *orig)
@@ -4369,9 +4369,8 @@ dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **
43694369
override = 1;
43704370
PyDictKeysObject *okeys = other->ma_keys;
43714371

4372-
// Clone a combined table directly, compacting deleted entries.
4372+
// Copy a combined table directly, compacting deleted entries.
43734373
if (mp->ma_values == NULL && other->ma_values == NULL) {
4374-
_PyDict_NotifyEvent(PyDict_EVENT_CLONED, mp, (PyObject *)other, NULL);
43754374
PyDictKeysObject *keys;
43764375
if (other->ma_used == okeys->dk_nentries &&
43774376
(DK_LOG_SIZE(okeys) == PyDict_LOG_MINSIZE ||
@@ -4384,10 +4383,11 @@ dict_dict_merge(PyDictObject *mp, PyDictObject *other, int override, PyObject **
43844383
if (keys == NULL)
43854384
return -1;
43864385

4386+
_PyDict_NotifyEvent(PyDict_EVENT_CLONED, mp, (PyObject *)other, NULL);
43874387
ensure_shared_on_resize(mp);
43884388
dictkeys_decref(mp->ma_keys, IS_DICT_SHARED(mp));
43894389
set_keys(mp, keys);
4390-
STORE_USED(mp, other->ma_used);
4390+
STORE_USED(mp, keys->dk_nentries);
43914391
ASSERT_CONSISTENT(mp);
43924392
return 0;
43934393
}

0 commit comments

Comments
 (0)