def test_fromkeys_operator_modifying_set_operand(self): # test fix for seg fault reported in issue 27945 part 4b. class X(int): def __hash__(self): return 13 def __eq__(self, other): if len(d) > 1: d.clear() return False d = {} # this is required to exist so that d can be constructed! d = {X(1), X(2)} try: Dict.fromkeys(d) # shouldn't crash except RuntimeError: # implementation defined pass
def test_track_dynamic(self): # Test GC-optimization of dynamically-created dicts class MyObject(object): pass x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject() d = Dict() self._not_tracked(d) d[1] = "a" self._not_tracked(d) d[y] = 2 self._not_tracked(d) d[z] = 3 self._not_tracked(d) self._not_tracked(d.copy()) d[4] = w self._tracked(d) self._tracked(d.copy()) d[4] = None self._not_tracked(d) self._not_tracked(d.copy()) # dd isn't tracked right now, but it may mutate and therefore d # which contains it must be tracked. d = Dict() dd = Dict() d[1] = dd self._not_tracked(dd) self._tracked(d) dd[1] = d self._tracked(dd) d = Dict.fromkeys([x, y, z]) self._not_tracked(d) dd = Dict() dd.update(d) self._not_tracked(dd) d = Dict.fromkeys([x, y, z, o]) self._tracked(d) dd = Dict() dd.update(d) self._tracked(dd) d = Dict(x=x, y=y, z=z) self._not_tracked(d) d = Dict(x=x, y=y, z=z, w=w) self._tracked(d) d = Dict() d.update(x=x, y=y, z=z) self._not_tracked(d) d.update(w=w) self._tracked(d) d = Dict([(x, y), (z, 1)]) self._not_tracked(d) d = Dict([(x, y), (z, w)]) self._tracked(d) d = Dict() d.update([(x, y), (z, 1)]) self._not_tracked(d) d.update([(x, y), (z, w)]) self._tracked(d)
def test_all(self): # Test constructors u = Dict() u0 = Dict(d0) u1 = Dict(d1) u2 = Dict(d2) uu = Dict(u) uu0 = Dict(u0) uu1 = Dict(u1) uu2 = Dict(u2) # keyword arg constructor self.assertEqual(Dict(one=1, two=2), d2) # item sequence constructor self.assertEqual(Dict([("one", 1), ("two", 2)]), d2) with self.assertWarnsRegex(DeprecationWarning, "'dict'"): self.assertEqual(Dict(dict=[("one", 1), ("two", 2)]), d2) # both together self.assertEqual(Dict([("one", 1), ("two", 2)], two=3, three=5), d3) # alternate constructor self.assertEqual(Dict.fromkeys("one two".split()), d4) self.assertEqual(Dict().fromkeys("one two".split()), d4) self.assertEqual(Dict.fromkeys("one two".split(), 1), d5) self.assertEqual(Dict().fromkeys("one two".split(), 1), d5) self.assertTrue(u1.fromkeys("one two".split()) is not u1) self.assertIsInstance(u1.fromkeys("one two".split()), Dict) self.assertIsInstance(u2.fromkeys("one two".split()), Dict) # Test __repr__ self.assertEqual(str(u0), str(d0)) self.assertEqual(repr(u1), repr(d1)) self.assertIn(repr(u2), ("{'one': 1, 'two': 2}", "{'two': 2, 'one': 1}")) # Test rich comparison and __len__ all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] for a in all: for b in all: self.assertEqual(a == b, len(a) == len(b)) # Test __getitem__ self.assertEqual(u2["one"], 1) self.assertRaises(KeyError, u1.__getitem__, "two") # Test __setitem__ u3 = Dict(u2) u3["two"] = 2 u3["three"] = 3 # Test __delitem__ del u3["three"] self.assertRaises(KeyError, u3.__delitem__, "three") # Test clear u3.clear() self.assertEqual(u3, {}) # Test copy() u2a = u2.copy() self.assertEqual(u2a, u2) u2b = Dict(x=42, y=23) u2c = u2b.copy() # making a copy of a UserDict is special cased self.assertEqual(u2b, u2c) class MyUserDict(Dict): def display(self): print(self) m2 = MyUserDict(u2) m2a = m2.copy() self.assertEqual(m2a, m2) # SF bug #476616 -- copy() of UserDict subclass shared data m2["foo"] = "bar" self.assertNotEqual(m2a, m2) # Test keys, items, values self.assertEqual(sorted(u2.keys()), sorted(d2.keys())) self.assertEqual(sorted(u2.items()), sorted(d2.items())) self.assertEqual(sorted(u2.values()), sorted(d2.values())) # Test "in". for i in u2.keys(): self.assertIn(i, u2) self.assertEqual(i in u1, i in d1) self.assertEqual(i in u0, i in d0) # Test update t = Dict() t.update(u2) self.assertEqual(t, u2) # Test get for i in u2.keys(): self.assertEqual(u2.get(i), u2[i]) self.assertEqual(u1.get(i), d1.get(i)) self.assertEqual(u0.get(i), d0.get(i)) # Test "in" iteration. for i in range(20): u2[i] = str(i) ikeys = [] for k in u2: ikeys.append(k) keys = u2.keys() self.assertEqual(set(ikeys), set(keys)) # Test setdefault t = Dict() self.assertEqual(t.setdefault("x", 42), 42) self.assertIn("x", t) self.assertEqual(t.setdefault("x", 23), 42) # Test pop t = Dict(x=42) self.assertEqual(t.pop("x"), 42) self.assertRaises(KeyError, t.pop, "x") self.assertEqual(t.pop("x", 1), 1) t["x"] = 42 self.assertEqual(t.pop("x", 1), 42) # Test popitem t = Dict(x=42) self.assertEqual(t.popitem(), ("x", 42)) self.assertRaises(KeyError, t.popitem)