annotate Lib/test/test_dict.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 0e498a9fde3c
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
1 import unittest
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
2 from test import test_support
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
3
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
4 import UserDict, random, string
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
5 import gc, weakref
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
6
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
7
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
8 class DictTest(unittest.TestCase):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
9 def test_constructor(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
10 # calling built-in types without argument must return empty
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
11 self.assertEqual(dict(), {})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
12 self.assert_(dict() is not {})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
13
44346
a28ac9b67bed Beef-up tests for dict literals
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
14 def test_literal_constructor(self):
a28ac9b67bed Beef-up tests for dict literals
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
15 # check literal constructor for different sized dicts (to exercise the BUILD_MAP oparg
45837
c5241069fffd Speed up test_dict by about 10x by only checking selected dict literal sizes,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45468
diff changeset
16 for n in (0, 1, 6, 256, 400):
c5241069fffd Speed up test_dict by about 10x by only checking selected dict literal sizes,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45468
diff changeset
17 items = [(''.join([random.choice(string.letters)
c5241069fffd Speed up test_dict by about 10x by only checking selected dict literal sizes,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45468
diff changeset
18 for j in range(8)]),
c5241069fffd Speed up test_dict by about 10x by only checking selected dict literal sizes,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45468
diff changeset
19 i)
c5241069fffd Speed up test_dict by about 10x by only checking selected dict literal sizes,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45468
diff changeset
20 for i in range(n)]
c5241069fffd Speed up test_dict by about 10x by only checking selected dict literal sizes,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45468
diff changeset
21 random.shuffle(items)
44346
a28ac9b67bed Beef-up tests for dict literals
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
22 dictliteral = '{' + ', '.join('%r: %d' % item for item in items) + '}'
a28ac9b67bed Beef-up tests for dict literals
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
23 self.assertEqual(eval(dictliteral), dict(items))
a28ac9b67bed Beef-up tests for dict literals
Raymond Hettinger <python@rcn.com>
parents: 43958
diff changeset
24
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
25 def test_bool(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
26 self.assert_(not {})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
27 self.assert_({1: 2})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
28 self.assert_(bool({}) is False)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
29 self.assert_(bool({1: 2}) is True)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
30
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
31 def test_keys(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
32 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
33 self.assertEqual(d.keys(), [])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
34 d = {'a': 1, 'b': 2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
35 k = d.keys()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
36 self.assert_(d.has_key('a'))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
37 self.assert_(d.has_key('b'))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
38
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
39 self.assertRaises(TypeError, d.keys, None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
40
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
41 def test_values(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
42 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
43 self.assertEqual(d.values(), [])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
44 d = {1:2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
45 self.assertEqual(d.values(), [2])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
46
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
47 self.assertRaises(TypeError, d.values, None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
48
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
49 def test_items(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
50 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
51 self.assertEqual(d.items(), [])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
52
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
53 d = {1:2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
54 self.assertEqual(d.items(), [(1, 2)])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
55
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
56 self.assertRaises(TypeError, d.items, None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
57
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
58 def test_has_key(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
59 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
60 self.assert_(not d.has_key('a'))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
61 d = {'a': 1, 'b': 2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
62 k = d.keys()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
63 k.sort()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
64 self.assertEqual(k, ['a', 'b'])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
65
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
66 self.assertRaises(TypeError, d.has_key)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
67
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
68 def test_contains(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
69 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
70 self.assert_(not ('a' in d))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
71 self.assert_('a' not in d)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
72 d = {'a': 1, 'b': 2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
73 self.assert_('a' in d)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
74 self.assert_('b' in d)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
75 self.assert_('c' not in d)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
76
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
77 self.assertRaises(TypeError, d.__contains__)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
78
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
79 def test_len(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
80 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
81 self.assertEqual(len(d), 0)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
82 d = {'a': 1, 'b': 2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
83 self.assertEqual(len(d), 2)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
84
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
85 def test_getitem(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
86 d = {'a': 1, 'b': 2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
87 self.assertEqual(d['a'], 1)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
88 self.assertEqual(d['b'], 2)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
89 d['c'] = 3
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
90 d['a'] = 4
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
91 self.assertEqual(d['c'], 3)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
92 self.assertEqual(d['a'], 4)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
93 del d['b']
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
94 self.assertEqual(d, {'a': 4, 'c': 3})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
95
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
96 self.assertRaises(TypeError, d.__getitem__)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
97
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
98 class BadEq(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
99 def __eq__(self, other):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
100 raise Exc()
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43783
diff changeset
101 def __hash__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43783
diff changeset
102 return 24
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
103
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
104 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
105 d[BadEq()] = 42
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
106 self.assertRaises(KeyError, d.__getitem__, 23)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
107
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
108 class Exc(Exception): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
109
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
110 class BadHash(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
111 fail = False
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
112 def __hash__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
113 if self.fail:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
114 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
115 else:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
116 return 42
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
117
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
118 x = BadHash()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
119 d[x] = 42
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
120 x.fail = True
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
121 self.assertRaises(Exc, d.__getitem__, x)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
122
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
123 def test_clear(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
124 d = {1:1, 2:2, 3:3}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
125 d.clear()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
126 self.assertEqual(d, {})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
127
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
128 self.assertRaises(TypeError, d.clear, None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
129
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
130 def test_update(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
131 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
132 d.update({1:100})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
133 d.update({2:20})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
134 d.update({1:1, 2:2, 3:3})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
135 self.assertEqual(d, {1:1, 2:2, 3:3})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
136
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
137 d.update()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
138 self.assertEqual(d, {1:1, 2:2, 3:3})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
139
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
140 self.assertRaises((TypeError, AttributeError), d.update, None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
141
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
142 class SimpleUserDict:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
143 def __init__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
144 self.d = {1:1, 2:2, 3:3}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
145 def keys(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
146 return self.d.keys()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
147 def __getitem__(self, i):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
148 return self.d[i]
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
149 d.clear()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
150 d.update(SimpleUserDict())
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
151 self.assertEqual(d, {1:1, 2:2, 3:3})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
152
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
153 class Exc(Exception): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
154
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
155 d.clear()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
156 class FailingUserDict:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
157 def keys(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
158 raise Exc
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
159 self.assertRaises(Exc, d.update, FailingUserDict())
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
160
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
161 class FailingUserDict:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
162 def keys(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
163 class BogonIter:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
164 def __init__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
165 self.i = 1
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
166 def __iter__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
167 return self
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
168 def next(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
169 if self.i:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
170 self.i = 0
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
171 return 'a'
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
172 raise Exc
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
173 return BogonIter()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
174 def __getitem__(self, key):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
175 return key
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
176 self.assertRaises(Exc, d.update, FailingUserDict())
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
177
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
178 class FailingUserDict:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
179 def keys(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
180 class BogonIter:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
181 def __init__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
182 self.i = ord('a')
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
183 def __iter__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
184 return self
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
185 def next(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
186 if self.i <= ord('z'):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
187 rtn = chr(self.i)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
188 self.i += 1
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
189 return rtn
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
190 raise StopIteration
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
191 return BogonIter()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
192 def __getitem__(self, key):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
193 raise Exc
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
194 self.assertRaises(Exc, d.update, FailingUserDict())
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
195
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
196 class badseq(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
197 def __iter__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
198 return self
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
199 def next(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
200 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
201
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
202 self.assertRaises(Exc, {}.update, badseq())
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
203
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
204 self.assertRaises(ValueError, {}.update, [(1, 2, 3)])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
205
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
206 def test_fromkeys(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
207 self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
208 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
209 self.assert_(not(d.fromkeys('abc') is d))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
210 self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
211 self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
212 self.assertEqual(d.fromkeys([]), {})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
213 def g():
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
214 yield 1
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
215 self.assertEqual(d.fromkeys(g()), {1:None})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
216 self.assertRaises(TypeError, {}.fromkeys, 3)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
217 class dictlike(dict): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
218 self.assertEqual(dictlike.fromkeys('a'), {'a':None})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
219 self.assertEqual(dictlike().fromkeys('a'), {'a':None})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
220 self.assert_(type(dictlike.fromkeys('a')) is dictlike)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
221 self.assert_(type(dictlike().fromkeys('a')) is dictlike)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
222 class mydict(dict):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
223 def __new__(cls):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
224 return UserDict.UserDict()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
225 ud = mydict.fromkeys('ab')
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
226 self.assertEqual(ud, {'a':None, 'b':None})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
227 self.assert_(isinstance(ud, UserDict.UserDict))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
228 self.assertRaises(TypeError, dict.fromkeys)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
229
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
230 class Exc(Exception): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
231
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
232 class baddict1(dict):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
233 def __init__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
234 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
235
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
236 self.assertRaises(Exc, baddict1.fromkeys, [1])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
237
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
238 class BadSeq(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
239 def __iter__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
240 return self
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
241 def next(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
242 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
243
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
244 self.assertRaises(Exc, dict.fromkeys, BadSeq())
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
245
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
246 class baddict2(dict):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
247 def __setitem__(self, key, value):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
248 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
249
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
250 self.assertRaises(Exc, baddict2.fromkeys, [1])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
251
43783
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
252 # test fast path for dictionary inputs
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
253 d = dict(zip(range(6), range(6)))
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
254 self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6)))
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
255
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
256 def test_copy(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
257 d = {1:1, 2:2, 3:3}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
258 self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
259 self.assertEqual({}.copy(), {})
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
260 self.assertRaises(TypeError, d.copy, None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
261
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
262 def test_get(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
263 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
264 self.assert_(d.get('c') is None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
265 self.assertEqual(d.get('c', 3), 3)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
266 d = {'a' : 1, 'b' : 2}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
267 self.assert_(d.get('c') is None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
268 self.assertEqual(d.get('c', 3), 3)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
269 self.assertEqual(d.get('a'), 1)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
270 self.assertEqual(d.get('a', 3), 1)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
271 self.assertRaises(TypeError, d.get)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
272 self.assertRaises(TypeError, d.get, None, None, None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
273
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
274 def test_setdefault(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
275 # dict.setdefault()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
276 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
277 self.assert_(d.setdefault('key0') is None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
278 d.setdefault('key0', [])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
279 self.assert_(d.setdefault('key0') is None)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
280 d.setdefault('key', []).append(3)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
281 self.assertEqual(d['key'][0], 3)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
282 d.setdefault('key', []).append(4)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
283 self.assertEqual(len(d['key']), 2)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
284 self.assertRaises(TypeError, d.setdefault)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
285
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
286 class Exc(Exception): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
287
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
288 class BadHash(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
289 fail = False
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
290 def __hash__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
291 if self.fail:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
292 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
293 else:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
294 return 42
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
295
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
296 x = BadHash()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
297 d[x] = 42
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
298 x.fail = True
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
299 self.assertRaises(Exc, d.setdefault, x, [])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
300
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
301 def test_popitem(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
302 # dict.popitem()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
303 for copymode in -1, +1:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
304 # -1: b has same structure as a
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
305 # +1: b is a.copy()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
306 for log2size in range(12):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
307 size = 2**log2size
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
308 a = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
309 b = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
310 for i in range(size):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
311 a[repr(i)] = i
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
312 if copymode < 0:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
313 b[repr(i)] = i
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
314 if copymode > 0:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
315 b = a.copy()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
316 for i in range(size):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
317 ka, va = ta = a.popitem()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
318 self.assertEqual(va, int(ka))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
319 kb, vb = tb = b.popitem()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
320 self.assertEqual(vb, int(kb))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
321 self.assert_(not(copymode < 0 and ta != tb))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
322 self.assert_(not a)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
323 self.assert_(not b)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
324
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
325 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
326 self.assertRaises(KeyError, d.popitem)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
327
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
328 def test_pop(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
329 # Tests for pop with specified key
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
330 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
331 k, v = 'abc', 'def'
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
332 d[k] = v
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
333 self.assertRaises(KeyError, d.pop, 'ghi')
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
334
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
335 self.assertEqual(d.pop(k), v)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
336 self.assertEqual(len(d), 0)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
337
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
338 self.assertRaises(KeyError, d.pop, k)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
339
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
340 # verify longs/ints get same value when key > 32 bits (for 64-bit archs)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
341 # see SF bug #689659
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
342 x = 4503599627370496L
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
343 y = 4503599627370496
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
344 h = {x: 'anything', y: 'something else'}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
345 self.assertEqual(h[x], h[y])
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
346
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
347 self.assertEqual(d.pop(k, v), v)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
348 d[k] = v
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
349 self.assertEqual(d.pop(k, 1), v)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
350
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
351 self.assertRaises(TypeError, d.pop)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
352
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
353 class Exc(Exception): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
354
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
355 class BadHash(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
356 fail = False
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
357 def __hash__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
358 if self.fail:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
359 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
360 else:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
361 return 42
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
362
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
363 x = BadHash()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
364 d[x] = 42
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
365 x.fail = True
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
366 self.assertRaises(Exc, d.pop, x)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
367
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
368 def test_mutatingiteration(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
369 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
370 d[1] = 1
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
371 try:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
372 for i in d:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
373 d[i+1] = 1
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
374 except RuntimeError:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
375 pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
376 else:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
377 self.fail("changing dict size during iteration doesn't raise Error")
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
378
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
379 def test_repr(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
380 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
381 self.assertEqual(repr(d), '{}')
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
382 d[1] = 2
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
383 self.assertEqual(repr(d), '{1: 2}')
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
384 d = {}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
385 d[1] = d
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
386 self.assertEqual(repr(d), '{1: {...}}')
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
387
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
388 class Exc(Exception): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
389
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
390 class BadRepr(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
391 def __repr__(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
392 raise Exc()
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
393
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
394 d = {1: BadRepr()}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
395 self.assertRaises(Exc, repr, d)
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
396
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
397 def test_le(self):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
398 self.assert_(not ({} < {}))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
399 self.assert_(not ({1: 2} < {1L: 2L}))
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
400
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
401 class Exc(Exception): pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
402
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
403 class BadCmp(object):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
404 def __eq__(self, other):
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
405 raise Exc()
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43783
diff changeset
406 def __hash__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43783
diff changeset
407 return 42
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
408
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
409 d1 = {BadCmp(): 1}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
410 d2 = {1: 1}
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
411 try:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
412 d1 < d2
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
413 except Exc:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
414 pass
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
415 else:
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
416 self.fail("< didn't raise Exc")
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
417
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
418 def test_missing(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
419 # Make sure dict doesn't have a __missing__ method
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
420 self.assertEqual(hasattr(dict, "__missing__"), False)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
421 self.assertEqual(hasattr({}, "__missing__"), False)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
422 # Test several cases:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
423 # (D) subclass defines __missing__ method returning a value
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
424 # (E) subclass defines __missing__ method raising RuntimeError
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
425 # (F) subclass sets __missing__ instance variable (no effect)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
426 # (G) subclass doesn't define __missing__ at a all
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
427 class D(dict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
428 def __missing__(self, key):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
429 return 42
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
430 d = D({1: 2, 3: 4})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
431 self.assertEqual(d[1], 2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
432 self.assertEqual(d[3], 4)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
433 self.assert_(2 not in d)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
434 self.assert_(2 not in d.keys())
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
435 self.assertEqual(d[2], 42)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
436 class E(dict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
437 def __missing__(self, key):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
438 raise RuntimeError(key)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
439 e = E()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
440 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
441 e[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
442 except RuntimeError, err:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
443 self.assertEqual(err.args, (42,))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
444 else:
41001
2f2b8935a81e Fix a bug in test_dict and test_userdict, found at the PyPy sprint.
Georg Brandl <georg@python.org>
parents: 40756
diff changeset
445 self.fail("e[42] didn't raise RuntimeError")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
446 class F(dict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
447 def __init__(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
448 # An instance variable __missing__ should have no effect
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
449 self.__missing__ = lambda key: None
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
450 f = F()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
451 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
452 f[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
453 except KeyError, err:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
454 self.assertEqual(err.args, (42,))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
455 else:
41001
2f2b8935a81e Fix a bug in test_dict and test_userdict, found at the PyPy sprint.
Georg Brandl <georg@python.org>
parents: 40756
diff changeset
456 self.fail("f[42] didn't raise KeyError")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
457 class G(dict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
458 pass
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
459 g = G()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
460 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
461 g[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
462 except KeyError, err:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
463 self.assertEqual(err.args, (42,))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
464 else:
41001
2f2b8935a81e Fix a bug in test_dict and test_userdict, found at the PyPy sprint.
Georg Brandl <georg@python.org>
parents: 40756
diff changeset
465 self.fail("g[42] didn't raise KeyError")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
466
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
467 def test_tuple_keyerror(self):
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
468 # SF #1576657
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
469 d = {}
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
470 try:
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
471 d[(1,)]
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
472 except KeyError, e:
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
473 self.assertEqual(e.args, ((1,),))
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
474 else:
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
475 self.fail("missing KeyError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41127
diff changeset
476
41127
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
477 def test_bad_key(self):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
478 # Dictionary lookups should fail if __cmp__() raises an exception.
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
479 class CustomException(Exception):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
480 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41127
diff changeset
481
41127
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
482 class BadDictKey:
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
483 def __hash__(self):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
484 return hash(self.__class__)
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
485
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
486 def __cmp__(self, other):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
487 if isinstance(other, self.__class__):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
488 raise CustomException
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
489 return other
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41127
diff changeset
490
41127
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
491 d = {}
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
492 x1 = BadDictKey()
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
493 x2 = BadDictKey()
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
494 d[x1] = 1
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
495 for stmt in ['d[x2] = 2',
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
496 'z = d[x2]',
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
497 'x2 in d',
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
498 'd.has_key(x2)',
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
499 'd.get(x2)',
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
500 'd.setdefault(x2, 42)',
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
501 'd.pop(x2)',
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
502 'd.update({x2: 2})']:
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
503 try:
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
504 exec stmt in locals()
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
505 except CustomException:
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
506 pass
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
507 else:
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
508 self.fail("Statement didn't raise exception")
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
509
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
510 def test_resize1(self):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
511 # Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
512 # This version got an assert failure in debug build, infinite loop in
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
513 # release build. Unfortunately, provoking this kind of stuff requires
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
514 # a mix of inserts and deletes hitting exactly the right hash codes in
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
515 # exactly the right order, and I can't think of a randomized approach
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
516 # that would be *likely* to hit a failing case in reasonable time.
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41127
diff changeset
517
41127
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
518 d = {}
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
519 for i in range(5):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
520 d[i] = i
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
521 for i in range(5):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
522 del d[i]
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
523 for i in range(5, 9): # i==8 was the problem
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
524 d[i] = i
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
525
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
526 def test_resize2(self):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
527 # Another dict resizing bug (SF bug #1456209).
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
528 # This caused Segmentation faults or Illegal instructions.
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41127
diff changeset
529
41127
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
530 class X(object):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
531 def __hash__(self):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
532 return 5
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
533 def __eq__(self, other):
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
534 if resizing:
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
535 d.clear()
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
536 return False
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
537 d = {}
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
538 resizing = False
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
539 d[X()] = 1
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
540 d[X()] = 2
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
541 d[X()] = 3
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
542 d[X()] = 4
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
543 d[X()] = 5
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
544 # now trigger a resize
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
545 resizing = True
7100c59182ff Patch #1678088: convert test_operations to use unittest, fold the result into test_dict.
Collin Winter <collinw@gmail.com>
parents: 41001
diff changeset
546 d[9] = 6
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 37408
diff changeset
547
48209
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
548 def test_empty_presized_dict_in_freelist(self):
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
549 # Bug #3537: if an empty but presized dict with a size larger
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
550 # than 7 was in the freelist, it triggered an assertion failure
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
551 try:
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
552 d = {'a': 1/0, 'b': None, 'c': None, 'd': None, 'e': None,
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
553 'f': None, 'g': None, 'h': None}
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
554 except ZeroDivisionError:
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
555 pass
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
556 d = {}
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
557
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
558 def test_container_iterator(self):
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50424
diff changeset
559 # Bug #3680: tp_traverse was not implemented for dictiter objects
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
560 class C(object):
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
561 pass
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
562 iterators = (dict.iteritems, dict.itervalues, dict.iterkeys)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
563 for i in iterators:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
564 obj = C()
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
565 ref = weakref.ref(obj)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
566 container = {obj: 1}
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
567 obj.x = i(container)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
568 del obj, container
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
569 gc.collect()
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
570 self.assert_(ref() is None, "Cycle was not collected")
48209
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 45837
diff changeset
571
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
572 def _not_tracked(self, t):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
573 # Nested containers can take several collections to untrack
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
574 gc.collect()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
575 gc.collect()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
576 self.assertFalse(gc.is_tracked(t), t)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
577
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
578 def _tracked(self, t):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
579 self.assertTrue(gc.is_tracked(t), t)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
580 gc.collect()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
581 gc.collect()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
582 self.assertTrue(gc.is_tracked(t), t)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
583
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
584 def test_track_literals(self):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
585 # Test GC-optimization of dict literals
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
586 x, y, z, w = 1.5, "a", (1, None), []
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
587
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
588 self._not_tracked({})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
589 self._not_tracked({x:(), y:x, z:1})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
590 self._not_tracked({1: "a", "b": 2})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
591 self._not_tracked({1: 2, (None, True, False, ()): int})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
592 self._not_tracked({1: object()})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
593
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
594 # Dicts with mutable elements are always tracked, even if those
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
595 # elements are not tracked right now.
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
596 self._tracked({1: []})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
597 self._tracked({1: ([],)})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
598 self._tracked({1: {}})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
599 self._tracked({1: set()})
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
600
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
601 def test_track_dynamic(self):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
602 # Test GC-optimization of dynamically-created dicts
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
603 class MyObject(object):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
604 pass
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
605 x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
606
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
607 d = dict()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
608 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
609 d[1] = "a"
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
610 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
611 d[y] = 2
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
612 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
613 d[z] = 3
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
614 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
615 self._not_tracked(d.copy())
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
616 d[4] = w
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
617 self._tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
618 self._tracked(d.copy())
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
619 d[4] = None
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
620 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
621 self._not_tracked(d.copy())
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
622
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
623 # dd isn't tracked right now, but it may mutate and therefore d
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
624 # which contains it must be tracked.
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
625 d = dict()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
626 dd = dict()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
627 d[1] = dd
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
628 self._not_tracked(dd)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
629 self._tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
630 dd[1] = d
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
631 self._tracked(dd)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
632
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
633 d = dict.fromkeys([x, y, z])
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
634 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
635 dd = dict()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
636 dd.update(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
637 self._not_tracked(dd)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
638 d = dict.fromkeys([x, y, z, o])
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
639 self._tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
640 dd = dict()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
641 dd.update(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
642 self._tracked(dd)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
643
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
644 d = dict(x=x, y=y, z=z)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
645 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
646 d = dict(x=x, y=y, z=z, w=w)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
647 self._tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
648 d = dict()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
649 d.update(x=x, y=y, z=z)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
650 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
651 d.update(w=w)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
652 self._tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
653
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
654 d = dict([(x, y), (z, 1)])
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
655 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
656 d = dict([(x, y), (z, w)])
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
657 self._tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
658 d = dict()
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
659 d.update([(x, y), (z, 1)])
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
660 self._not_tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
661 d.update([(x, y), (z, w)])
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
662 self._tracked(d)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
663
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
664 def test_track_subtypes(self):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
665 # Dict subtypes are always tracked
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
666 class MyDict(dict):
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
667 pass
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
668 self._tracked(MyDict())
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50522
diff changeset
669
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 33631
diff changeset
670
37408
a60a9443e8da Use absolute imports
Neal Norwitz <nnorwitz@gmail.com>
parents: 36656
diff changeset
671 from test import mapping_tests
33631
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
672
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
673 class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
674 type2test = dict
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
675
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
676 class Dict(dict):
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
677 pass
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
678
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
679 class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
680 type2test = Dict
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
681
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
682 def test_main():
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
683 test_support.run_unittest(
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
684 DictTest,
33631
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
685 GeneralMappingTests,
655f74ea0f31 Expand scope to include general mapping protocol tests.
Raymond Hettinger <python@rcn.com>
parents: 33630
diff changeset
686 SubclassMappingTests,
33630
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
687 )
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
688
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
689 if __name__ == "__main__":
0f0ddb4cb043 Add missing test_dict.py from patch #736962.
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
690 test_main()