annotate Lib/test/test_defaultdict.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents a8deeb549e1a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Unit tests for collections.defaultdict."""
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import os
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 import copy
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import tempfile
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import unittest
39206
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
7 from test import test_support
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 from collections import defaultdict
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def foobar():
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 return list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 class TestDefaultDict(unittest.TestCase):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 def test_basic(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 d1 = defaultdict()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 self.assertEqual(d1.default_factory, None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 d1[12].append(42)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 self.assertEqual(d1, {12: [42]})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 d1[12].append(24)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 self.assertEqual(d1, {12: [42, 24]})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 d1[13]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 d1[14]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 self.assertEqual(d1, {12: [42, 24], 13: [], 14: []})
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45231
diff changeset
27 self.assertTrue(d1[12] is not d1[13] is not d1[14])
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 d2 = defaultdict(list, foo=1, bar=2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 self.assertEqual(d2.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 self.assertEqual(d2, {"foo": 1, "bar": 2})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 self.assertEqual(d2["foo"], 1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 self.assertEqual(d2["bar"], 2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 self.assertEqual(d2[42], [])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
34 self.assertIn("foo", d2)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
35 self.assertIn("foo", d2.keys())
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
36 self.assertIn("bar", d2)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
37 self.assertIn("bar", d2.keys())
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
38 self.assertIn(42, d2)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
39 self.assertIn(42, d2.keys())
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
40 self.assertNotIn(12, d2)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55535
diff changeset
41 self.assertNotIn(12, d2.keys())
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 d2.default_factory = None
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 self.assertEqual(d2.default_factory, None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 d2[15]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 except KeyError, err:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 self.assertEqual(err.args, (15,))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 else:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 self.fail("d2[15] didn't raise KeyError")
40760
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 39206
diff changeset
50 self.assertRaises(TypeError, defaultdict, 1)
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 def test_missing(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 d1 = defaultdict()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 self.assertRaises(KeyError, d1.__missing__, 42)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 self.assertEqual(d1.__missing__(42), [])
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def test_repr(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 d1 = defaultdict()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 self.assertEqual(d1.default_factory, None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 self.assertEqual(repr(d1), "defaultdict(None, {})")
55534
dd4da75ebc97 Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 45231
diff changeset
62 self.assertEqual(eval(repr(d1)), d1)
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 d1[11] = 41
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 self.assertEqual(repr(d1), "defaultdict(None, {11: 41})")
40760
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 39206
diff changeset
65 d2 = defaultdict(int)
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 39206
diff changeset
66 self.assertEqual(d2.default_factory, int)
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 d2[12] = 42
40760
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 39206
diff changeset
68 self.assertEqual(repr(d2), "defaultdict(<type 'int'>, {12: 42})")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 def foo(): return 43
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 d3 = defaultdict(foo)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45231
diff changeset
71 self.assertTrue(d3.default_factory is foo)
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 d3[13]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 self.assertEqual(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def test_print(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 d1 = defaultdict()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def foo(): return 42
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 d2 = defaultdict(foo, {1: 2})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 # NOTE: We can't use tempfile.[Named]TemporaryFile since this
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # code must exercise the tp_print C code, which only gets
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # invoked for *real* files.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 tfn = tempfile.mktemp()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 f = open(tfn, "w+")
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 print >>f, d1
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 print >>f, d2
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 f.seek(0)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 self.assertEqual(f.readline(), repr(d1) + "\n")
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 self.assertEqual(f.readline(), repr(d2) + "\n")
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 finally:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 f.close()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 finally:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 os.remove(tfn)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def test_copy(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 d1 = defaultdict()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 d2 = d1.copy()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 self.assertEqual(type(d2), defaultdict)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 self.assertEqual(d2.default_factory, None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 self.assertEqual(d2, {})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 d3 = d1.copy()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.assertEqual(type(d3), defaultdict)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.assertEqual(d3.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 self.assertEqual(d3, {})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 d1[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 d4 = d1.copy()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 self.assertEqual(type(d4), defaultdict)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self.assertEqual(d4.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 self.assertEqual(d4, {42: []})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 d4[12]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 self.assertEqual(d4, {42: [], 12: []})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
55534
dd4da75ebc97 Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 45231
diff changeset
115 # Issue 6637: Copy fails for empty default dict
dd4da75ebc97 Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 45231
diff changeset
116 d = defaultdict()
dd4da75ebc97 Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 45231
diff changeset
117 d['a'] = 42
dd4da75ebc97 Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 45231
diff changeset
118 e = d.copy()
dd4da75ebc97 Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 45231
diff changeset
119 self.assertEqual(e['a'], 42)
dd4da75ebc97 Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 45231
diff changeset
120
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def test_shallow_copy(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 d1 = defaultdict(foobar, {1: 1})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 d2 = copy.copy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 self.assertEqual(d2.default_factory, foobar)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 self.assertEqual(d2, d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 d2 = copy.copy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 self.assertEqual(d2.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 self.assertEqual(d2, d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 def test_deep_copy(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 d1 = defaultdict(foobar, {1: [1]})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 d2 = copy.deepcopy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 self.assertEqual(d2.default_factory, foobar)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 self.assertEqual(d2, d1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45231
diff changeset
136 self.assertTrue(d1[1] is not d2[1])
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 d2 = copy.deepcopy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 self.assertEqual(d2.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 self.assertEqual(d2, d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
41021
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
142 def test_keyerror_without_factory(self):
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
143 d1 = defaultdict()
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
144 try:
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
145 d1[(1,)]
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
146 except KeyError, err:
41617
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 41021
diff changeset
147 self.assertEqual(err.args[0], (1,))
41021
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
148 else:
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
149 self.fail("expected KeyError")
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
150
45232
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
151 def test_recursive_repr(self):
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
152 # Issue2045: stack overflow when default_factory is a bound method
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
153 class sub(defaultdict):
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
154 def __init__(self):
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
155 self.default_factory = self._factory
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
156 def _factory(self):
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
157 return []
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
158 d = sub()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 45231
diff changeset
159 self.assertTrue(repr(d).startswith(
45232
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
160 "defaultdict(<bound method sub._factory of defaultdict(..."))
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
161
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
162 # NOTE: printing a subclass of a builtin type does not call its
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
163 # tp_print slot. So this part is essentially the same test as above.
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
164 tfn = tempfile.mktemp()
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
165 try:
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
166 f = open(tfn, "w+")
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
167 try:
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
168 print >>f, d
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
169 finally:
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
170 f.close()
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
171 finally:
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
172 os.remove(tfn)
4166945a0cc4 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41021
diff changeset
173
73881
a8deeb549e1a #13531: add a test for defaultdict with a non-callable arg. Patch by Mike Cheng.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
174 def test_callable_arg(self):
a8deeb549e1a #13531: add a test for defaultdict with a non-callable arg. Patch by Mike Cheng.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
175 self.assertRaises(TypeError, defaultdict, {})
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
39206
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
177 def test_main():
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
178 test_support.run_unittest(TestDefaultDict)
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
179
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 if __name__ == "__main__":
39206
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
181 test_main()