aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_dict.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_dict.py')
-rw-r--r--Lib/test/test_dict.py36
1 files changed, 34 insertions, 2 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index 10a6d071b0f..60c62430370 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -290,6 +290,38 @@ class DictTest(unittest.TestCase):
['Cannot convert dictionary update sequence element #0 to a sequence'],
)
+ def test_update_shared_keys(self):
+ class MyClass: pass
+
+ # Subclass str to enable us to create an object during the
+ # dict.update() call.
+ class MyStr(str):
+ def __hash__(self):
+ return super().__hash__()
+
+ def __eq__(self, other):
+ # Create an object that shares the same PyDictKeysObject as
+ # obj.__dict__.
+ obj2 = MyClass()
+ obj2.a = "a"
+ obj2.b = "b"
+ obj2.c = "c"
+ return super().__eq__(other)
+
+ obj = MyClass()
+ obj.a = "a"
+ obj.b = "b"
+
+ x = {}
+ x[MyStr("a")] = MyStr("a")
+
+ # gh-132617: this previously raised "dict mutated during update" error
+ x.update(obj.__dict__)
+
+ self.assertEqual(x, {
+ MyStr("a"): "a",
+ "b": "b",
+ })
def test_fromkeys(self):
self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
@@ -787,8 +819,8 @@ class DictTest(unittest.TestCase):
def test_missing(self):
# Make sure dict doesn't have a __missing__ method
- self.assertFalse(hasattr(dict, "__missing__"))
- self.assertFalse(hasattr({}, "__missing__"))
+ self.assertNotHasAttr(dict, "__missing__")
+ self.assertNotHasAttr({}, "__missing__")
# Test several cases:
# (D) subclass defines __missing__ method returning a value
# (E) subclass defines __missing__ method raising RuntimeError