]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44206: Make sure that dict-keys's version is set to zero when value is popped...
authorMark Shannon <mark@hotpy.org>
Mon, 2 Aug 2021 13:54:23 +0000 (14:54 +0100)
committerGitHub <noreply@github.com>
Mon, 2 Aug 2021 13:54:23 +0000 (14:54 +0100)
Lib/test/test_module.py
Objects/dictobject.c

index 6608dbc8a5a70b1b747ba58eb2fc544f6bdedc8a..619348e0e40c03b92bb4948226247d3a95b20d74 100644 (file)
@@ -332,6 +332,18 @@ a = A(destroyed)"""
         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.
 
 
index 7f1d38dd5f43f0f312a9a4801668400d0264b0b8..5fb9d015612363da55b3907d652c9bca574743be 100644 (file)
@@ -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;