[Python-checkins] bpo-44206: Make sure that dict-keys's version is set to zero when value is popped (GH-27542)

pablogsal webhook-mailer at python.org
Mon Aug 2 09:54:41 EDT 2021


https://github.com/python/cpython/commit/e06ae75e16dbf46b6626b6a41b62391ea1fdb319
commit: e06ae75e16dbf46b6626b6a41b62391ea1fdb319
branch: main
author: Mark Shannon <mark at hotpy.org>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-08-02T14:54:23+01:00
summary:

bpo-44206: Make sure that dict-keys's version is set to zero when value is popped (GH-27542)

files:
M Lib/test/test_module.py
M Objects/dictobject.c

diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py
index 6608dbc8a5a70..619348e0e40c0 100644
--- a/Lib/test/test_module.py
+++ b/Lib/test/test_module.py
@@ -332,6 +332,18 @@ def test_annotations_are_created_correctly(self):
         self.assertFalse("__annotations__" in ann_module4.__dict__)
 
 
+    def test_repeated_attribute_pops(self):
+        # Repeated accesses to module attribute will be specialized
+        # Check that popping the attribute doesn't break it
+        m = ModuleType("test")
+        d = m.__dict__
+        count = 0
+        for _ in range(100):
+            m.attr = 1
+            count += m.attr # Might be specialized
+            d.pop("attr")
+        self.assertEqual(count, 100)
+
     # frozen and namespace module reprs are tested in importlib.
 
 
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 7f1d38dd5f43f..5fb9d01561236 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1798,6 +1798,7 @@ _PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *d
     assert(old_value != NULL);
     mp->ma_used--;
     mp->ma_version_tag = DICT_NEXT_VERSION();
+    mp->ma_keys->dk_version = 0;
     dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
     ep = &DK_ENTRIES(mp->ma_keys)[ix];
     mp->ma_keys->dk_version = 0;



More information about the Python-checkins mailing list