annotate Lib/pprint.py @ 83198:be3b4aa2ad28 2.7

- Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h files when imported, instead doing it at build time. This makes importing sysconfig faster and reduces Python startup time by 20%.
author doko@ubuntu.com
date Mon, 08 Apr 2013 21:20:09 +0200
parents d174cb3f5b9e
children fcd889046ee1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
1 # Author: Fred L. Drake, Jr.
20348
425c6c558fac Remove obsolete email address.
Fred Drake <fdrake@acm.org>
parents: 19697
diff changeset
2 # fdrake@acm.org
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # This is a simple little module I wrote to make life easier. I didn't
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # see anything quite like it in the library, though I may have overlooked
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # something. I wrote this when I was trying to read some heavily nested
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 11620
diff changeset
7 # tuples with fairly non-descriptive content. This is modeled very much
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # after Lisp/Scheme - style pretty-printing of lists. If you find it
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # useful, thank small children who sleep at night.
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 """Support to pretty-print lists, tuples, & dictionaries recursively.
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 Very simple, but useful, especially in debugging data structures.
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
15 Classes
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
16 -------
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
17
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
18 PrettyPrinter()
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
19 Handle pretty-printing operations onto a stream using a configured
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
20 set of formatting parameters.
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
21
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 Functions
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 ---------
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 pformat()
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 Format a Python object into a pretty-printed representation.
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 pprint()
32107
7e3a2c274b36 typo - fixes 954030.
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
29 Pretty-print a Python object to a stream [default is sys.stdout].
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
31 saferepr()
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
32 Generate a 'standard' repr()-like value, but protect against recursive
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
33 data structures.
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 """
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
37 import sys as _sys
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
38 import warnings
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
83198
be3b4aa2ad28 - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h
doko@ubuntu.com
parents: 82880
diff changeset
40 try:
be3b4aa2ad28 - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h
doko@ubuntu.com
parents: 82880
diff changeset
41 from cStringIO import StringIO as _StringIO
be3b4aa2ad28 - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h
doko@ubuntu.com
parents: 82880
diff changeset
42 except ImportError:
be3b4aa2ad28 - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h
doko@ubuntu.com
parents: 82880
diff changeset
43 from StringIO import StringIO as _StringIO
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 13620
diff changeset
45 __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 13620
diff changeset
46 "PrettyPrinter"]
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
48 # cache these for faster access:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
49 _commajoin = ", ".join
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
50 _id = id
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
51 _len = len
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
52 _type = type
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
53
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
54
31085
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
55 def pprint(object, stream=None, indent=1, width=80, depth=None):
32107
7e3a2c274b36 typo - fixes 954030.
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
56 """Pretty-print a Python object to a stream [default is sys.stdout]."""
31085
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
57 printer = PrettyPrinter(
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
58 stream=stream, indent=indent, width=width, depth=depth)
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
59 printer.pprint(object)
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
60
31085
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
61 def pformat(object, indent=1, width=80, depth=None):
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
62 """Format a Python object into a pretty-printed representation."""
31085
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
63 return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
64
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
65 def saferepr(object):
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
66 """Version of repr() which can handle recursive data structures."""
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
67 return _safe_repr(object, {}, None, 0)[0]
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
69 def isreadable(object):
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
70 """Determine if saferepr(object) is readable by eval()."""
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
71 return _safe_repr(object, {}, None, 0)[1]
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
72
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
73 def isrecursive(object):
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
74 """Determine if object requires a recursive representation."""
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
75 return _safe_repr(object, {}, None, 0)[2]
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
77 def _sorted(iterable):
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
78 with warnings.catch_warnings():
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
79 if _sys.py3kwarning:
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
80 warnings.filterwarnings("ignore", "comparing unequal types "
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
81 "not supported", DeprecationWarning)
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
82 return sorted(iterable)
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
83
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
84 class PrettyPrinter:
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
85 def __init__(self, indent=1, width=80, depth=None, stream=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
86 """Handle pretty printing operations onto a stream using a set of
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
87 configured parameters.
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
89 indent
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
90 Number of spaces to indent for each level of nesting.
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
91
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
92 width
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
93 Attempted maximum number of columns in the output.
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
94
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
95 depth
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
96 The maximum depth to print out nested structures.
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
98 stream
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
99 The desired output stream. If omitted (or false), the standard
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
100 output stream available at construction will be used.
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
101
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
102 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
103 indent = int(indent)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
104 width = int(width)
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
105 assert indent >= 0, "indent must be >= 0"
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
106 assert depth is None or depth > 0, "depth must be > 0"
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
107 assert width, "width must be != 0"
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
108 self._depth = depth
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
109 self._indent_per_level = indent
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
110 self._width = width
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
111 if stream is not None:
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
112 self._stream = stream
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
113 else:
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
114 self._stream = _sys.stdout
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
116 def pprint(self, object):
35852
ee3e0ee1edca Change PrettyPrinter.pprint(), so that output is written directly to the
Walter Dörwald <walter@livinglogic.de>
parents: 33993
diff changeset
117 self._format(object, self._stream, 0, 0, {}, 0)
ee3e0ee1edca Change PrettyPrinter.pprint(), so that output is written directly to the
Walter Dörwald <walter@livinglogic.de>
parents: 33993
diff changeset
118 self._stream.write("\n")
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
119
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
120 def pformat(self, object):
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
121 sio = _StringIO()
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
122 self._format(object, sio, 0, 0, {}, 0)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
123 return sio.getvalue()
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
5837
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
125 def isrecursive(self, object):
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
126 return self.format(object, {}, 0, 0)[2]
5837
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
127
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
128 def isreadable(self, object):
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
129 s, readable, recursive = self.format(object, {}, 0, 0)
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
130 return readable and not recursive
5837
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
131
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
132 def _format(self, object, stream, indent, allowance, context, level):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
133 level = level + 1
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
134 objid = _id(object)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
135 if objid in context:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
136 stream.write(_recursion(object))
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
137 self._recursive = True
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
138 self._readable = False
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
139 return
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
140 rep = self._repr(object, context, level - 1)
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
141 typ = _type(object)
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
142 sepLines = _len(rep) > (self._width - 1 - indent - allowance)
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
143 write = stream.write
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
46947
07a53bb82baa #1713041: fix pprint's handling of maximum depth.
Georg Brandl <georg@python.org>
parents: 44927
diff changeset
145 if self._depth and level > self._depth:
07a53bb82baa #1713041: fix pprint's handling of maximum depth.
Georg Brandl <georg@python.org>
parents: 44927
diff changeset
146 write(rep)
07a53bb82baa #1713041: fix pprint's handling of maximum depth.
Georg Brandl <georg@python.org>
parents: 44927
diff changeset
147 return
07a53bb82baa #1713041: fix pprint's handling of maximum depth.
Georg Brandl <georg@python.org>
parents: 44927
diff changeset
148
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
149 r = getattr(typ, "__repr__", None)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
150 if issubclass(typ, dict) and r is dict.__repr__:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
151 write('{')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
152 if self._indent_per_level > 1:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
153 write((self._indent_per_level - 1) * ' ')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
154 length = _len(object)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
155 if length:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
156 context[objid] = 1
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
157 indent = indent + self._indent_per_level
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
158 items = _sorted(object.items())
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
159 key, ent = items[0]
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
160 rep = self._repr(key, context, level)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
161 write(rep)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
162 write(': ')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
163 self._format(ent, stream, indent + _len(rep) + 2,
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
164 allowance + 1, context, level)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
165 if length > 1:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
166 for key, ent in items[1:]:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
167 rep = self._repr(key, context, level)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
168 if sepLines:
21061
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20967
diff changeset
169 write(',\n%s%s: ' % (' '*indent, rep))
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
170 else:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
171 write(', %s: ' % rep)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
172 self._format(ent, stream, indent + _len(rep) + 2,
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
173 allowance + 1, context, level)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
174 indent = indent - self._indent_per_level
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
175 del context[objid]
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
176 write('}')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
177 return
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
44882
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
179 if ((issubclass(typ, list) and r is list.__repr__) or
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
180 (issubclass(typ, tuple) and r is tuple.__repr__) or
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
181 (issubclass(typ, set) and r is set.__repr__) or
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
182 (issubclass(typ, frozenset) and r is frozenset.__repr__)
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
183 ):
44927
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
184 length = _len(object)
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
185 if issubclass(typ, list):
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
186 write('[')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
187 endchar = ']'
44882
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
188 elif issubclass(typ, set):
44927
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
189 if not length:
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
190 write('set()')
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
191 return
44882
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
192 write('set([')
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
193 endchar = '])'
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
194 object = _sorted(object)
44882
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
195 indent += 4
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
196 elif issubclass(typ, frozenset):
44927
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
197 if not length:
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
198 write('frozenset()')
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
199 return
44882
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
200 write('frozenset([')
6bd4849b5458 Let pprint() support sets and frozensets (suggested by David Mertz).
Raymond Hettinger <python@rcn.com>
parents: 44815
diff changeset
201 endchar = '])'
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
202 object = _sorted(object)
44927
757a7f31e460 Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures.
Raymond Hettinger <python@rcn.com>
parents: 44882
diff changeset
203 indent += 10
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
204 else:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
205 write('(')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
206 endchar = ')'
47797
016166b7b976 Fixed issue #2888. Now the behaviour of pprint when working with nested
Facundo Batista <facundobatista@gmail.com>
parents: 46947
diff changeset
207 if self._indent_per_level > 1 and sepLines:
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
208 write((self._indent_per_level - 1) * ' ')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
209 if length:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
210 context[objid] = 1
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
211 indent = indent + self._indent_per_level
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
212 self._format(object[0], stream, indent, allowance + 1,
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
213 context, level)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
214 if length > 1:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
215 for ent in object[1:]:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
216 if sepLines:
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
217 write(',\n' + ' '*indent)
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
218 else:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
219 write(', ')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
220 self._format(ent, stream, indent,
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
221 allowance + 1, context, level)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
222 indent = indent - self._indent_per_level
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
223 del context[objid]
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
224 if issubclass(typ, tuple) and length == 1:
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
225 write(',')
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
226 write(endchar)
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
227 return
46947
07a53bb82baa #1713041: fix pprint's handling of maximum depth.
Georg Brandl <georg@python.org>
parents: 44927
diff changeset
228
44815
942e4b42da14 #1351692: in pprint, always call format() for dict and list items to enable
Georg Brandl <georg@python.org>
parents: 38546
diff changeset
229 write(rep)
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
231 def _repr(self, object, context, level):
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
232 repr, readable, recursive = self.format(object, context.copy(),
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
233 self._depth, level)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
234 if not readable:
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
235 self._readable = False
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
236 if recursive:
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
237 self._recursive = True
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
238 return repr
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
240 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
241 """Format object for a specific context, returning a string
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
242 and flags indicating whether the representation is 'readable'
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
243 and whether the object represents a recursive construct.
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
244 """
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
245 return _safe_repr(object, context, maxlevels, level)
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
246
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
247
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
248 # Return triple (repr_string, isreadable, isrecursive).
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
250 def _safe_repr(object, context, maxlevels, level):
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
251 typ = _type(object)
29379
347e7b6dfd53 Revert 1.25, as overloaded __repr__ is not considered.
Martin v. Löwis <martin@v.loewis.de>
parents: 29378
diff changeset
252 if typ is str:
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
253 if 'locale' not in _sys.modules:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
254 return repr(object), True, False
19697
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
255 if "'" in object and '"' not in object:
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
256 closure = '"'
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
257 quotes = {'"': '\\"'}
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
258 else:
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
259 closure = "'"
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
260 quotes = {"'": "\\'"}
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
261 qget = quotes.get
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
262 sio = _StringIO()
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
263 write = sio.write
19697
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
264 for char in object:
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
265 if char.isalpha():
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
266 write(char)
19697
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
267 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
268 write(qget(char, repr(char)[1:-1]))
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
269 return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
270
33993
6771e1840bbc Fix pprint to be able to handle objects that don't have a __repr__
Walter Dörwald <walter@livinglogic.de>
parents: 32107
diff changeset
271 r = getattr(typ, "__repr__", None)
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
272 if issubclass(typ, dict) and r is dict.__repr__:
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
273 if not object:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
274 return "{}", True, False
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
275 objid = _id(object)
46947
07a53bb82baa #1713041: fix pprint's handling of maximum depth.
Georg Brandl <georg@python.org>
parents: 44927
diff changeset
276 if maxlevels and level >= maxlevels:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
277 return "{...}", False, objid in context
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
278 if objid in context:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
279 return _recursion(object), False, True
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
280 context[objid] = 1
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
281 readable = True
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
282 recursive = False
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
283 components = []
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
284 append = components.append
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
285 level += 1
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
286 saferepr = _safe_repr
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 47797
diff changeset
287 for k, v in _sorted(object.items()):
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
288 krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
289 vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
290 append("%s: %s" % (krepr, vrepr))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
291 readable = readable and kreadable and vreadable
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
292 if krecur or vrecur:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
293 recursive = True
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
294 del context[objid]
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
295 return "{%s}" % _commajoin(components), readable, recursive
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
296
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
297 if (issubclass(typ, list) and r is list.__repr__) or \
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
298 (issubclass(typ, tuple) and r is tuple.__repr__):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
299 if issubclass(typ, list):
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
300 if not object:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
301 return "[]", True, False
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
302 format = "[%s]"
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
303 elif _len(object) == 1:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
304 format = "(%s,)"
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
305 else:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
306 if not object:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
307 return "()", True, False
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
308 format = "(%s)"
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
309 objid = _id(object)
46947
07a53bb82baa #1713041: fix pprint's handling of maximum depth.
Georg Brandl <georg@python.org>
parents: 44927
diff changeset
310 if maxlevels and level >= maxlevels:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
311 return format % "...", False, objid in context
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
312 if objid in context:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
313 return _recursion(object), False, True
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
314 context[objid] = 1
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
315 readable = True
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
316 recursive = False
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
317 components = []
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
318 append = components.append
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
319 level += 1
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
320 for o in object:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
321 orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
322 append(orepr)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
323 if not oreadable:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
324 readable = False
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
325 if orecur:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
326 recursive = True
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
327 del context[objid]
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
328 return format % _commajoin(components), readable, recursive
20967
bb8716095a81 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20819
diff changeset
329
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
330 rep = repr(object)
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
331 return rep, (rep and not rep.startswith('<')), False
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
332
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
333
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
334 def _recursion(object):
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
335 return ("<Recursion on %s with id=%s>"
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
336 % (_type(object).__name__, _id(object)))
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
337
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
339 def _perfcheck(object=None):
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
340 import time
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
341 if object is None:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
342 object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
343 p = PrettyPrinter()
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
344 t1 = time.time()
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
345 _safe_repr(object, {}, None, 0)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
346 t2 = time.time()
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
347 p.pformat(object)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
348 t3 = time.time()
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
349 print "_safe_repr:", t2 - t1
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
350 print "pformat:", t3 - t2
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
352 if __name__ == "__main__":
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
353 _perfcheck()