Skip to content

Commit e06ae75

Browse files
authored
bpo-44206: Make sure that dict-keys's version is set to zero when value is popped (GH-27542)
1 parent 626d397 commit e06ae75

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Lib/test/test_module.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,18 @@ def test_annotations_are_created_correctly(self):
332332
self.assertFalse("__annotations__" in ann_module4.__dict__)
333333

334334

335+
def test_repeated_attribute_pops(self):
336+
# Repeated accesses to module attribute will be specialized
337+
# Check that popping the attribute doesn't break it
338+
m = ModuleType("test")
339+
d = m.__dict__
340+
count = 0
341+
for _ in range(100):
342+
m.attr = 1
343+
count += m.attr # Might be specialized
344+
d.pop("attr")
345+
self.assertEqual(count, 100)
346+
335347
# frozen and namespace module reprs are tested in importlib.
336348

337349

Objects/dictobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,7 @@ _PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *d
17981798
assert(old_value != NULL);
17991799
mp->ma_used--;
18001800
mp->ma_version_tag = DICT_NEXT_VERSION();
1801+
mp->ma_keys->dk_version = 0;
18011802
dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
18021803
ep = &DK_ENTRIES(mp->ma_keys)[ix];
18031804
mp->ma_keys->dk_version = 0;

0 commit comments

Comments
 (0)