1 """Unit tests for collections.defaultdict."""
7 from test
import test_support
9 from collections
import defaultdict
14 class TestDefaultDict(unittest
.TestCase
):
18 self
.assertEqual(d1
.default_factory
, None)
19 d1
.default_factory
= list
21 self
.assertEqual(d1
, {12: [42]})
23 self
.assertEqual(d1
, {12: [42, 24]})
26 self
.assertEqual(d1
, {12: [42, 24], 13: [], 14: []})
27 self
.assertTrue(d1
[12] is not d1
[13] is not d1
[14])
28 d2
= defaultdict(list, foo
=1, bar
=2)
29 self
.assertEqual(d2
.default_factory
, list)
30 self
.assertEqual(d2
, {"foo": 1, "bar": 2})
31 self
.assertEqual(d2
["foo"], 1)
32 self
.assertEqual(d2
["bar"], 2)
33 self
.assertEqual(d2
[42], [])
34 self
.assertTrue("foo" in d2
)
35 self
.assertTrue("foo" in d2
.keys())
36 self
.assertTrue("bar" in d2
)
37 self
.assertTrue("bar" in d2
.keys())
38 self
.assertTrue(42 in d2
)
39 self
.assertTrue(42 in d2
.keys())
40 self
.assertTrue(12 not in d2
)
41 self
.assertTrue(12 not in d2
.keys())
42 d2
.default_factory
= None
43 self
.assertEqual(d2
.default_factory
, None)
47 self
.assertEqual(err
.args
, (15,))
49 self
.fail("d2[15] didn't raise KeyError")
50 self
.assertRaises(TypeError, defaultdict
, 1)
52 def test_missing(self
):
54 self
.assertRaises(KeyError, d1
.__missing__
, 42)
55 d1
.default_factory
= list
56 self
.assertEqual(d1
.__missing__(42), [])
60 self
.assertEqual(d1
.default_factory
, None)
61 self
.assertEqual(repr(d1
), "defaultdict(None, {})")
62 self
.assertEqual(eval(repr(d1
)), d1
)
64 self
.assertEqual(repr(d1
), "defaultdict(None, {11: 41})")
66 self
.assertEqual(d2
.default_factory
, int)
68 self
.assertEqual(repr(d2
), "defaultdict(<type 'int'>, {12: 42})")
71 self
.assertTrue(d3
.default_factory
is foo
)
73 self
.assertEqual(repr(d3
), "defaultdict(%s, {13: 43})" % repr(foo
))
78 d2
= defaultdict(foo
, {1: 2})
79 # NOTE: We can't use tempfile.[Named]TemporaryFile since this
80 # code must exercise the tp_print C code, which only gets
81 # invoked for *real* files.
82 tfn
= tempfile
.mktemp()
89 self
.assertEqual(f
.readline(), repr(d1
) + "\n")
90 self
.assertEqual(f
.readline(), repr(d2
) + "\n")
99 self
.assertEqual(type(d2
), defaultdict
)
100 self
.assertEqual(d2
.default_factory
, None)
101 self
.assertEqual(d2
, {})
102 d1
.default_factory
= list
104 self
.assertEqual(type(d3
), defaultdict
)
105 self
.assertEqual(d3
.default_factory
, list)
106 self
.assertEqual(d3
, {})
109 self
.assertEqual(type(d4
), defaultdict
)
110 self
.assertEqual(d4
.default_factory
, list)
111 self
.assertEqual(d4
, {42: []})
113 self
.assertEqual(d4
, {42: [], 12: []})
115 # Issue 6637: Copy fails for empty default dict
119 self
.assertEqual(e
['a'], 42)
121 def test_shallow_copy(self
):
122 d1
= defaultdict(foobar
, {1: 1})
124 self
.assertEqual(d2
.default_factory
, foobar
)
125 self
.assertEqual(d2
, d1
)
126 d1
.default_factory
= list
128 self
.assertEqual(d2
.default_factory
, list)
129 self
.assertEqual(d2
, d1
)
131 def test_deep_copy(self
):
132 d1
= defaultdict(foobar
, {1: [1]})
133 d2
= copy
.deepcopy(d1
)
134 self
.assertEqual(d2
.default_factory
, foobar
)
135 self
.assertEqual(d2
, d1
)
136 self
.assertTrue(d1
[1] is not d2
[1])
137 d1
.default_factory
= list
138 d2
= copy
.deepcopy(d1
)
139 self
.assertEqual(d2
.default_factory
, list)
140 self
.assertEqual(d2
, d1
)
142 def test_keyerror_without_factory(self
):
146 except KeyError, err
:
147 self
.assertEqual(err
.args
[0], (1,))
149 self
.fail("expected KeyError")
151 def test_recursive_repr(self
):
152 # Issue2045: stack overflow when default_factory is a bound method
153 class sub(defaultdict
):
155 self
.default_factory
= self
._factory
159 self
.assertTrue(repr(d
).startswith(
160 "defaultdict(<bound method sub._factory of defaultdict(..."))
162 # NOTE: printing a subclass of a builtin type does not call its
163 # tp_print slot. So this part is essentially the same test as above.
164 tfn
= tempfile
.mktemp()
176 test_support
.run_unittest(TestDefaultDict
)
178 if __name__
== "__main__":