Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_pprint.py @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 2eb8be0f8731 |
| children | 757a7f31e460 1c26505c1de6 |
| rev | line source |
|---|---|
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1 import pprint |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
2 import test.test_support |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
3 import unittest |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
4 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
5 try: |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
6 uni = unicode |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
7 except NameError: |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
8 def uni(x): |
|
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
9 return x |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
10 |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
11 # list, tuple and dict subclasses that do or don't overwrite __repr__ |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
12 class list2(list): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
13 pass |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
14 |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
15 class list3(list): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
16 def __repr__(self): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
17 return list.__repr__(self) |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
18 |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
19 class tuple2(tuple): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
20 pass |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
21 |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
22 class tuple3(tuple): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
23 def __repr__(self): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
24 return tuple.__repr__(self) |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
25 |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
26 class dict2(dict): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
27 pass |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
28 |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
29 class dict3(dict): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
30 def __repr__(self): |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
31 return dict.__repr__(self) |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
32 |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
33 class QueryTestCase(unittest.TestCase): |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
34 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
35 def setUp(self): |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
36 self.a = range(100) |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
37 self.b = range(200) |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
38 self.a[-12] = self.b |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
39 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
40 def test_basic(self): |
|
24957
dc66eb4cd17a
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
41 # Verify .isrecursive() and .isreadable() w/o recursion |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
42 verify = self.assert_ |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
43 pp = pprint.PrettyPrinter() |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
44 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"), |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
45 self.a, self.b): |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
46 # module-level convenience functions |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
47 verify(not pprint.isrecursive(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
48 "expected not isrecursive for %r" % (safe,)) |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
49 verify(pprint.isreadable(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
50 "expected isreadable for %r" % (safe,)) |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
51 # PrettyPrinter methods |
|
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
52 verify(not pp.isrecursive(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
53 "expected not isrecursive for %r" % (safe,)) |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
54 verify(pp.isreadable(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
55 "expected isreadable for %r" % (safe,)) |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
56 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
57 def test_knotted(self): |
|
24957
dc66eb4cd17a
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
58 # Verify .isrecursive() and .isreadable() w/ recursion |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
59 # Tie a knot. |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
60 self.b[67] = self.a |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
61 # Messy dict. |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
62 self.d = {} |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
63 self.d[0] = self.d[1] = self.d[2] = self.d |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
64 |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
65 verify = self.assert_ |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
66 pp = pprint.PrettyPrinter() |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
67 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
68 for icky in self.a, self.b, self.d, (self.d, self.d): |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
69 verify(pprint.isrecursive(icky), "expected isrecursive") |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
70 verify(not pprint.isreadable(icky), "expected not isreadable") |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
71 verify(pp.isrecursive(icky), "expected isrecursive") |
|
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
72 verify(not pp.isreadable(icky), "expected not isreadable") |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
73 |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
74 # Break the cycles. |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
75 self.d.clear() |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
76 del self.a[:] |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
77 del self.b[:] |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
78 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
79 for safe in self.a, self.b, self.d, (self.d, self.d): |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
80 # module-level convenience functions |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
81 verify(not pprint.isrecursive(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
82 "expected not isrecursive for %r" % (safe,)) |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
83 verify(pprint.isreadable(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
84 "expected isreadable for %r" % (safe,)) |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
85 # PrettyPrinter methods |
|
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
86 verify(not pp.isrecursive(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
87 "expected not isrecursive for %r" % (safe,)) |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
88 verify(pp.isreadable(safe), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
89 "expected isreadable for %r" % (safe,)) |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
90 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
91 def test_unreadable(self): |
|
24957
dc66eb4cd17a
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
92 # Not recursive but not readable anyway |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
93 verify = self.assert_ |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
94 pp = pprint.PrettyPrinter() |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
95 for unreadable in type(3), pprint, pprint.isrecursive: |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
96 # module-level convenience functions |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
97 verify(not pprint.isrecursive(unreadable), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
98 "expected not isrecursive for %r" % (unreadable,)) |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
99 verify(not pprint.isreadable(unreadable), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
100 "expected not isreadable for %r" % (unreadable,)) |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
101 # PrettyPrinter methods |
|
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
102 verify(not pp.isrecursive(unreadable), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
103 "expected not isrecursive for %r" % (unreadable,)) |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
104 verify(not pp.isreadable(unreadable), |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
105 "expected not isreadable for %r" % (unreadable,)) |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
106 |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
107 def test_same_as_repr(self): |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
108 # Simple objects, small containers and classes that overwrite __repr__ |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
109 # For those the result should be the same as repr(). |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
110 # Ahem. The docs don't say anything about that -- this appears to |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
111 # be testing an implementation quirk. Starting in Python 2.5, it's |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
112 # not true for dicts: pprint always sorts dicts by key now; before, |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
113 # it sorted a dict display if and only if the display required |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
114 # multiple lines. For that reason, dicts with more than one element |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
115 # aren't tested here. |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
116 verify = self.assert_ |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
117 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
118 (), tuple2(), tuple3(), |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
119 [], list2(), list3(), |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
120 {}, dict2(), dict3(), |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
121 verify, pprint, |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
122 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6}, |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
123 (1,2), [3,4], {5: 6, 7: 8}, |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
124 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)), |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
125 [3,4], list2([3,4]), list3([3,4]), list3(range(100)), |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
126 {5: 6, 7: 8}, dict2({5: 6}), dict3({5: 6}), |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
127 range(10, -11, -1) |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
128 ): |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
129 native = repr(simple) |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
130 for function in "pformat", "saferepr": |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
131 f = getattr(pprint, function) |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
132 got = f(simple) |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
133 verify(native == got, "expected %s got %s from pprint.%s" % |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
134 (native, got, function)) |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
135 |
|
21061
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
136 def test_basic_line_wrap(self): |
|
24957
dc66eb4cd17a
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
137 # verify basic line-wrapping operation |
|
21061
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
138 o = {'RPM_cal': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
139 'RPM_cal2': 48059, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
140 'Speed_cal': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
141 'controldesk_runtime_us': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
142 'main_code_runtime_us': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
143 'read_io_runtime_us': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
144 'write_io_runtime_us': 43690} |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
145 exp = """\ |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
146 {'RPM_cal': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
147 'RPM_cal2': 48059, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
148 'Speed_cal': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
149 'controldesk_runtime_us': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
150 'main_code_runtime_us': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
151 'read_io_runtime_us': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
152 'write_io_runtime_us': 43690}""" |
|
31084
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
153 for type in [dict, dict2]: |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
154 self.assertEqual(pprint.pformat(type(o)), exp) |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
155 |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
156 o = range(100) |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
157 exp = '[%s]' % ',\n '.join(map(str, o)) |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
158 for type in [list, list2]: |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
159 self.assertEqual(pprint.pformat(type(o)), exp) |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
160 |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
161 o = tuple(range(100)) |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
162 exp = '(%s)' % ',\n '.join(map(str, o)) |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
163 for type in [tuple, tuple2]: |
|
e3025fe70d8b
Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents:
26930
diff
changeset
|
164 self.assertEqual(pprint.pformat(type(o)), exp) |
|
21061
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
165 |
|
31085
a2f6c6a67f24
Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents:
31084
diff
changeset
|
166 # indent parameter |
|
a2f6c6a67f24
Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents:
31084
diff
changeset
|
167 o = range(100) |
|
a2f6c6a67f24
Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents:
31084
diff
changeset
|
168 exp = '[ %s]' % ',\n '.join(map(str, o)) |
|
a2f6c6a67f24
Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents:
31084
diff
changeset
|
169 for type in [list, list2]: |
|
a2f6c6a67f24
Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents:
31084
diff
changeset
|
170 self.assertEqual(pprint.pformat(type(o), indent=4), exp) |
|
a2f6c6a67f24
Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents:
31084
diff
changeset
|
171 |
|
38546
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
172 def test_sorted_dict(self): |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
173 # Starting in Python 2.5, pprint sorts dict displays by key regardless |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
174 # of how small the dictionary may be. |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
175 # Before the change, on 32-bit Windows pformat() gave order |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
176 # 'a', 'c', 'b' here, so this test failed. |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
177 d = {'a': 1, 'b': 1, 'c': 1} |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
178 self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}") |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
179 self.assertEqual(pprint.pformat([d, d]), |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
180 "[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]") |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
181 |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
182 # The next one is kind of goofy. The sorted order depends on the |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
183 # alphabetic order of type names: "int" < "str" < "tuple". Before |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
184 # Python 2.5, this was in the test_same_as_repr() test. It's worth |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
185 # keeping around for now because it's one of few tests of pprint |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
186 # against a crazy mix of types. |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
187 self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
188 r"{5: [[]], 'xy\tab\n': (3,), (): {}}") |
|
2eb8be0f8731
pprint functions used to sort a dict (by key) if and only if
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
189 |
|
22766
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
190 def test_subclassing(self): |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
191 o = {'names with spaces': 'should be presented using repr()', |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
192 'others.should.not.be': 'like.this'} |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
193 exp = """\ |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
194 {'names with spaces': 'should be presented using repr()', |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
195 others.should.not.be: like.this}""" |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
196 self.assertEqual(DottedPrettyPrinter().pformat(o), exp) |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
197 |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
198 |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
199 class DottedPrettyPrinter(pprint.PrettyPrinter): |
|
24957
dc66eb4cd17a
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
200 |
|
22766
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
201 def format(self, object, context, maxlevels, level): |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
202 if isinstance(object, str): |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
203 if ' ' in object: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
31085
diff
changeset
|
204 return repr(object), 1, 0 |
|
22766
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
205 else: |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
206 return object, 0, 0 |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
207 else: |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
208 return pprint.PrettyPrinter.format( |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
209 self, object, context, maxlevels, level) |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
210 |
|
d26d8575ac62
Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents:
21061
diff
changeset
|
211 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
212 def test_main(): |
|
26930
32e4c7753416
Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents:
24957
diff
changeset
|
213 test.test_support.run_unittest(QueryTestCase) |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
214 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
215 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
216 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
217 test_main() |
