Catch situations where currentframe() returns None. See SF patch #1447410, this is...
[python.git] / Lib / test / test_pprint.py
blob27d6b5208470c13cbae54434badeea55f1168695
1 import pprint
2 import test.test_support
3 import unittest
5 try:
6 uni = unicode
7 except NameError:
8 def uni(x):
9 return x
11 # list, tuple and dict subclasses that do or don't overwrite __repr__
12 class list2(list):
13 pass
14 class list3(list):
15 def __repr__(self):
16 return list.__repr__(self)
17 class tuple2(tuple):
18 pass
19 class tuple3(tuple):
20 def __repr__(self):
21 return tuple.__repr__(self)
22 class dict2(dict):
23 pass
24 class dict3(dict):
25 def __repr__(self):
26 return dict.__repr__(self)
28 class QueryTestCase(unittest.TestCase):
30 def setUp(self):
31 self.a = range(100)
32 self.b = range(200)
33 self.a[-12] = self.b
35 def test_basic(self):
36 # Verify .isrecursive() and .isreadable() w/o recursion
37 verify = self.assert_
38 pp = pprint.PrettyPrinter()
39 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
40 self.a, self.b):
41 # module-level convenience functions
42 verify(not pprint.isrecursive(safe),
43 "expected not isrecursive for %r" % (safe,))
44 verify(pprint.isreadable(safe),
45 "expected isreadable for %r" % (safe,))
46 # PrettyPrinter methods
47 verify(not pp.isrecursive(safe),
48 "expected not isrecursive for %r" % (safe,))
49 verify(pp.isreadable(safe),
50 "expected isreadable for %r" % (safe,))
52 def test_knotted(self):
53 # Verify .isrecursive() and .isreadable() w/ recursion
54 # Tie a knot.
55 self.b[67] = self.a
56 # Messy dict.
57 self.d = {}
58 self.d[0] = self.d[1] = self.d[2] = self.d
60 verify = self.assert_
61 pp = pprint.PrettyPrinter()
63 for icky in self.a, self.b, self.d, (self.d, self.d):
64 verify(pprint.isrecursive(icky), "expected isrecursive")
65 verify(not pprint.isreadable(icky), "expected not isreadable")
66 verify(pp.isrecursive(icky), "expected isrecursive")
67 verify(not pp.isreadable(icky), "expected not isreadable")
69 # Break the cycles.
70 self.d.clear()
71 del self.a[:]
72 del self.b[:]
74 for safe in self.a, self.b, self.d, (self.d, self.d):
75 # module-level convenience functions
76 verify(not pprint.isrecursive(safe),
77 "expected not isrecursive for %r" % (safe,))
78 verify(pprint.isreadable(safe),
79 "expected isreadable for %r" % (safe,))
80 # PrettyPrinter methods
81 verify(not pp.isrecursive(safe),
82 "expected not isrecursive for %r" % (safe,))
83 verify(pp.isreadable(safe),
84 "expected isreadable for %r" % (safe,))
86 def test_unreadable(self):
87 # Not recursive but not readable anyway
88 verify = self.assert_
89 pp = pprint.PrettyPrinter()
90 for unreadable in type(3), pprint, pprint.isrecursive:
91 # module-level convenience functions
92 verify(not pprint.isrecursive(unreadable),
93 "expected not isrecursive for %r" % (unreadable,))
94 verify(not pprint.isreadable(unreadable),
95 "expected not isreadable for %r" % (unreadable,))
96 # PrettyPrinter methods
97 verify(not pp.isrecursive(unreadable),
98 "expected not isrecursive for %r" % (unreadable,))
99 verify(not pp.isreadable(unreadable),
100 "expected not isreadable for %r" % (unreadable,))
102 def test_same_as_repr(self):
103 # Simple objects, small containers and classes that overwrite __repr__
104 # For those the result should be the same as repr()
105 verify = self.assert_
106 for simple in (0, 0L, 0+0j, 0.0, "", uni(""),
107 (), tuple2(), tuple3(),
108 [], list2(), list3(),
109 {}, dict2(), dict3(),
110 verify, pprint,
111 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
112 (1,2), [3,4], {5: 6, 7: 8},
113 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
114 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
115 {5: 6, 7: 8}, dict2({5: 6, 7: 8}), dict3({5: 6, 7: 8}),
116 dict3([(x,x) for x in range(100)]),
117 {"xy\tab\n": (3,), 5: [[]], (): {}},
118 range(10, -11, -1)
120 native = repr(simple)
121 for function in "pformat", "saferepr":
122 f = getattr(pprint, function)
123 got = f(simple)
124 verify(native == got, "expected %s got %s from pprint.%s" %
125 (native, got, function))
127 def test_basic_line_wrap(self):
128 # verify basic line-wrapping operation
129 o = {'RPM_cal': 0,
130 'RPM_cal2': 48059,
131 'Speed_cal': 0,
132 'controldesk_runtime_us': 0,
133 'main_code_runtime_us': 0,
134 'read_io_runtime_us': 0,
135 'write_io_runtime_us': 43690}
136 exp = """\
137 {'RPM_cal': 0,
138 'RPM_cal2': 48059,
139 'Speed_cal': 0,
140 'controldesk_runtime_us': 0,
141 'main_code_runtime_us': 0,
142 'read_io_runtime_us': 0,
143 'write_io_runtime_us': 43690}"""
144 for type in [dict, dict2]:
145 self.assertEqual(pprint.pformat(type(o)), exp)
147 o = range(100)
148 exp = '[%s]' % ',\n '.join(map(str, o))
149 for type in [list, list2]:
150 self.assertEqual(pprint.pformat(type(o)), exp)
152 o = tuple(range(100))
153 exp = '(%s)' % ',\n '.join(map(str, o))
154 for type in [tuple, tuple2]:
155 self.assertEqual(pprint.pformat(type(o)), exp)
157 # indent parameter
158 o = range(100)
159 exp = '[ %s]' % ',\n '.join(map(str, o))
160 for type in [list, list2]:
161 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
163 def test_subclassing(self):
164 o = {'names with spaces': 'should be presented using repr()',
165 'others.should.not.be': 'like.this'}
166 exp = """\
167 {'names with spaces': 'should be presented using repr()',
168 others.should.not.be: like.this}"""
169 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
172 class DottedPrettyPrinter(pprint.PrettyPrinter):
174 def format(self, object, context, maxlevels, level):
175 if isinstance(object, str):
176 if ' ' in object:
177 return repr(object), 1, 0
178 else:
179 return object, 0, 0
180 else:
181 return pprint.PrettyPrinter.format(
182 self, object, context, maxlevels, level)
185 def test_main():
186 test.test_support.run_unittest(QueryTestCase)
189 if __name__ == "__main__":
190 test_main()