diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-02-26 23:59:34 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-02-26 23:59:34 (GMT) |
commit | 70d2717f2e3fcf7e03b722ef2b63fbdf39bec2d2 (patch) | |
tree | 8bc02db1871066542ee75094805a54a5be6e3958 /Lib | |
parent | 6181b397c44842ba3d651f1a1ba41e3f8adb0d49 (diff) | |
parent | e965d97ed1adac6a9a5cf69f04770249589756c7 (diff) | |
download | cpython-70d2717f2e3fcf7e03b722ef2b63fbdf39bec2d2.zip cpython-70d2717f2e3fcf7e03b722ef2b63fbdf39bec2d2.tar.gz cpython-70d2717f2e3fcf7e03b722ef2b63fbdf39bec2d2.tar.bz2 |
Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Patch by Filip GruszczyĆski.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_dict.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 1507e42..d2740a3 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -299,6 +299,26 @@ class DictTest(unittest.TestCase): x.fail = True self.assertRaises(Exc, d.setdefault, x, []) + def test_setdefault_atomic(self): + # Issue #13521: setdefault() calls __hash__ and __eq__ only once. + class Hashed(object): + def __init__(self): + self.hash_count = 0 + self.eq_count = 0 + def __hash__(self): + self.hash_count += 1 + return 42 + def __eq__(self, other): + self.eq_count += 1 + return id(self) == id(other) + hashed1 = Hashed() + y = {hashed1: 5} + hashed2 = Hashed() + y.setdefault(hashed2, []) + self.assertEqual(hashed1.hash_count, 1) + self.assertEqual(hashed2.hash_count, 1) + self.assertEqual(hashed1.eq_count + hashed2.eq_count, 1) + def test_popitem(self): # dict.popitem() for copymode in -1, +1: |