annotate Lib/test/test_defaultdict.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 e94af0c7aa12
children dd4da75ebc97 6768b2350c57
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: []})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 self.assert_(d1[12] is not d1[13] is not d1[14])
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], [])
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 self.assert_("foo" in d2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 self.assert_("foo" in d2.keys())
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 self.assert_("bar" in d2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 self.assert_("bar" in d2.keys())
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 self.assert_(42 in d2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 self.assert_(42 in d2.keys())
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self.assert_(12 not in d2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 self.assert_(12 not in d2.keys())
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, {})")
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 d1[11] = 41
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 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
64 d2 = defaultdict(int)
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 39206
diff changeset
65 self.assertEqual(d2.default_factory, int)
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 d2[12] = 42
40760
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 39206
diff changeset
67 self.assertEqual(repr(d2), "defaultdict(<type 'int'>, {12: 42})")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def foo(): return 43
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 d3 = defaultdict(foo)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 self.assert_(d3.default_factory is foo)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 d3[13]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 self.assertEqual(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 def test_print(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 d1 = defaultdict()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 def foo(): return 42
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 d2 = defaultdict(foo, {1: 2})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 # NOTE: We can't use tempfile.[Named]TemporaryFile since this
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 # code must exercise the tp_print C code, which only gets
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # invoked for *real* files.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 tfn = tempfile.mktemp()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 f = open(tfn, "w+")
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 print >>f, d1
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 print >>f, d2
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 f.seek(0)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 self.assertEqual(f.readline(), repr(d1) + "\n")
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 self.assertEqual(f.readline(), repr(d2) + "\n")
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 finally:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 f.close()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 finally:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 os.remove(tfn)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 def test_copy(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 d1 = defaultdict()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 d2 = d1.copy()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 self.assertEqual(type(d2), defaultdict)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 self.assertEqual(d2.default_factory, None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 self.assertEqual(d2, {})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 d3 = d1.copy()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 self.assertEqual(type(d3), defaultdict)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.assertEqual(d3.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.assertEqual(d3, {})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 d1[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 d4 = d1.copy()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 self.assertEqual(type(d4), defaultdict)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 self.assertEqual(d4.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self.assertEqual(d4, {42: []})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 d4[12]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 self.assertEqual(d4, {42: [], 12: []})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 def test_shallow_copy(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 d1 = defaultdict(foobar, {1: 1})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 d2 = copy.copy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 self.assertEqual(d2.default_factory, foobar)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 self.assertEqual(d2, d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 d2 = copy.copy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 self.assertEqual(d2.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 self.assertEqual(d2, d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 def test_deep_copy(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 d1 = defaultdict(foobar, {1: [1]})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 d2 = copy.deepcopy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 self.assertEqual(d2.default_factory, foobar)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 self.assertEqual(d2, d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 self.assert_(d1[1] is not d2[1])
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 d1.default_factory = list
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 d2 = copy.deepcopy(d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 self.assertEqual(d2.default_factory, list)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 self.assertEqual(d2, d1)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
41021
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
135 def test_keyerror_without_factory(self):
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
136 d1 = defaultdict()
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
137 try:
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
138 d1[(1,)]
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
139 except KeyError, err:
41617
615795d52b9f Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents: 41021
diff changeset
140 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
141 else:
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
142 self.fail("expected KeyError")
09d08fcb8f7b A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents: 40760
diff changeset
143
45231
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
144 def test_recursive_repr(self):
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
145 # Issue2045: stack overflow when default_factory is a bound method
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
146 class sub(defaultdict):
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
147 def __init__(self):
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
148 self.default_factory = self._factory
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
149 def _factory(self):
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
150 return []
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
151 d = sub()
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
152 self.assert_(repr(d).startswith(
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
153 "defaultdict(<bound method sub._factory of defaultdict(..."))
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
154
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
155 # NOTE: printing a subclass of a builtin type does not call its
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
156 # tp_print slot. So this part is essentially the same test as above.
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
157 tfn = tempfile.mktemp()
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
158 try:
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
159 f = open(tfn, "w+")
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
160 try:
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
161 print >>f, d
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
162 finally:
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
163 f.close()
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
164 finally:
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
165 os.remove(tfn)
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41617
diff changeset
166
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
39206
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
168 def test_main():
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
169 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
170
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 if __name__ == "__main__":
39206
025c27b812f6 Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
172 test_main()