Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 3 | |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 4 | import collections, random, string |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 5 | import gc, weakref |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class DictTest(unittest.TestCase): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 10 | def test_invalid_keyword_arguments(self): |
Benjamin Peterson | f609654 | 2010-11-17 22:33:12 +0000 | [diff] [blame] | 11 | class Custom(dict): |
| 12 | pass |
| 13 | for invalid in {1 : 2}, Custom({1 : 2}): |
| 14 | with self.assertRaises(TypeError): |
| 15 | dict(**invalid) |
| 16 | with self.assertRaises(TypeError): |
| 17 | {}.update(**invalid) |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 18 | |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 19 | def test_constructor(self): |
| 20 | # calling built-in types without argument must return empty |
| 21 | self.assertEqual(dict(), {}) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 22 | self.assertIsNot(dict(), {}) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 23 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 24 | def test_literal_constructor(self): |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 25 | # check literal constructor for different sized dicts |
| 26 | # (to exercise the BUILD_MAP oparg). |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 27 | for n in (0, 1, 6, 256, 400): |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 28 | items = [(''.join(random.sample(string.ascii_letters, 8)), i) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 29 | for i in range(n)] |
| 30 | random.shuffle(items) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 31 | formatted_items = ('{!r}: {:d}'.format(k, v) for k, v in items) |
| 32 | dictliteral = '{' + ', '.join(formatted_items) + '}' |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 33 | self.assertEqual(eval(dictliteral), dict(items)) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 34 | |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 35 | def test_bool(self): |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 36 | self.assertIs(not {}, True) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 37 | self.assertTrue({1: 2}) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 38 | self.assertIs(bool({}), False) |
| 39 | self.assertIs(bool({1: 2}), True) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 40 | |
| 41 | def test_keys(self): |
| 42 | d = {} |
Guido van Rossum | e34cdd1 | 2007-02-14 17:49:04 +0000 | [diff] [blame] | 43 | self.assertEqual(set(d.keys()), set()) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 44 | d = {'a': 1, 'b': 2} |
| 45 | k = d.keys() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 46 | self.assertIn('a', d) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 47 | self.assertIn('b', d) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 48 | self.assertRaises(TypeError, d.keys, None) |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 49 | self.assertEqual(repr(dict(a=1).keys()), "dict_keys(['a'])") |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 50 | |
| 51 | def test_values(self): |
| 52 | d = {} |
Guido van Rossum | e34cdd1 | 2007-02-14 17:49:04 +0000 | [diff] [blame] | 53 | self.assertEqual(set(d.values()), set()) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 54 | d = {1:2} |
Guido van Rossum | e34cdd1 | 2007-02-14 17:49:04 +0000 | [diff] [blame] | 55 | self.assertEqual(set(d.values()), {2}) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 56 | self.assertRaises(TypeError, d.values, None) |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 57 | self.assertEqual(repr(dict(a=1).values()), "dict_values([1])") |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 58 | |
| 59 | def test_items(self): |
| 60 | d = {} |
Guido van Rossum | e34cdd1 | 2007-02-14 17:49:04 +0000 | [diff] [blame] | 61 | self.assertEqual(set(d.items()), set()) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 62 | |
| 63 | d = {1:2} |
Guido van Rossum | e34cdd1 | 2007-02-14 17:49:04 +0000 | [diff] [blame] | 64 | self.assertEqual(set(d.items()), {(1, 2)}) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 65 | self.assertRaises(TypeError, d.items, None) |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 66 | self.assertEqual(repr(dict(a=1).items()), "dict_items([('a', 1)])") |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 67 | |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 68 | def test_contains(self): |
| 69 | d = {} |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 70 | self.assertNotIn('a', d) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 71 | self.assertFalse('a' in d) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 72 | self.assertTrue('a' not in d) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 73 | d = {'a': 1, 'b': 2} |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 74 | self.assertIn('a', d) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 75 | self.assertIn('b', d) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 76 | self.assertNotIn('c', d) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 77 | |
| 78 | self.assertRaises(TypeError, d.__contains__) |
| 79 | |
| 80 | def test_len(self): |
| 81 | d = {} |
| 82 | self.assertEqual(len(d), 0) |
| 83 | d = {'a': 1, 'b': 2} |
| 84 | self.assertEqual(len(d), 2) |
| 85 | |
| 86 | def test_getitem(self): |
| 87 | d = {'a': 1, 'b': 2} |
| 88 | self.assertEqual(d['a'], 1) |
| 89 | self.assertEqual(d['b'], 2) |
| 90 | d['c'] = 3 |
| 91 | d['a'] = 4 |
| 92 | self.assertEqual(d['c'], 3) |
| 93 | self.assertEqual(d['a'], 4) |
| 94 | del d['b'] |
| 95 | self.assertEqual(d, {'a': 4, 'c': 3}) |
| 96 | |
| 97 | self.assertRaises(TypeError, d.__getitem__) |
| 98 | |
| 99 | class BadEq(object): |
| 100 | def __eq__(self, other): |
| 101 | raise Exc() |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 102 | def __hash__(self): |
| 103 | return 24 |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 104 | |
| 105 | d = {} |
| 106 | d[BadEq()] = 42 |
| 107 | self.assertRaises(KeyError, d.__getitem__, 23) |
| 108 | |
| 109 | class Exc(Exception): pass |
| 110 | |
| 111 | class BadHash(object): |
| 112 | fail = False |
| 113 | def __hash__(self): |
| 114 | if self.fail: |
| 115 | raise Exc() |
| 116 | else: |
| 117 | return 42 |
| 118 | |
| 119 | x = BadHash() |
| 120 | d[x] = 42 |
| 121 | x.fail = True |
| 122 | self.assertRaises(Exc, d.__getitem__, x) |
| 123 | |
| 124 | def test_clear(self): |
| 125 | d = {1:1, 2:2, 3:3} |
| 126 | d.clear() |
| 127 | self.assertEqual(d, {}) |
| 128 | |
| 129 | self.assertRaises(TypeError, d.clear, None) |
| 130 | |
| 131 | def test_update(self): |
| 132 | d = {} |
| 133 | d.update({1:100}) |
| 134 | d.update({2:20}) |
| 135 | d.update({1:1, 2:2, 3:3}) |
| 136 | self.assertEqual(d, {1:1, 2:2, 3:3}) |
| 137 | |
| 138 | d.update() |
| 139 | self.assertEqual(d, {1:1, 2:2, 3:3}) |
| 140 | |
| 141 | self.assertRaises((TypeError, AttributeError), d.update, None) |
| 142 | |
| 143 | class SimpleUserDict: |
| 144 | def __init__(self): |
| 145 | self.d = {1:1, 2:2, 3:3} |
| 146 | def keys(self): |
| 147 | return self.d.keys() |
| 148 | def __getitem__(self, i): |
| 149 | return self.d[i] |
| 150 | d.clear() |
| 151 | d.update(SimpleUserDict()) |
| 152 | self.assertEqual(d, {1:1, 2:2, 3:3}) |
| 153 | |
| 154 | class Exc(Exception): pass |
| 155 | |
| 156 | d.clear() |
| 157 | class FailingUserDict: |
| 158 | def keys(self): |
| 159 | raise Exc |
| 160 | self.assertRaises(Exc, d.update, FailingUserDict()) |
| 161 | |
| 162 | class FailingUserDict: |
| 163 | def keys(self): |
| 164 | class BogonIter: |
| 165 | def __init__(self): |
| 166 | self.i = 1 |
| 167 | def __iter__(self): |
| 168 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 169 | def __next__(self): |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 170 | if self.i: |
| 171 | self.i = 0 |
| 172 | return 'a' |
| 173 | raise Exc |
| 174 | return BogonIter() |
| 175 | def __getitem__(self, key): |
| 176 | return key |
| 177 | self.assertRaises(Exc, d.update, FailingUserDict()) |
| 178 | |
| 179 | class FailingUserDict: |
| 180 | def keys(self): |
| 181 | class BogonIter: |
| 182 | def __init__(self): |
| 183 | self.i = ord('a') |
| 184 | def __iter__(self): |
| 185 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 186 | def __next__(self): |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 187 | if self.i <= ord('z'): |
| 188 | rtn = chr(self.i) |
| 189 | self.i += 1 |
| 190 | return rtn |
| 191 | raise StopIteration |
| 192 | return BogonIter() |
| 193 | def __getitem__(self, key): |
| 194 | raise Exc |
| 195 | self.assertRaises(Exc, d.update, FailingUserDict()) |
| 196 | |
| 197 | class badseq(object): |
| 198 | def __iter__(self): |
| 199 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 200 | def __next__(self): |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 201 | raise Exc() |
| 202 | |
| 203 | self.assertRaises(Exc, {}.update, badseq()) |
| 204 | |
| 205 | self.assertRaises(ValueError, {}.update, [(1, 2, 3)]) |
| 206 | |
| 207 | def test_fromkeys(self): |
| 208 | self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) |
| 209 | d = {} |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 210 | self.assertIsNot(d.fromkeys('abc'), d) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 211 | self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) |
| 212 | self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0}) |
| 213 | self.assertEqual(d.fromkeys([]), {}) |
| 214 | def g(): |
| 215 | yield 1 |
| 216 | self.assertEqual(d.fromkeys(g()), {1:None}) |
| 217 | self.assertRaises(TypeError, {}.fromkeys, 3) |
| 218 | class dictlike(dict): pass |
| 219 | self.assertEqual(dictlike.fromkeys('a'), {'a':None}) |
| 220 | self.assertEqual(dictlike().fromkeys('a'), {'a':None}) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 221 | self.assertIsInstance(dictlike.fromkeys('a'), dictlike) |
| 222 | self.assertIsInstance(dictlike().fromkeys('a'), dictlike) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 223 | class mydict(dict): |
| 224 | def __new__(cls): |
Raymond Hettinger | f80680d | 2008-02-06 00:07:11 +0000 | [diff] [blame] | 225 | return collections.UserDict() |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 226 | ud = mydict.fromkeys('ab') |
| 227 | self.assertEqual(ud, {'a':None, 'b':None}) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 228 | self.assertIsInstance(ud, collections.UserDict) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 229 | self.assertRaises(TypeError, dict.fromkeys) |
| 230 | |
| 231 | class Exc(Exception): pass |
| 232 | |
| 233 | class baddict1(dict): |
| 234 | def __init__(self): |
| 235 | raise Exc() |
| 236 | |
| 237 | self.assertRaises(Exc, baddict1.fromkeys, [1]) |
| 238 | |
| 239 | class BadSeq(object): |
| 240 | def __iter__(self): |
| 241 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 242 | def __next__(self): |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 243 | raise Exc() |
| 244 | |
| 245 | self.assertRaises(Exc, dict.fromkeys, BadSeq()) |
| 246 | |
| 247 | class baddict2(dict): |
| 248 | def __setitem__(self, key, value): |
| 249 | raise Exc() |
| 250 | |
| 251 | self.assertRaises(Exc, baddict2.fromkeys, [1]) |
| 252 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 253 | # test fast path for dictionary inputs |
| 254 | d = dict(zip(range(6), range(6))) |
| 255 | self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6))) |
| 256 | |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 257 | def test_copy(self): |
| 258 | d = {1:1, 2:2, 3:3} |
| 259 | self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) |
| 260 | self.assertEqual({}.copy(), {}) |
| 261 | self.assertRaises(TypeError, d.copy, None) |
| 262 | |
| 263 | def test_get(self): |
| 264 | d = {} |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 265 | self.assertIs(d.get('c'), None) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 266 | self.assertEqual(d.get('c', 3), 3) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 267 | d = {'a': 1, 'b': 2} |
| 268 | self.assertIs(d.get('c'), None) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 269 | self.assertEqual(d.get('c', 3), 3) |
| 270 | self.assertEqual(d.get('a'), 1) |
| 271 | self.assertEqual(d.get('a', 3), 1) |
| 272 | self.assertRaises(TypeError, d.get) |
| 273 | self.assertRaises(TypeError, d.get, None, None, None) |
| 274 | |
| 275 | def test_setdefault(self): |
| 276 | # dict.setdefault() |
| 277 | d = {} |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 278 | self.assertIs(d.setdefault('key0'), None) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 279 | d.setdefault('key0', []) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 280 | self.assertIs(d.setdefault('key0'), None) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 281 | d.setdefault('key', []).append(3) |
| 282 | self.assertEqual(d['key'][0], 3) |
| 283 | d.setdefault('key', []).append(4) |
| 284 | self.assertEqual(len(d['key']), 2) |
| 285 | self.assertRaises(TypeError, d.setdefault) |
| 286 | |
| 287 | class Exc(Exception): pass |
| 288 | |
| 289 | class BadHash(object): |
| 290 | fail = False |
| 291 | def __hash__(self): |
| 292 | if self.fail: |
| 293 | raise Exc() |
| 294 | else: |
| 295 | return 42 |
| 296 | |
| 297 | x = BadHash() |
| 298 | d[x] = 42 |
| 299 | x.fail = True |
| 300 | self.assertRaises(Exc, d.setdefault, x, []) |
| 301 | |
| 302 | def test_popitem(self): |
| 303 | # dict.popitem() |
| 304 | for copymode in -1, +1: |
| 305 | # -1: b has same structure as a |
| 306 | # +1: b is a.copy() |
| 307 | for log2size in range(12): |
| 308 | size = 2**log2size |
| 309 | a = {} |
| 310 | b = {} |
| 311 | for i in range(size): |
| 312 | a[repr(i)] = i |
| 313 | if copymode < 0: |
| 314 | b[repr(i)] = i |
| 315 | if copymode > 0: |
| 316 | b = a.copy() |
| 317 | for i in range(size): |
| 318 | ka, va = ta = a.popitem() |
| 319 | self.assertEqual(va, int(ka)) |
| 320 | kb, vb = tb = b.popitem() |
| 321 | self.assertEqual(vb, int(kb)) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 322 | self.assertFalse(copymode < 0 and ta != tb) |
| 323 | self.assertFalse(a) |
| 324 | self.assertFalse(b) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 325 | |
| 326 | d = {} |
| 327 | self.assertRaises(KeyError, d.popitem) |
| 328 | |
| 329 | def test_pop(self): |
| 330 | # Tests for pop with specified key |
| 331 | d = {} |
| 332 | k, v = 'abc', 'def' |
| 333 | d[k] = v |
| 334 | self.assertRaises(KeyError, d.pop, 'ghi') |
| 335 | |
| 336 | self.assertEqual(d.pop(k), v) |
| 337 | self.assertEqual(len(d), 0) |
| 338 | |
| 339 | self.assertRaises(KeyError, d.pop, k) |
| 340 | |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 341 | self.assertEqual(d.pop(k, v), v) |
| 342 | d[k] = v |
| 343 | self.assertEqual(d.pop(k, 1), v) |
| 344 | |
| 345 | self.assertRaises(TypeError, d.pop) |
| 346 | |
| 347 | class Exc(Exception): pass |
| 348 | |
| 349 | class BadHash(object): |
| 350 | fail = False |
| 351 | def __hash__(self): |
| 352 | if self.fail: |
| 353 | raise Exc() |
| 354 | else: |
| 355 | return 42 |
| 356 | |
| 357 | x = BadHash() |
| 358 | d[x] = 42 |
| 359 | x.fail = True |
| 360 | self.assertRaises(Exc, d.pop, x) |
| 361 | |
| 362 | def test_mutatingiteration(self): |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 363 | # changing dict size during iteration |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 364 | d = {} |
| 365 | d[1] = 1 |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 366 | with self.assertRaises(RuntimeError): |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 367 | for i in d: |
| 368 | d[i+1] = 1 |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 369 | |
| 370 | def test_repr(self): |
| 371 | d = {} |
| 372 | self.assertEqual(repr(d), '{}') |
| 373 | d[1] = 2 |
| 374 | self.assertEqual(repr(d), '{1: 2}') |
| 375 | d = {} |
| 376 | d[1] = d |
| 377 | self.assertEqual(repr(d), '{1: {...}}') |
| 378 | |
| 379 | class Exc(Exception): pass |
| 380 | |
| 381 | class BadRepr(object): |
| 382 | def __repr__(self): |
| 383 | raise Exc() |
| 384 | |
| 385 | d = {1: BadRepr()} |
| 386 | self.assertRaises(Exc, repr, d) |
| 387 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 388 | def test_eq(self): |
| 389 | self.assertEqual({}, {}) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 390 | self.assertEqual({1: 2}, {1: 2}) |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 391 | |
| 392 | class Exc(Exception): pass |
| 393 | |
| 394 | class BadCmp(object): |
| 395 | def __eq__(self, other): |
| 396 | raise Exc() |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 397 | def __hash__(self): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 398 | return 1 |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 399 | |
| 400 | d1 = {BadCmp(): 1} |
| 401 | d2 = {1: 1} |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 402 | |
| 403 | with self.assertRaises(Exc): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 404 | d1 == d2 |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 406 | def test_keys_contained(self): |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 407 | self.helper_keys_contained(lambda x: x.keys()) |
| 408 | self.helper_keys_contained(lambda x: x.items()) |
| 409 | |
| 410 | def helper_keys_contained(self, fn): |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 411 | # Test rich comparisons against dict key views, which should behave the |
| 412 | # same as sets. |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 413 | empty = fn(dict()) |
| 414 | empty2 = fn(dict()) |
| 415 | smaller = fn({1:1, 2:2}) |
| 416 | larger = fn({1:1, 2:2, 3:3}) |
| 417 | larger2 = fn({1:1, 2:2, 3:3}) |
| 418 | larger3 = fn({4:1, 2:2, 3:3}) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 420 | self.assertTrue(smaller < larger) |
| 421 | self.assertTrue(smaller <= larger) |
| 422 | self.assertTrue(larger > smaller) |
| 423 | self.assertTrue(larger >= smaller) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 425 | self.assertFalse(smaller >= larger) |
| 426 | self.assertFalse(smaller > larger) |
| 427 | self.assertFalse(larger <= smaller) |
| 428 | self.assertFalse(larger < smaller) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 430 | self.assertFalse(smaller < larger3) |
| 431 | self.assertFalse(smaller <= larger3) |
| 432 | self.assertFalse(larger3 > smaller) |
| 433 | self.assertFalse(larger3 >= smaller) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 434 | |
| 435 | # Inequality strictness |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 436 | self.assertTrue(larger2 >= larger) |
| 437 | self.assertTrue(larger2 <= larger) |
| 438 | self.assertFalse(larger2 > larger) |
| 439 | self.assertFalse(larger2 < larger) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 440 | |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 441 | self.assertTrue(larger == larger2) |
| 442 | self.assertTrue(smaller != larger) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 443 | |
| 444 | # There is an optimization on the zero-element case. |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 445 | self.assertTrue(empty == empty2) |
| 446 | self.assertFalse(empty != empty2) |
| 447 | self.assertFalse(empty == smaller) |
| 448 | self.assertTrue(empty != smaller) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 449 | |
| 450 | # With the same size, an elementwise compare happens |
Guido van Rossum | cf2ce36 | 2007-08-24 23:49:54 +0000 | [diff] [blame] | 451 | self.assertTrue(larger != larger3) |
| 452 | self.assertFalse(larger == larger3) |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 453 | |
| 454 | def test_errors_in_view_containment_check(self): |
| 455 | class C: |
| 456 | def __eq__(self, other): |
| 457 | raise RuntimeError |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 459 | d1 = {1: C()} |
| 460 | d2 = {1: C()} |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 461 | with self.assertRaises(RuntimeError): |
| 462 | d1.items() == d2.items() |
| 463 | with self.assertRaises(RuntimeError): |
| 464 | d1.items() != d2.items() |
| 465 | with self.assertRaises(RuntimeError): |
| 466 | d1.items() <= d2.items() |
| 467 | with self.assertRaises(RuntimeError): |
| 468 | d1.items() >= d2.items() |
| 469 | |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 470 | d3 = {1: C(), 2: C()} |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 471 | with self.assertRaises(RuntimeError): |
| 472 | d2.items() < d3.items() |
| 473 | with self.assertRaises(RuntimeError): |
| 474 | d3.items() > d2.items() |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 475 | |
Guido van Rossum | be53471 | 2007-08-24 23:43:52 +0000 | [diff] [blame] | 476 | def test_dictview_set_operations_on_keys(self): |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 477 | k1 = {1:1, 2:2}.keys() |
| 478 | k2 = {1:1, 2:2, 3:3}.keys() |
| 479 | k3 = {4:4}.keys() |
| 480 | |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 481 | self.assertEqual(k1 - k2, set()) |
| 482 | self.assertEqual(k1 - k3, {1,2}) |
| 483 | self.assertEqual(k2 - k1, {3}) |
| 484 | self.assertEqual(k3 - k1, {4}) |
| 485 | self.assertEqual(k1 & k2, {1,2}) |
| 486 | self.assertEqual(k1 & k3, set()) |
| 487 | self.assertEqual(k1 | k2, {1,2,3}) |
| 488 | self.assertEqual(k1 ^ k2, {3}) |
| 489 | self.assertEqual(k1 ^ k3, {1,2,4}) |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | be53471 | 2007-08-24 23:43:52 +0000 | [diff] [blame] | 491 | def test_dictview_set_operations_on_items(self): |
| 492 | k1 = {1:1, 2:2}.items() |
| 493 | k2 = {1:1, 2:2, 3:3}.items() |
| 494 | k3 = {4:4}.items() |
| 495 | |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 496 | self.assertEqual(k1 - k2, set()) |
| 497 | self.assertEqual(k1 - k3, {(1,1), (2,2)}) |
| 498 | self.assertEqual(k2 - k1, {(3,3)}) |
| 499 | self.assertEqual(k3 - k1, {(4,4)}) |
| 500 | self.assertEqual(k1 & k2, {(1,1), (2,2)}) |
| 501 | self.assertEqual(k1 & k3, set()) |
| 502 | self.assertEqual(k1 | k2, {(1,1), (2,2), (3,3)}) |
| 503 | self.assertEqual(k1 ^ k2, {(3,3)}) |
| 504 | self.assertEqual(k1 ^ k3, {(1,1), (2,2), (4,4)}) |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | 1d71996 | 2007-08-24 23:47:30 +0000 | [diff] [blame] | 506 | def test_dictview_mixed_set_operations(self): |
| 507 | # Just a few for .keys() |
Guido van Rossum | a401bbe | 2007-08-24 23:51:55 +0000 | [diff] [blame] | 508 | self.assertTrue({1:1}.keys() == {1}) |
| 509 | self.assertTrue({1} == {1:1}.keys()) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 510 | self.assertEqual({1:1}.keys() | {2}, {1, 2}) |
| 511 | self.assertEqual({2} | {1:1}.keys(), {1, 2}) |
Guido van Rossum | 1d71996 | 2007-08-24 23:47:30 +0000 | [diff] [blame] | 512 | # And a few for .items() |
Guido van Rossum | a401bbe | 2007-08-24 23:51:55 +0000 | [diff] [blame] | 513 | self.assertTrue({1:1}.items() == {(1,1)}) |
| 514 | self.assertTrue({(1,1)} == {1:1}.items()) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 515 | self.assertEqual({1:1}.items() | {2}, {(1,1), 2}) |
| 516 | self.assertEqual({2} | {1:1}.items(), {(1,1), 2}) |
Guido van Rossum | 1d71996 | 2007-08-24 23:47:30 +0000 | [diff] [blame] | 517 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 518 | def test_missing(self): |
| 519 | # Make sure dict doesn't have a __missing__ method |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 520 | self.assertFalse(hasattr(dict, "__missing__")) |
| 521 | self.assertFalse(hasattr({}, "__missing__")) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 522 | # Test several cases: |
| 523 | # (D) subclass defines __missing__ method returning a value |
| 524 | # (E) subclass defines __missing__ method raising RuntimeError |
| 525 | # (F) subclass sets __missing__ instance variable (no effect) |
| 526 | # (G) subclass doesn't define __missing__ at a all |
| 527 | class D(dict): |
| 528 | def __missing__(self, key): |
| 529 | return 42 |
| 530 | d = D({1: 2, 3: 4}) |
| 531 | self.assertEqual(d[1], 2) |
| 532 | self.assertEqual(d[3], 4) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 533 | self.assertNotIn(2, d) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 534 | self.assertNotIn(2, d.keys()) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 535 | self.assertEqual(d[2], 42) |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 536 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 537 | class E(dict): |
| 538 | def __missing__(self, key): |
| 539 | raise RuntimeError(key) |
| 540 | e = E() |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 541 | with self.assertRaises(RuntimeError) as c: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 542 | e[42] |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 543 | self.assertEqual(c.exception.args, (42,)) |
| 544 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 545 | class F(dict): |
| 546 | def __init__(self): |
| 547 | # An instance variable __missing__ should have no effect |
| 548 | self.__missing__ = lambda key: None |
| 549 | f = F() |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 550 | with self.assertRaises(KeyError) as c: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 551 | f[42] |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 552 | self.assertEqual(c.exception.args, (42,)) |
| 553 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 554 | class G(dict): |
| 555 | pass |
| 556 | g = G() |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 557 | with self.assertRaises(KeyError) as c: |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 558 | g[42] |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 559 | self.assertEqual(c.exception.args, (42,)) |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 560 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 561 | def test_tuple_keyerror(self): |
| 562 | # SF #1576657 |
| 563 | d = {} |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 564 | with self.assertRaises(KeyError) as c: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 565 | d[(1,)] |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 566 | self.assertEqual(c.exception.args, ((1,),)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 567 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 568 | def test_bad_key(self): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 569 | # Dictionary lookups should fail if __eq__() raises an exception. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 570 | class CustomException(Exception): |
| 571 | pass |
| 572 | |
| 573 | class BadDictKey: |
| 574 | def __hash__(self): |
| 575 | return hash(self.__class__) |
| 576 | |
| 577 | def __eq__(self, other): |
| 578 | if isinstance(other, self.__class__): |
| 579 | raise CustomException |
| 580 | return other |
| 581 | |
| 582 | d = {} |
| 583 | x1 = BadDictKey() |
| 584 | x2 = BadDictKey() |
| 585 | d[x1] = 1 |
| 586 | for stmt in ['d[x2] = 2', |
| 587 | 'z = d[x2]', |
| 588 | 'x2 in d', |
| 589 | 'd.get(x2)', |
| 590 | 'd.setdefault(x2, 42)', |
| 591 | 'd.pop(x2)', |
| 592 | 'd.update({x2: 2})']: |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 593 | with self.assertRaises(CustomException): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 594 | exec(stmt, locals()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 595 | |
| 596 | def test_resize1(self): |
| 597 | # Dict resizing bug, found by Jack Jansen in 2.2 CVS development. |
| 598 | # This version got an assert failure in debug build, infinite loop in |
| 599 | # release build. Unfortunately, provoking this kind of stuff requires |
| 600 | # a mix of inserts and deletes hitting exactly the right hash codes in |
| 601 | # exactly the right order, and I can't think of a randomized approach |
| 602 | # that would be *likely* to hit a failing case in reasonable time. |
| 603 | |
| 604 | d = {} |
| 605 | for i in range(5): |
| 606 | d[i] = i |
| 607 | for i in range(5): |
| 608 | del d[i] |
| 609 | for i in range(5, 9): # i==8 was the problem |
| 610 | d[i] = i |
| 611 | |
| 612 | def test_resize2(self): |
| 613 | # Another dict resizing bug (SF bug #1456209). |
| 614 | # This caused Segmentation faults or Illegal instructions. |
| 615 | |
| 616 | class X(object): |
| 617 | def __hash__(self): |
| 618 | return 5 |
| 619 | def __eq__(self, other): |
| 620 | if resizing: |
| 621 | d.clear() |
| 622 | return False |
| 623 | d = {} |
| 624 | resizing = False |
| 625 | d[X()] = 1 |
| 626 | d[X()] = 2 |
| 627 | d[X()] = 3 |
| 628 | d[X()] = 4 |
| 629 | d[X()] = 5 |
| 630 | # now trigger a resize |
| 631 | resizing = True |
| 632 | d[9] = 6 |
| 633 | |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 634 | def test_empty_presized_dict_in_freelist(self): |
| 635 | # Bug #3537: if an empty but presized dict with a size larger |
| 636 | # than 7 was in the freelist, it triggered an assertion failure |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 637 | with self.assertRaises(ZeroDivisionError): |
| 638 | d = {'a': 1 // 0, 'b': None, 'c': None, 'd': None, 'e': None, |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 639 | 'f': None, 'g': None, 'h': None} |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 640 | d = {} |
| 641 | |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 642 | def test_container_iterator(self): |
| 643 | # Bug #3680: tp_traverse was not implemented for dictiter and |
| 644 | # dictview objects. |
| 645 | class C(object): |
| 646 | pass |
| 647 | views = (dict.items, dict.values, dict.keys) |
| 648 | for v in views: |
| 649 | obj = C() |
| 650 | ref = weakref.ref(obj) |
| 651 | container = {obj: 1} |
| 652 | obj.v = v(container) |
| 653 | obj.x = iter(obj.v) |
| 654 | del obj, container |
| 655 | gc.collect() |
Florent Xicluna | a988e42 | 2010-03-02 16:06:24 +0000 | [diff] [blame] | 656 | self.assertIs(ref(), None, "Cycle was not collected") |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 658 | def _not_tracked(self, t): |
| 659 | # Nested containers can take several collections to untrack |
| 660 | gc.collect() |
| 661 | gc.collect() |
| 662 | self.assertFalse(gc.is_tracked(t), t) |
| 663 | |
| 664 | def _tracked(self, t): |
| 665 | self.assertTrue(gc.is_tracked(t), t) |
| 666 | gc.collect() |
| 667 | gc.collect() |
| 668 | self.assertTrue(gc.is_tracked(t), t) |
| 669 | |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 670 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 671 | def test_track_literals(self): |
| 672 | # Test GC-optimization of dict literals |
| 673 | x, y, z, w = 1.5, "a", (1, None), [] |
| 674 | |
| 675 | self._not_tracked({}) |
| 676 | self._not_tracked({x:(), y:x, z:1}) |
| 677 | self._not_tracked({1: "a", "b": 2}) |
| 678 | self._not_tracked({1: 2, (None, True, False, ()): int}) |
| 679 | self._not_tracked({1: object()}) |
| 680 | |
| 681 | # Dicts with mutable elements are always tracked, even if those |
| 682 | # elements are not tracked right now. |
| 683 | self._tracked({1: []}) |
| 684 | self._tracked({1: ([],)}) |
| 685 | self._tracked({1: {}}) |
| 686 | self._tracked({1: set()}) |
| 687 | |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 688 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 689 | def test_track_dynamic(self): |
| 690 | # Test GC-optimization of dynamically-created dicts |
| 691 | class MyObject(object): |
| 692 | pass |
| 693 | x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject() |
| 694 | |
| 695 | d = dict() |
| 696 | self._not_tracked(d) |
| 697 | d[1] = "a" |
| 698 | self._not_tracked(d) |
| 699 | d[y] = 2 |
| 700 | self._not_tracked(d) |
| 701 | d[z] = 3 |
| 702 | self._not_tracked(d) |
| 703 | self._not_tracked(d.copy()) |
| 704 | d[4] = w |
| 705 | self._tracked(d) |
| 706 | self._tracked(d.copy()) |
| 707 | d[4] = None |
| 708 | self._not_tracked(d) |
| 709 | self._not_tracked(d.copy()) |
| 710 | |
| 711 | # dd isn't tracked right now, but it may mutate and therefore d |
| 712 | # which contains it must be tracked. |
| 713 | d = dict() |
| 714 | dd = dict() |
| 715 | d[1] = dd |
| 716 | self._not_tracked(dd) |
| 717 | self._tracked(d) |
| 718 | dd[1] = d |
| 719 | self._tracked(dd) |
| 720 | |
| 721 | d = dict.fromkeys([x, y, z]) |
| 722 | self._not_tracked(d) |
| 723 | dd = dict() |
| 724 | dd.update(d) |
| 725 | self._not_tracked(dd) |
| 726 | d = dict.fromkeys([x, y, z, o]) |
| 727 | self._tracked(d) |
| 728 | dd = dict() |
| 729 | dd.update(d) |
| 730 | self._tracked(dd) |
| 731 | |
| 732 | d = dict(x=x, y=y, z=z) |
| 733 | self._not_tracked(d) |
| 734 | d = dict(x=x, y=y, z=z, w=w) |
| 735 | self._tracked(d) |
| 736 | d = dict() |
| 737 | d.update(x=x, y=y, z=z) |
| 738 | self._not_tracked(d) |
| 739 | d.update(w=w) |
| 740 | self._tracked(d) |
| 741 | |
| 742 | d = dict([(x, y), (z, 1)]) |
| 743 | self._not_tracked(d) |
| 744 | d = dict([(x, y), (z, w)]) |
| 745 | self._tracked(d) |
| 746 | d = dict() |
| 747 | d.update([(x, y), (z, 1)]) |
| 748 | self._not_tracked(d) |
| 749 | d.update([(x, y), (z, w)]) |
| 750 | self._tracked(d) |
| 751 | |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 752 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 753 | def test_track_subtypes(self): |
| 754 | # Dict subtypes are always tracked |
| 755 | class MyDict(dict): |
| 756 | pass |
| 757 | self._tracked(MyDict()) |
| 758 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 759 | |
Neal Norwitz | c3e54b8 | 2006-03-24 07:38:37 +0000 | [diff] [blame] | 760 | from test import mapping_tests |
Raymond Hettinger | 49c522b | 2004-09-30 15:07:29 +0000 | [diff] [blame] | 761 | |
| 762 | class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol): |
| 763 | type2test = dict |
| 764 | |
| 765 | class Dict(dict): |
| 766 | pass |
| 767 | |
| 768 | class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol): |
| 769 | type2test = Dict |
| 770 | |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 771 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 772 | support.run_unittest( |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 773 | DictTest, |
Raymond Hettinger | 49c522b | 2004-09-30 15:07:29 +0000 | [diff] [blame] | 774 | GeneralMappingTests, |
| 775 | SubclassMappingTests, |
Walter Dörwald | 59b23e8 | 2004-09-30 13:46:00 +0000 | [diff] [blame] | 776 | ) |
| 777 | |
| 778 | if __name__ == "__main__": |
| 779 | test_main() |