blob: 3a798d946ca013ab31fce973389bf489b20c59df [file] [log] [blame]
Antoine Pitrou64c16c32013-03-23 20:30:39 +01001# -*- coding: utf-8 -*-
2
Tim Petersa814db52001-05-14 07:05:58 +00003import pprint
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004import test.support
Fred Drake43913dd2001-05-14 17:41:20 +00005import unittest
Christian Heimes969fe572008-01-25 11:23:10 +00006import test.test_set
Raymond Hettingera7da1662009-11-19 01:07:05 +00007import random
Raymond Hettingerbad3c882010-09-09 12:31:00 +00008import collections
9import itertools
Serhiy Storchaka87eb4822015-03-24 19:31:50 +020010import types
Tim Petersa814db52001-05-14 07:05:58 +000011
Walter Dörwald7a7ede52003-12-03 20:15:28 +000012# list, tuple and dict subclasses that do or don't overwrite __repr__
13class list2(list):
14 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000015
Walter Dörwald7a7ede52003-12-03 20:15:28 +000016class list3(list):
17 def __repr__(self):
18 return list.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000019
Walter Dörwald7a7ede52003-12-03 20:15:28 +000020class tuple2(tuple):
21 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000022
Walter Dörwald7a7ede52003-12-03 20:15:28 +000023class tuple3(tuple):
24 def __repr__(self):
25 return tuple.__repr__(self)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000026
Serhiy Storchaka51844382013-10-02 11:40:49 +030027class set2(set):
28 pass
29
30class set3(set):
31 def __repr__(self):
32 return set.__repr__(self)
33
34class frozenset2(frozenset):
35 pass
36
37class frozenset3(frozenset):
38 def __repr__(self):
39 return frozenset.__repr__(self)
40
Walter Dörwald7a7ede52003-12-03 20:15:28 +000041class dict2(dict):
42 pass
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000043
Walter Dörwald7a7ede52003-12-03 20:15:28 +000044class dict3(dict):
45 def __repr__(self):
46 return dict.__repr__(self)
Tim Petersa814db52001-05-14 07:05:58 +000047
Raymond Hettingera7da1662009-11-19 01:07:05 +000048class Unorderable:
49 def __repr__(self):
50 return str(id(self))
51
Fred Drake43913dd2001-05-14 17:41:20 +000052class QueryTestCase(unittest.TestCase):
Tim Petersa814db52001-05-14 07:05:58 +000053
Fred Drake43913dd2001-05-14 17:41:20 +000054 def setUp(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000055 self.a = list(range(100))
56 self.b = list(range(200))
Fred Drake43913dd2001-05-14 17:41:20 +000057 self.a[-12] = self.b
Tim Petersa814db52001-05-14 07:05:58 +000058
Fred Drake43913dd2001-05-14 17:41:20 +000059 def test_basic(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000060 # Verify .isrecursive() and .isreadable() w/o recursion
Fred Drakeb456e4f2002-12-31 07:16:16 +000061 pp = pprint.PrettyPrinter()
Walter Dörwald5de48bd2007-06-11 21:38:39 +000062 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, "yaddayadda",
Fred Drake43913dd2001-05-14 17:41:20 +000063 self.a, self.b):
Fred Drakeb456e4f2002-12-31 07:16:16 +000064 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000065 self.assertFalse(pprint.isrecursive(safe),
66 "expected not isrecursive for %r" % (safe,))
67 self.assertTrue(pprint.isreadable(safe),
68 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +000069 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +000070 self.assertFalse(pp.isrecursive(safe),
71 "expected not isrecursive for %r" % (safe,))
72 self.assertTrue(pp.isreadable(safe),
73 "expected isreadable for %r" % (safe,))
Tim Petersa814db52001-05-14 07:05:58 +000074
Fred Drake43913dd2001-05-14 17:41:20 +000075 def test_knotted(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000076 # Verify .isrecursive() and .isreadable() w/ recursion
Fred Drake43913dd2001-05-14 17:41:20 +000077 # Tie a knot.
78 self.b[67] = self.a
79 # Messy dict.
80 self.d = {}
81 self.d[0] = self.d[1] = self.d[2] = self.d
Tim Petersa814db52001-05-14 07:05:58 +000082
Fred Drakeb456e4f2002-12-31 07:16:16 +000083 pp = pprint.PrettyPrinter()
Tim Petersa814db52001-05-14 07:05:58 +000084
Fred Drake43913dd2001-05-14 17:41:20 +000085 for icky in self.a, self.b, self.d, (self.d, self.d):
Ezio Melottib19f43d2010-01-24 20:59:24 +000086 self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
87 self.assertFalse(pprint.isreadable(icky), "expected not isreadable")
88 self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
89 self.assertFalse(pp.isreadable(icky), "expected not isreadable")
Fred Drake43913dd2001-05-14 17:41:20 +000090
91 # Break the cycles.
92 self.d.clear()
93 del self.a[:]
94 del self.b[:]
95
96 for safe in self.a, self.b, self.d, (self.d, self.d):
Fred Drakeb456e4f2002-12-31 07:16:16 +000097 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +000098 self.assertFalse(pprint.isrecursive(safe),
99 "expected not isrecursive for %r" % (safe,))
100 self.assertTrue(pprint.isreadable(safe),
101 "expected isreadable for %r" % (safe,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000102 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000103 self.assertFalse(pp.isrecursive(safe),
104 "expected not isrecursive for %r" % (safe,))
105 self.assertTrue(pp.isreadable(safe),
106 "expected isreadable for %r" % (safe,))
Fred Drake43913dd2001-05-14 17:41:20 +0000107
108 def test_unreadable(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000109 # Not recursive but not readable anyway
Fred Drakeb456e4f2002-12-31 07:16:16 +0000110 pp = pprint.PrettyPrinter()
Fred Drake43913dd2001-05-14 17:41:20 +0000111 for unreadable in type(3), pprint, pprint.isrecursive:
Fred Drakeb456e4f2002-12-31 07:16:16 +0000112 # module-level convenience functions
Ezio Melottib19f43d2010-01-24 20:59:24 +0000113 self.assertFalse(pprint.isrecursive(unreadable),
114 "expected not isrecursive for %r" % (unreadable,))
115 self.assertFalse(pprint.isreadable(unreadable),
116 "expected not isreadable for %r" % (unreadable,))
Fred Drakeb456e4f2002-12-31 07:16:16 +0000117 # PrettyPrinter methods
Ezio Melottib19f43d2010-01-24 20:59:24 +0000118 self.assertFalse(pp.isrecursive(unreadable),
119 "expected not isrecursive for %r" % (unreadable,))
120 self.assertFalse(pp.isreadable(unreadable),
121 "expected not isreadable for %r" % (unreadable,))
Fred Drake43913dd2001-05-14 17:41:20 +0000122
Tim Peters95b3f782001-05-14 18:39:41 +0000123 def test_same_as_repr(self):
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000124 # Simple objects, small containers and classes that overwrite __repr__
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000125 # For those the result should be the same as repr().
126 # Ahem. The docs don't say anything about that -- this appears to
127 # be testing an implementation quirk. Starting in Python 2.5, it's
128 # not true for dicts: pprint always sorts dicts by key now; before,
129 # it sorted a dict display if and only if the display required
130 # multiple lines. For that reason, dicts with more than one element
131 # aren't tested here.
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000132 for simple in (0, 0, 0+0j, 0.0, "", b"",
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000133 (), tuple2(), tuple3(),
134 [], list2(), list3(),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300135 set(), set2(), set3(),
136 frozenset(), frozenset2(), frozenset3(),
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000137 {}, dict2(), dict3(),
Ezio Melottib19f43d2010-01-24 20:59:24 +0000138 self.assertTrue, pprint,
Walter Dörwald5de48bd2007-06-11 21:38:39 +0000139 -6, -6, -6-6j, -1.5, "x", b"x", (3,), [3], {3: 6},
Benjamin Petersond23f8222009-04-05 19:13:16 +0000140 (1,2), [3,4], {5: 6},
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000141 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
142 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
Serhiy Storchaka51844382013-10-02 11:40:49 +0300143 set({7}), set2({7}), set3({7}),
144 frozenset({8}), frozenset2({8}), frozenset3({8}),
Benjamin Petersond23f8222009-04-05 19:13:16 +0000145 dict2({5: 6}), dict3({5: 6}),
Tim Peters95b3f782001-05-14 18:39:41 +0000146 range(10, -11, -1)
147 ):
148 native = repr(simple)
Serhiy Storchaka51844382013-10-02 11:40:49 +0300149 self.assertEqual(pprint.pformat(simple), native)
150 self.assertEqual(pprint.pformat(simple, width=1, indent=0)
151 .replace('\n', ' '), native)
152 self.assertEqual(pprint.saferepr(simple), native)
Fred Drake43913dd2001-05-14 17:41:20 +0000153
Barry Warsaw00859c02001-11-28 05:49:39 +0000154 def test_basic_line_wrap(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000155 # verify basic line-wrapping operation
Barry Warsaw00859c02001-11-28 05:49:39 +0000156 o = {'RPM_cal': 0,
157 'RPM_cal2': 48059,
158 'Speed_cal': 0,
159 'controldesk_runtime_us': 0,
160 'main_code_runtime_us': 0,
161 'read_io_runtime_us': 0,
162 'write_io_runtime_us': 43690}
163 exp = """\
164{'RPM_cal': 0,
165 'RPM_cal2': 48059,
166 'Speed_cal': 0,
167 'controldesk_runtime_us': 0,
168 'main_code_runtime_us': 0,
169 'read_io_runtime_us': 0,
170 'write_io_runtime_us': 43690}"""
Walter Dörwald7a7ede52003-12-03 20:15:28 +0000171 for type in [dict, dict2]:
172 self.assertEqual(pprint.pformat(type(o)), exp)
173
174 o = range(100)
175 exp = '[%s]' % ',\n '.join(map(str, o))
176 for type in [list, list2]:
177 self.assertEqual(pprint.pformat(type(o)), exp)
178
179 o = tuple(range(100))
180 exp = '(%s)' % ',\n '.join(map(str, o))
181 for type in [tuple, tuple2]:
182 self.assertEqual(pprint.pformat(type(o)), exp)
Barry Warsaw00859c02001-11-28 05:49:39 +0000183
Walter Dörwaldc8de4582003-12-03 20:26:05 +0000184 # indent parameter
185 o = range(100)
186 exp = '[ %s]' % ',\n '.join(map(str, o))
187 for type in [list, list2]:
188 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
189
Georg Brandl3ccb7872008-07-16 03:00:45 +0000190 def test_nested_indentations(self):
191 o1 = list(range(10))
192 o2 = dict(first=1, second=2, third=3)
193 o = [o1, o2]
194 expected = """\
195[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200196 {'first': 1, 'second': 2, 'third': 3}]"""
197 self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
198 expected = """\
199[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
Georg Brandl3ccb7872008-07-16 03:00:45 +0000200 { 'first': 1,
201 'second': 2,
202 'third': 3}]"""
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200203 self.assertEqual(pprint.pformat(o, indent=4, width=41), expected)
204
205 def test_width(self):
206 expected = """\
207[[[[[[1, 2, 3],
208 '1 2']]]],
209 {1: [1, 2, 3],
210 2: [12, 34]},
211 'abc def ghi',
212 ('ab cd ef',),
213 set2({1, 23}),
214 [[[[[1, 2, 3],
215 '1 2']]]]]"""
216 o = eval(expected)
217 self.assertEqual(pprint.pformat(o, width=15), expected)
218 self.assertEqual(pprint.pformat(o, width=16), expected)
219 self.assertEqual(pprint.pformat(o, width=25), expected)
220 self.assertEqual(pprint.pformat(o, width=14), """\
221[[[[[[1,
222 2,
223 3],
224 '1 '
225 '2']]]],
226 {1: [1,
227 2,
228 3],
229 2: [12,
230 34]},
231 'abc def '
232 'ghi',
233 ('ab cd '
234 'ef',),
235 set2({1,
236 23}),
237 [[[[[1,
238 2,
239 3],
240 '1 '
241 '2']]]]]""")
Georg Brandl3ccb7872008-07-16 03:00:45 +0000242
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000243 def test_sorted_dict(self):
244 # Starting in Python 2.5, pprint sorts dict displays by key regardless
245 # of how small the dictionary may be.
246 # Before the change, on 32-bit Windows pformat() gave order
247 # 'a', 'c', 'b' here, so this test failed.
248 d = {'a': 1, 'b': 1, 'c': 1}
249 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
250 self.assertEqual(pprint.pformat([d, d]),
251 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
252
253 # The next one is kind of goofy. The sorted order depends on the
254 # alphabetic order of type names: "int" < "str" < "tuple". Before
255 # Python 2.5, this was in the test_same_as_repr() test. It's worth
256 # keeping around for now because it's one of few tests of pprint
257 # against a crazy mix of types.
258 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
259 r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
260
Raymond Hettingerbad3c882010-09-09 12:31:00 +0000261 def test_ordered_dict(self):
262 words = 'the quick brown fox jumped over a lazy dog'.split()
263 d = collections.OrderedDict(zip(words, itertools.count()))
264 self.assertEqual(pprint.pformat(d),
265"""\
266{'the': 0,
267 'quick': 1,
268 'brown': 2,
269 'fox': 3,
270 'jumped': 4,
271 'over': 5,
272 'a': 6,
273 'lazy': 7,
274 'dog': 8}""")
Serhiy Storchaka87eb4822015-03-24 19:31:50 +0200275
276 def test_mapping_proxy(self):
277 words = 'the quick brown fox jumped over a lazy dog'.split()
278 d = dict(zip(words, itertools.count()))
279 m = types.MappingProxyType(d)
280 self.assertEqual(pprint.pformat(m), """\
281mappingproxy({'a': 6,
282 'brown': 2,
283 'dog': 8,
284 'fox': 3,
285 'jumped': 4,
286 'lazy': 7,
287 'over': 5,
288 'quick': 1,
289 'the': 0})""")
290 d = collections.OrderedDict(zip(words, itertools.count()))
291 m = types.MappingProxyType(d)
292 self.assertEqual(pprint.pformat(m), """\
293mappingproxy({'the': 0,
294 'quick': 1,
295 'brown': 2,
296 'fox': 3,
297 'jumped': 4,
298 'over': 5,
299 'a': 6,
300 'lazy': 7,
301 'dog': 8})""")
302
Fred Drakeaee113d2002-04-02 05:08:35 +0000303 def test_subclassing(self):
304 o = {'names with spaces': 'should be presented using repr()',
305 'others.should.not.be': 'like.this'}
306 exp = """\
307{'names with spaces': 'should be presented using repr()',
308 others.should.not.be: like.this}"""
309 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
310
Serhiy Storchaka51844382013-10-02 11:40:49 +0300311 def test_set_reprs(self):
312 self.assertEqual(pprint.pformat(set()), 'set()')
313 self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
314 self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\
315{0,
316 1,
317 2,
318 3,
319 4,
320 5,
321 6}''')
322 self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\
323set2({0,
324 1,
325 2,
326 3,
327 4,
328 5,
329 6})''')
330 self.assertEqual(pprint.pformat(set3(range(7)), width=20),
331 'set3({0, 1, 2, 3, 4, 5, 6})')
332
333 self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
334 self.assertEqual(pprint.pformat(frozenset(range(3))),
335 'frozenset({0, 1, 2})')
336 self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\
337frozenset({0,
338 1,
339 2,
340 3,
341 4,
342 5,
343 6})''')
344 self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\
345frozenset2({0,
346 1,
347 2,
348 3,
349 4,
350 5,
351 6})''')
352 self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
353 'frozenset3({0, 1, 2, 3, 4, 5, 6})')
354
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400355 @unittest.expectedFailure
356 #See http://bugs.python.org/issue13907
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000357 @test.support.cpython_only
Serhiy Storchaka51844382013-10-02 11:40:49 +0300358 def test_set_of_sets_reprs(self):
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000359 # This test creates a complex arrangement of frozensets and
360 # compares the pretty-printed repr against a string hard-coded in
361 # the test. The hard-coded repr depends on the sort order of
362 # frozensets.
363 #
364 # However, as the docs point out: "Since sets only define
365 # partial ordering (subset relationships), the output of the
366 # list.sort() method is undefined for lists of sets."
367 #
368 # In a nutshell, the test assumes frozenset({0}) will always
369 # sort before frozenset({1}), but:
370 #
371 # >>> frozenset({0}) < frozenset({1})
372 # False
373 # >>> frozenset({1}) < frozenset({0})
374 # False
375 #
376 # Consequently, this test is fragile and
377 # implementation-dependent. Small changes to Python's sort
378 # algorithm cause the test to fail when it should pass.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400379 # XXX Or changes to the dictionary implmentation...
Daniel Stutzbachc944cfc2010-09-21 21:08:09 +0000380
Christian Heimes969fe572008-01-25 11:23:10 +0000381 cube_repr_tgt = """\
382{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000383 frozenset({0}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000384 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000385 frozenset({0, 1})}),
386 frozenset({1}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000387 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000388 frozenset({0, 1})}),
389 frozenset({2}): frozenset({frozenset(),
Christian Heimes969fe572008-01-25 11:23:10 +0000390 frozenset({1, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000391 frozenset({0, 2})}),
392 frozenset({1, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000393 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000394 frozenset({0, 1, 2})}),
395 frozenset({0, 2}): frozenset({frozenset({2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000396 frozenset({0}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000397 frozenset({0, 1, 2})}),
398 frozenset({0, 1}): frozenset({frozenset({0}),
Christian Heimes969fe572008-01-25 11:23:10 +0000399 frozenset({1}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000400 frozenset({0, 1, 2})}),
401 frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
Christian Heimes969fe572008-01-25 11:23:10 +0000402 frozenset({0, 2}),
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000403 frozenset({0, 1})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000404 cube = test.test_set.cube(3)
405 self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
406 cubo_repr_tgt = """\
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000407{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset({frozenset({frozenset({0,
408 2}),
409 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000410 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000411 2})}),
412 frozenset({frozenset({0}),
413 frozenset({0,
414 1})}),
415 frozenset({frozenset(),
416 frozenset({0})}),
417 frozenset({frozenset({2}),
418 frozenset({0,
419 2})})}),
420 frozenset({frozenset({0, 1}), frozenset({1})}): frozenset({frozenset({frozenset({0,
421 1}),
422 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000423 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000424 2})}),
425 frozenset({frozenset({0}),
426 frozenset({0,
427 1})}),
428 frozenset({frozenset({1}),
429 frozenset({1,
430 2})}),
431 frozenset({frozenset(),
432 frozenset({1})})}),
433 frozenset({frozenset({1, 2}), frozenset({1})}): frozenset({frozenset({frozenset({1,
434 2}),
435 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000436 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000437 2})}),
438 frozenset({frozenset({2}),
439 frozenset({1,
440 2})}),
441 frozenset({frozenset(),
442 frozenset({1})}),
443 frozenset({frozenset({1}),
444 frozenset({0,
445 1})})}),
446 frozenset({frozenset({1, 2}), frozenset({2})}): frozenset({frozenset({frozenset({1,
447 2}),
448 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000449 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000450 2})}),
451 frozenset({frozenset({1}),
452 frozenset({1,
453 2})}),
454 frozenset({frozenset({2}),
455 frozenset({0,
456 2})}),
457 frozenset({frozenset(),
458 frozenset({2})})}),
459 frozenset({frozenset(), frozenset({0})}): frozenset({frozenset({frozenset({0}),
460 frozenset({0,
461 1})}),
462 frozenset({frozenset({0}),
463 frozenset({0,
464 2})}),
465 frozenset({frozenset(),
466 frozenset({1})}),
467 frozenset({frozenset(),
468 frozenset({2})})}),
469 frozenset({frozenset(), frozenset({1})}): frozenset({frozenset({frozenset(),
470 frozenset({0})}),
471 frozenset({frozenset({1}),
472 frozenset({1,
473 2})}),
474 frozenset({frozenset(),
475 frozenset({2})}),
476 frozenset({frozenset({1}),
477 frozenset({0,
478 1})})}),
479 frozenset({frozenset({2}), frozenset()}): frozenset({frozenset({frozenset({2}),
480 frozenset({1,
481 2})}),
482 frozenset({frozenset(),
483 frozenset({0})}),
484 frozenset({frozenset(),
485 frozenset({1})}),
486 frozenset({frozenset({2}),
487 frozenset({0,
488 2})})}),
489 frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset({frozenset({frozenset({1,
490 2}),
491 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000492 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000493 2})}),
494 frozenset({frozenset({0,
495 2}),
496 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000497 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000498 2})}),
499 frozenset({frozenset({0}),
500 frozenset({0,
501 1})}),
502 frozenset({frozenset({1}),
503 frozenset({0,
504 1})})}),
505 frozenset({frozenset({0}), frozenset({0, 1})}): frozenset({frozenset({frozenset(),
506 frozenset({0})}),
507 frozenset({frozenset({0,
508 1}),
509 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000510 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000511 2})}),
512 frozenset({frozenset({0}),
513 frozenset({0,
514 2})}),
515 frozenset({frozenset({1}),
516 frozenset({0,
517 1})})}),
518 frozenset({frozenset({2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({0,
519 2}),
520 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000521 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000522 2})}),
523 frozenset({frozenset({2}),
524 frozenset({1,
525 2})}),
526 frozenset({frozenset({0}),
527 frozenset({0,
528 2})}),
529 frozenset({frozenset(),
530 frozenset({2})})}),
531 frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset({frozenset({frozenset({1,
532 2}),
533 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000534 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000535 2})}),
536 frozenset({frozenset({0,
537 1}),
538 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000539 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000540 2})}),
541 frozenset({frozenset({0}),
542 frozenset({0,
543 2})}),
544 frozenset({frozenset({2}),
545 frozenset({0,
546 2})})}),
547 frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset({frozenset({frozenset({0,
548 2}),
549 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000550 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000551 2})}),
552 frozenset({frozenset({0,
553 1}),
554 frozenset({0,
Christian Heimes969fe572008-01-25 11:23:10 +0000555 1,
Raymond Hettinger4b8db412008-01-31 01:10:03 +0000556 2})}),
557 frozenset({frozenset({2}),
558 frozenset({1,
559 2})}),
560 frozenset({frozenset({1}),
561 frozenset({1,
562 2})})})}"""
Christian Heimes969fe572008-01-25 11:23:10 +0000563
564 cubo = test.test_set.linegraph(cube)
565 self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
566
Alexandre Vassalottieca20b62008-05-16 02:54:33 +0000567 def test_depth(self):
568 nested_tuple = (1, (2, (3, (4, (5, 6)))))
569 nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
570 nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
571 self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
572 self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
573 self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
574
575 lv1_tuple = '(1, (...))'
576 lv1_dict = '{1: {...}}'
577 lv1_list = '[1, [...]]'
578 self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
579 self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
580 self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
581
Raymond Hettingera7da1662009-11-19 01:07:05 +0000582 def test_sort_unorderable_values(self):
583 # Issue 3976: sorted pprints fail for unorderable values.
584 n = 20
585 keys = [Unorderable() for i in range(n)]
586 random.shuffle(keys)
587 skeys = sorted(keys, key=id)
588 clean = lambda s: s.replace(' ', '').replace('\n','')
589
590 self.assertEqual(clean(pprint.pformat(set(keys))),
591 '{' + ','.join(map(repr, skeys)) + '}')
592 self.assertEqual(clean(pprint.pformat(frozenset(keys))),
593 'frozenset({' + ','.join(map(repr, skeys)) + '})')
594 self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
595 '{' + ','.join('%r:None' % k for k in skeys) + '}')
Fred Drakeaee113d2002-04-02 05:08:35 +0000596
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200597 # Issue 10017: TypeError on user-defined types as dict keys.
598 self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),
599 '{1: 0, ' + repr(Unorderable) +': 0}')
600
601 # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
Florent Xicluna6e571d62012-07-21 12:44:20 +0200602 keys = [(1,), (None,)]
603 self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
604 '{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200605
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100606 def test_str_wrap(self):
607 # pprint tries to wrap strings intelligently
608 fox = 'the quick brown fox jumped over a lazy dog'
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200609 self.assertEqual(pprint.pformat(fox, width=19), """\
610('the quick brown '
611 'fox jumped over '
612 'a lazy dog')""")
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100613 self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200614 width=25), """\
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100615{'a': 1,
616 'b': 'the quick brown '
617 'fox jumped over '
618 'a lazy dog',
619 'c': 2}""")
620 # With some special characters
621 # - \n always triggers a new line in the pprint
622 # - \t and \n are escaped
623 # - non-ASCII is allowed
624 # - an apostrophe doesn't disrupt the pprint
625 special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200626 self.assertEqual(pprint.pformat(special, width=68), repr(special))
627 self.assertEqual(pprint.pformat(special, width=31), """\
628('Portons dix bons "whiskys"\\n'
629 "à l'avocat goujat\\t qui "
630 'fumait au zoo')""")
631 self.assertEqual(pprint.pformat(special, width=20), """\
632('Portons dix bons '
633 '"whiskys"\\n'
Serhiy Storchakafe3dc372014-12-20 20:57:15 +0200634 "à l'avocat "
635 'goujat\\t qui '
636 'fumait au zoo')""")
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200637 self.assertEqual(pprint.pformat([[[[[special]]]]], width=35), """\
638[[[[['Portons dix bons "whiskys"\\n'
639 "à l'avocat goujat\\t qui "
640 'fumait au zoo']]]]]""")
641 self.assertEqual(pprint.pformat([[[[[special]]]]], width=25), """\
642[[[[['Portons dix bons '
643 '"whiskys"\\n'
644 "à l'avocat "
645 'goujat\\t qui '
646 'fumait au zoo']]]]]""")
647 self.assertEqual(pprint.pformat([[[[[special]]]]], width=23), """\
648[[[[['Portons dix '
649 'bons "whiskys"\\n'
650 "à l'avocat "
651 'goujat\\t qui '
652 'fumait au '
653 'zoo']]]]]""")
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100654 # An unwrappable string is formatted as its repr
655 unwrappable = "x" * 100
656 self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
657 self.assertEqual(pprint.pformat(''), "''")
658 # Check that the pprint is a usable repr
659 special *= 10
660 for width in range(3, 40):
661 formatted = pprint.pformat(special, width=width)
Serhiy Storchakafe3dc372014-12-20 20:57:15 +0200662 self.assertEqual(eval(formatted), special)
663 formatted = pprint.pformat([special] * 2, width=width)
664 self.assertEqual(eval(formatted), [special] * 2)
Antoine Pitrou64c16c32013-03-23 20:30:39 +0100665
Serhiy Storchaka7c411a42013-10-02 11:56:18 +0300666 def test_compact(self):
667 o = ([list(range(i * i)) for i in range(5)] +
668 [list(range(i)) for i in range(6)])
669 expected = """\
670[[], [0], [0, 1, 2, 3],
671 [0, 1, 2, 3, 4, 5, 6, 7, 8],
672 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
673 14, 15],
674 [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
675 [0, 1, 2, 3, 4]]"""
Serhiy Storchakaa750ce32015-02-14 10:55:19 +0200676 self.assertEqual(pprint.pformat(o, width=47, compact=True), expected)
677
678 def test_compact_width(self):
679 levels = 20
680 number = 10
681 o = [0] * number
682 for i in range(levels - 1):
683 o = [o]
684 for w in range(levels * 2 + 1, levels + 3 * number - 1):
685 lines = pprint.pformat(o, width=w, compact=True).splitlines()
686 maxwidth = max(map(len, lines))
687 self.assertLessEqual(maxwidth, w)
688 self.assertGreater(maxwidth, w - 3)
Serhiy Storchaka7c411a42013-10-02 11:56:18 +0300689
Serhiy Storchaka022f2032015-03-24 19:22:37 +0200690 def test_bytes_wrap(self):
691 self.assertEqual(pprint.pformat(b'', width=1), "b''")
692 self.assertEqual(pprint.pformat(b'abcd', width=1), "b'abcd'")
693 letters = b'abcdefghijklmnopqrstuvwxyz'
694 self.assertEqual(pprint.pformat(letters, width=29), repr(letters))
695 self.assertEqual(pprint.pformat(letters, width=19), """\
696(b'abcdefghijkl'
697 b'mnopqrstuvwxyz')""")
698 self.assertEqual(pprint.pformat(letters, width=18), """\
699(b'abcdefghijkl'
700 b'mnopqrstuvwx'
701 b'yz')""")
702 self.assertEqual(pprint.pformat(letters, width=16), """\
703(b'abcdefghijkl'
704 b'mnopqrstuvwx'
705 b'yz')""")
706 special = bytes(range(16))
707 self.assertEqual(pprint.pformat(special, width=61), repr(special))
708 self.assertEqual(pprint.pformat(special, width=48), """\
709(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
710 b'\\x0c\\r\\x0e\\x0f')""")
711 self.assertEqual(pprint.pformat(special, width=32), """\
712(b'\\x00\\x01\\x02\\x03'
713 b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
714 b'\\x0c\\r\\x0e\\x0f')""")
715 self.assertEqual(pprint.pformat(special, width=1), """\
716(b'\\x00\\x01\\x02\\x03'
717 b'\\x04\\x05\\x06\\x07'
718 b'\\x08\\t\\n\\x0b'
719 b'\\x0c\\r\\x0e\\x0f')""")
720 self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
721 width=21), """\
722{'a': 1,
723 'b': b'abcdefghijkl'
724 b'mnopqrstuvwx'
725 b'yz',
726 'c': 2}""")
727 self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
728 width=20), """\
729{'a': 1,
730 'b': b'abcdefgh'
731 b'ijklmnop'
732 b'qrstuvwxyz',
733 'c': 2}""")
734 self.assertEqual(pprint.pformat([[[[[[letters]]]]]], width=25), """\
735[[[[[[b'abcdefghijklmnop'
736 b'qrstuvwxyz']]]]]]""")
737 self.assertEqual(pprint.pformat([[[[[[special]]]]]], width=41), """\
738[[[[[[b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
739 b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f']]]]]]""")
740 # Check that the pprint is a usable repr
741 for width in range(1, 64):
742 formatted = pprint.pformat(special, width=width)
743 self.assertEqual(eval(formatted), special)
744 formatted = pprint.pformat([special] * 2, width=width)
745 self.assertEqual(eval(formatted), [special] * 2)
746
747 def test_bytearray_wrap(self):
748 self.assertEqual(pprint.pformat(bytearray(), width=1), "bytearray(b'')")
749 letters = bytearray(b'abcdefghijklmnopqrstuvwxyz')
750 self.assertEqual(pprint.pformat(letters, width=40), repr(letters))
751 self.assertEqual(pprint.pformat(letters, width=28), """\
752bytearray(b'abcdefghijkl'
753 b'mnopqrstuvwxyz')""")
754 self.assertEqual(pprint.pformat(letters, width=27), """\
755bytearray(b'abcdefghijkl'
756 b'mnopqrstuvwx'
757 b'yz')""")
758 self.assertEqual(pprint.pformat(letters, width=25), """\
759bytearray(b'abcdefghijkl'
760 b'mnopqrstuvwx'
761 b'yz')""")
762 special = bytearray(range(16))
763 self.assertEqual(pprint.pformat(special, width=72), repr(special))
764 self.assertEqual(pprint.pformat(special, width=57), """\
765bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
766 b'\\x0c\\r\\x0e\\x0f')""")
767 self.assertEqual(pprint.pformat(special, width=41), """\
768bytearray(b'\\x00\\x01\\x02\\x03'
769 b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
770 b'\\x0c\\r\\x0e\\x0f')""")
771 self.assertEqual(pprint.pformat(special, width=1), """\
772bytearray(b'\\x00\\x01\\x02\\x03'
773 b'\\x04\\x05\\x06\\x07'
774 b'\\x08\\t\\n\\x0b'
775 b'\\x0c\\r\\x0e\\x0f')""")
776 self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
777 width=31), """\
778{'a': 1,
779 'b': bytearray(b'abcdefghijkl'
780 b'mnopqrstuvwx'
781 b'yz'),
782 'c': 2}""")
783 self.assertEqual(pprint.pformat([[[[[letters]]]]], width=37), """\
784[[[[[bytearray(b'abcdefghijklmnop'
785 b'qrstuvwxyz')]]]]]""")
786 self.assertEqual(pprint.pformat([[[[[special]]]]], width=50), """\
787[[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
788 b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""")
789
Florent Xiclunad6da90f2012-07-21 11:17:38 +0200790
Fred Drakeaee113d2002-04-02 05:08:35 +0000791class DottedPrettyPrinter(pprint.PrettyPrinter):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000792
Fred Drakeaee113d2002-04-02 05:08:35 +0000793 def format(self, object, context, maxlevels, level):
794 if isinstance(object, str):
795 if ' ' in object:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000796 return repr(object), 1, 0
Fred Drakeaee113d2002-04-02 05:08:35 +0000797 else:
798 return object, 0, 0
799 else:
800 return pprint.PrettyPrinter.format(
801 self, object, context, maxlevels, level)
802
803
Fred Drake2e2be372001-09-20 21:33:42 +0000804def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000805 test.support.run_unittest(QueryTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000806
807
808if __name__ == "__main__":
809 test_main()