blob: 43709356ca27976725988276daa43230cb8d8828 [file] [log] [blame]
Tim Petersa814db52001-05-14 07:05:58 +00001import pprint
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002import test.support
Fred Drake43913dd2001-05-14 17:41:20 +00003import unittest
Christian Heimes969fe572008-01-25 11:23:10 +00004import test.test_set
Raymond Hettingera7da1662009-11-19 01:07:05 +00005import random
Tim Petersa814db52001-05-14 07:05:58 +00006
Walter Dörwald7a7ede52003-12-03 20:15:28 +00007# list, tuple and dict subclasses that do or don't overwrite __repr__
8class list2(list):
9 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000010
Walter Dörwald7a7ede52003-12-03 20:15:28 +000011class list3(list):
12 def __repr__(self):
13 return list.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000014
Walter Dörwald7a7ede52003-12-03 20:15:28 +000015class tuple2(tuple):
16 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000017
Walter Dörwald7a7ede52003-12-03 20:15:28 +000018class tuple3(tuple):
19 def __repr__(self):
20 return tuple.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000021
Walter Dörwald7a7ede52003-12-03 20:15:28 +000022class dict2(dict):
23 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000024
Walter Dörwald7a7ede52003-12-03 20:15:28 +000025class dict3(dict):
26 def __repr__(self):
27 return dict.__repr__(self)
Tim Petersa814db52001-05-14 07:05:58 +000028
Raymond Hettingera7da1662009-11-19 01:07:05 +000029class Unorderable:
30 def __repr__(self):
31 return str(id(self))
32
Fred Drake43913dd2001-05-14 17:41:20 +000033class QueryTestCase(unittest.TestCase):
Tim Petersa814db52001-05-14 07:05:58 +000034
Fred Drake43913dd2001-05-14 17:41:20 +000035 def setUp(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000036 self.a = list(range(100))
37 self.b = list(range(200))
Fred Drake43913dd2001-05-14 17:41:20 +000038 self.a[-12] = self.b
Tim Petersa814db52001-05-14 07:05:58 +000039
Fred Drake43913dd2001-05-14 17:41:20 +000040 def test_basic(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000041 # Verify .isrecursive() and .isreadable() w/o recursion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000042 verify = self.assertTrue
Fred Drakeb456e4f2002-12-31 07:16:16 +000043 pp = pprint.PrettyPrinter()
Walter Dörwald5de48bd2007-06-11 21:38:39 +000044 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
Fred Drake43913dd2001-05-14 17:41:20 +000045 self.a, self.b):
Fred Drakeb456e4f2002-12-31 07:16:16 +000046 # module-level convenience functions
Fred Drake43913dd2001-05-14 17:41:20 +000047 verify(not pprint.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000048 "expected not isrecursive for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000049 verify(pprint.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000050 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000051 # PrettyPrinter methods
52 verify(not pp.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000053 "expected not isrecursive for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000054 verify(pp.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000055 "expected isreadable for %r" % (safe,))
Tim Petersa814db52001-05-14 07:05:58 +000056
Fred Drake43913dd2001-05-14 17:41:20 +000057 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000058 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +000059 # Tie a knot.
60 self.b[67] = self.a
61 # Messy dict.
62 self.d = {}
63 self.d[0] = self.d[1] = self.d[2] = self.d
Tim Petersa814db52001-05-14 07:05:58 +000064
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000065 verify = self.assertTrue
Fred Drakeb456e4f2002-12-31 07:16:16 +000066 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +000067
Fred Drake43913dd2001-05-14 17:41:20 +000068 for icky in self.a, self.b, self.d, (self.d, self.d):
69 verify(pprint.isrecursive(icky), "expected isrecursive")
70 verify(not pprint.isreadable(icky), "expected not isreadable")
Fred Drakeb456e4f2002-12-31 07:16:16 +000071 verify(pp.isrecursive(icky), "expected isrecursive")
72 verify(not pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +000073
74 # Break the cycles.
75 self.d.clear()
76 del self.a[:]
77 del self.b[:]
78
79 for safe in self.a, self.b, self.d, (self.d, self.d):
Fred Drakeb456e4f2002-12-31 07:16:16 +000080 # module-level convenience functions
Fred Drake43913dd2001-05-14 17:41:20 +000081 verify(not pprint.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000082 "expected not isrecursive for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000083 verify(pprint.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000084 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000085 # PrettyPrinter methods
86 verify(not pp.isrecursive(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000087 "expected not isrecursive for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000088 verify(pp.isreadable(safe),
Walter Dörwald70a6b492004-02-12 17:35:32 +000089 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +000090
91 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000092 # Not recursive but not readable anyway
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000093 verify = self.assertTrue
Fred Drakeb456e4f2002-12-31 07:16:16 +000094 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +000095 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +000096 # module-level convenience functions
Fred Drake43913dd2001-05-14 17:41:20 +000097 verify(not pprint.isrecursive(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +000098 "expected not isrecursive for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +000099 verify(not pprint.isreadable(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +0000100 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000101 # PrettyPrinter methods
102 verify(not pp.isrecursive(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +0000103 "expected not isrecursive for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000104 verify(not pp.isreadable(unreadable),
Walter Dörwald70a6b492004-02-12 17:35:32 +0000105 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000106
Tim Peters95b3f782001-05-14 18:39:41 +0000107 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000108 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000109 # For those the result should be the same as repr().
110 # Ahem. The docs don't say anything about that -- this appears to
111 # be testing an implementation quirk. Starting in Python 2.5, it's
112 # not true for dicts: pprint always sorts dicts by key now; before,
113 # it sorted a dict display if and only if the display required
114 # multiple lines. For that reason, dicts with more than one element
115 # aren't tested here.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000116 verify = self.assertTrue
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000117 for simple in (0, 0, 0+0j, 0.0, "", b"",
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000118 (), tuple2(), tuple3(),
119 [], list2(), list3(),
120 {}, dict2(), dict3(),
121 verify, pprint,
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000122 -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
Benjamin Petersond23f8222009-04-05 19:13:16 +0000123 (1,2), [3,4], {5: 6},
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000124 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
125 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
Benjamin Petersond23f8222009-04-05 19:13:16 +0000126 dict2({5: 6}), dict3({5: 6}),
Tim Peters95b3f782001-05-14 18:39:41 +0000127 range(10, -11, -1)
128 ):
129 native = repr(simple)
130 for function in "pformat", "saferepr":
131 f = getattr(pprint, function)
132 got = f(simple)
133 verify(native == got, "expected %s got %s from pprint.%s" %
134 (native, got, function))
Fred Drake43913dd2001-05-14 17:41:20 +0000135
Barry Warsaw00859c02001-11-28 05:49:39 +0000136 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000137 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000138 o = {'RPM_cal': 0,
139 'RPM_cal2': 48059,
140 'Speed_cal': 0,
141 'controldesk_runtime_us': 0,
142 'main_code_runtime_us': 0,
143 'read_io_runtime_us': 0,
144 'write_io_runtime_us': 43690}
145 exp = """\
146{'RPM_cal': 0,
147 'RPM_cal2': 48059,
148 'Speed_cal': 0,
149 'controldesk_runtime_us': 0,
150 'main_code_runtime_us': 0,
151 'read_io_runtime_us': 0,
152 'write_io_runtime_us': 43690}"""
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000153 for type in [dict, dict2]:
154 self.assertEqual(pprint.pformat(type(o)), exp)
155
156 o = range(100)
157 exp = '[%s]' % ',\n '.join(map(str, o))
158 for type in [list, list2]:
159 self.assertEqual(pprint.pformat(type(o)), exp)
160
161 o = tuple(range(100))
162 exp = '(%s)' % ',\n '.join(map(str, o))
163 for type in [tuple, tuple2]:
164 self.assertEqual(pprint.pformat(type(o)), exp)
Barry Warsaw00859c02001-11-28 05:49:39 +0000165
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000166 # indent parameter
167 o = range(100)
168 exp = '[ %s]' % ',\n '.join(map(str, o))
169 for type in [list, list2]:
170 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
171
Georg Brandl3ccb7872008-07-16 03:00:45 +0000172 def test_nested_indentations(self):
173 o1 = list(range(10))
174 o2 = dict(first=1, second=2, third=3)
175 o = [o1, o2]
176 expected = """\
177[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
178 { 'first': 1,
179 'second': 2,
180 'third': 3}]"""
181 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
182
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000183 def test_sorted_dict(self):
184 # Starting in Python 2.5, pprint sorts dict displays by key regardless
185 # of how small the dictionary may be.
186 # Before the change, on 32-bit Windows pformat() gave order
187 # 'a', 'c', 'b' here, so this test failed.
188 d = {'a': 1, 'b': 1, 'c': 1}
189 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
190 self.assertEqual(pprint.pformat([d, d]),
191 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
192
193 # The next one is kind of goofy. The sorted order depends on the
194 # alphabetic order of type names: "int" < "str" < "tuple". Before
195 # Python 2.5, this was in the test_same_as_repr() test. It's worth
196 # keeping around for now because it's one of few tests of pprint
197 # against a crazy mix of types.
198 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
199 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
200
Fred Drakeaee113d2002-04-02 05:08:35 +0000201 def test_subclassing(self):
202 o = {'names with spaces': 'should be presented using repr()',
203 'others.should.not.be': 'like.this'}
204 exp = """\
205{'names with spaces': 'should be presented using repr()',
206 others.should.not.be: like.this}"""
207 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
208
Christian Heimes969fe572008-01-25 11:23:10 +0000209 def test_set_reprs(self):
210 self.assertEqual(pprint.pformat(set()), 'set()')
211 self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
212 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
213 self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})')
214 cube_repr_tgt = """\
215{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000216 frozenset({0}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000217 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000218 frozenset({0, 1})}),
219 frozenset({1}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000220 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000221 frozenset({0, 1})}),
222 frozenset({2}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000223 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000224 frozenset({0, 2})}),
225 frozenset({1, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000226 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000227 frozenset({0, 1, 2})}),
228 frozenset({0, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000229 frozenset({0}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000230 frozenset({0, 1, 2})}),
231 frozenset({0, 1}): frozenset({frozenset({0}),
Christian Heimes969fe572008-01-25 11:23:10 +0000232 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000233 frozenset({0, 1, 2})}),
234 frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000235 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000236 frozenset({0, 1})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000237 cube = test.test_set.cube(3)
238 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
239 cubo_repr_tgt = """\
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000240{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
241 2}),
242 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000243 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000244 2})}),
245 frozenset({frozenset({0}),
246 frozenset({0,
247 1})}),
248 frozenset({frozenset(),
249 frozenset({0})}),
250 frozenset({frozenset({2}),
251 frozenset({0,
252 2})})}),
253 frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
254 1}),
255 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000256 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000257 2})}),
258 frozenset({frozenset({0}),
259 frozenset({0,
260 1})}),
261 frozenset({frozenset({1}),
262 frozenset({1,
263 2})}),
264 frozenset({frozenset(),
265 frozenset({1})})}),
266 frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
267 2}),
268 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000269 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000270 2})}),
271 frozenset({frozenset({2}),
272 frozenset({1,
273 2})}),
274 frozenset({frozenset(),
275 frozenset({1})}),
276 frozenset({frozenset({1}),
277 frozenset({0,
278 1})})}),
279 frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
280 2}),
281 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000282 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000283 2})}),
284 frozenset({frozenset({1}),
285 frozenset({1,
286 2})}),
287 frozenset({frozenset({2}),
288 frozenset({0,
289 2})}),
290 frozenset({frozenset(),
291 frozenset({2})})}),
292 frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
293 frozenset({0,
294 1})}),
295 frozenset({frozenset({0}),
296 frozenset({0,
297 2})}),
298 frozenset({frozenset(),
299 frozenset({1})}),
300 frozenset({frozenset(),
301 frozenset({2})})}),
302 frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
303 frozenset({0})}),
304 frozenset({frozenset({1}),
305 frozenset({1,
306 2})}),
307 frozenset({frozenset(),
308 frozenset({2})}),
309 frozenset({frozenset({1}),
310 frozenset({0,
311 1})})}),
312 frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
313 frozenset({1,
314 2})}),
315 frozenset({frozenset(),
316 frozenset({0})}),
317 frozenset({frozenset(),
318 frozenset({1})}),
319 frozenset({frozenset({2}),
320 frozenset({0,
321 2})})}),
322 frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
323 2}),
324 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000325 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000326 2})}),
327 frozenset({frozenset({0,
328 2}),
329 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000330 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000331 2})}),
332 frozenset({frozenset({0}),
333 frozenset({0,
334 1})}),
335 frozenset({frozenset({1}),
336 frozenset({0,
337 1})})}),
338 frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
339 frozenset({0})}),
340 frozenset({frozenset({0,
341 1}),
342 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000343 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000344 2})}),
345 frozenset({frozenset({0}),
346 frozenset({0,
347 2})}),
348 frozenset({frozenset({1}),
349 frozenset({0,
350 1})})}),
351 frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
352 2}),
353 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000354 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000355 2})}),
356 frozenset({frozenset({2}),
357 frozenset({1,
358 2})}),
359 frozenset({frozenset({0}),
360 frozenset({0,
361 2})}),
362 frozenset({frozenset(),
363 frozenset({2})})}),
364 frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
365 2}),
366 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000367 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000368 2})}),
369 frozenset({frozenset({0,
370 1}),
371 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000372 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000373 2})}),
374 frozenset({frozenset({0}),
375 frozenset({0,
376 2})}),
377 frozenset({frozenset({2}),
378 frozenset({0,
379 2})})}),
380 frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
381 2}),
382 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000383 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000384 2})}),
385 frozenset({frozenset({0,
386 1}),
387 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000388 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000389 2})}),
390 frozenset({frozenset({2}),
391 frozenset({1,
392 2})}),
393 frozenset({frozenset({1}),
394 frozenset({1,
395 2})})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000396
397 cubo = test.test_set.linegraph(cube)
398 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
399
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000400 def test_depth(self):
401 nested_tuple = (1, (2, (3, (4, (5, 6)))))
402 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
403 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
404 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
405 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
406 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
407
408 lv1_tuple = '(1, (...))'
409 lv1_dict = '{1: {...}}'
410 lv1_list = '[1, [...]]'
411 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
412 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
413 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
414
Raymond Hettingera7da1662009-11-19 01:07:05 +0000415 def test_sort_unorderable_values(self):
416 # Issue 3976: sorted pprints fail for unorderable values.
417 n = 20
418 keys = [Unorderable() for i in range(n)]
419 random.shuffle(keys)
420 skeys = sorted(keys, key=id)
421 clean = lambda s: s.replace(' ', '').replace('\n','')
422
423 self.assertEqual(clean(pprint.pformat(set(keys))),
424 '{' + ','.join(map(repr, skeys)) + '}')
425 self.assertEqual(clean(pprint.pformat(frozenset(keys))),
426 'frozenset({' + ','.join(map(repr, skeys)) + '})')
427 self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
428 '{' + ','.join('%r:None' % k for k in skeys) + '}')
Fred Drakeaee113d2002-04-02 05:08:35 +0000429
430class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000431
Fred Drakeaee113d2002-04-02 05:08:35 +0000432 def format(self, object, context, maxlevels, level):
433 if isinstance(object, str):
434 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000435 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +0000436 else:
437 return object, 0, 0
438 else:
439 return pprint.PrettyPrinter.format(
440 self, object, context, maxlevels, level)
441
442
Fred Drake2e2be372001-09-20 21:33:42 +0000443def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000444 test.support.run_unittest(QueryTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000445
446
447if __name__ == "__main__":
448 test_main()