Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_pprint.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 1802a9edb4ee |
| children | d26d8575ac62 |
| rev | line source |
|---|---|
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1 import pprint |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
2 import unittest |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
4 import test_support |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
5 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
6 try: |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
7 uni = unicode |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
8 except NameError: |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
9 def uni(x):return x |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
10 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
11 |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
12 class QueryTestCase(unittest.TestCase): |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
13 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
14 def setUp(self): |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
15 self.a = range(100) |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
16 self.b = range(200) |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
17 self.a[-12] = self.b |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
18 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
19 def test_basic(self): |
|
17829
b54dbef1f8d3
Convert a couple of comments to docstrings -- PyUnit can use these when
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
20 """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
|
21 verify = self.assert_ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
22 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
|
23 self.a, self.b): |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
24 verify(not pprint.isrecursive(safe), |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
25 "expected not isrecursive for " + `safe`) |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
26 verify(pprint.isreadable(safe), |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
27 "expected isreadable for " + `safe`) |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
28 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
29 def test_knotted(self): |
|
17829
b54dbef1f8d3
Convert a couple of comments to docstrings -- PyUnit can use these when
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
30 """Verify .isrecursive() and .isreadable() w/ recursion.""" |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
31 # Tie a knot. |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
32 self.b[67] = self.a |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
33 # Messy dict. |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
34 self.d = {} |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
35 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
|
36 |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
37 verify = self.assert_ |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
38 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
39 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
|
40 verify(pprint.isrecursive(icky), "expected isrecursive") |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
41 verify(not pprint.isreadable(icky), "expected not isreadable") |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
42 |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
43 # Break the cycles. |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
44 self.d.clear() |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
45 del self.a[:] |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
46 del self.b[:] |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
47 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
48 for safe 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
|
49 verify(not pprint.isrecursive(safe), |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
50 "expected not isrecursive for " + `safe`) |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
51 verify(pprint.isreadable(safe), |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
52 "expected isreadable for " + `safe`) |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
53 |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
54 def test_unreadable(self): |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
55 """Not recursive but not readable anyway.""" |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
56 verify = self.assert_ |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
57 for unreadable in type(3), pprint, pprint.isrecursive: |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
58 verify(not pprint.isrecursive(unreadable), |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
59 "expected not isrecursive for " + `unreadable`) |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
60 verify(not pprint.isreadable(unreadable), |
|
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
61 "expected not isreadable for " + `unreadable`) |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
62 |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
63 def test_same_as_repr(self): |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
64 "Simple objects and small containers that should be same as repr()." |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
65 verify = self.assert_ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
66 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), (), [], {}, verify, pprint, |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
17829
diff
changeset
|
67 -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
|
68 (1,2), [3,4], {5: 6, 7: 8}, |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
69 {"xy\tab\n": (3,), 5: [[]], (): {}}, |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
70 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
|
71 ): |
|
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17827
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 (native, got, function)) |
|
17827
0b3da99824c7
Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents:
17820
diff
changeset
|
78 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
79 |
|
21061
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
80 def test_basic_line_wrap(self): |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
81 """verify basic line-wrapping operation""" |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
82 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
|
83 'RPM_cal2': 48059, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
84 'Speed_cal': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
85 '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
|
86 '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
|
87 '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
|
88 '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
|
89 exp = """\ |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
90 {'RPM_cal': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
91 'RPM_cal2': 48059, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
92 'Speed_cal': 0, |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
93 '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
|
94 '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
|
95 '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
|
96 '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
|
97 self.assertEqual(pprint.pformat(o), exp) |
|
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
98 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
99 def test_main(): |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
100 test_support.run_unittest(QueryTestCase) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
101 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
102 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19343
diff
changeset
|
103 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
|
104 test_main() |
