annotate Lib/pprint.py @ 87081:dc5ae99bc605

Issue #19437: Fix GetKeepedObjects() of ctypes, handle PyCData_GetContainer() failure
author Victor Stinner <victor.stinner@gmail.com>
date Wed, 13 Nov 2013 13:24:50 +0100
parents d926f78cd055
children
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
82919
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
37 import re
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
38 import sys as _sys
64583
9659e8c40079 Have pprint() respect the order in an OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 57340
diff changeset
39 from collections import OrderedDict as _OrderedDict
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
40 from io 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
41
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 13620
diff changeset
42 __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 13620
diff changeset
43 "PrettyPrinter"]
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
45
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
46 def pprint(object, stream=None, indent=1, width=80, depth=None, *,
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
47 compact=False):
32107
7e3a2c274b36 typo - fixes 954030.
Skip Montanaro <skip@pobox.com>
parents: 31515
diff changeset
48 """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
49 printer = PrettyPrinter(
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
50 stream=stream, indent=indent, width=width, depth=depth,
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
51 compact=compact)
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
52 printer.pprint(object)
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
53
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
54 def pformat(object, indent=1, width=80, depth=None, *, compact=False):
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
55 """Format a Python object into a pretty-printed representation."""
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
56 return PrettyPrinter(indent=indent, width=width, depth=depth,
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
57 compact=compact).pformat(object)
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
58
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
59 def saferepr(object):
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
60 """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
61 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
62
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
63 def isreadable(object):
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
64 """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
65 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
66
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
67 def isrecursive(object):
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
68 """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
69 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
70
57340
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
71 class _safe_key:
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
72 """Helper function for key functions when sorting unorderable objects.
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
73
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
74 The wrapped-object will fallback to an Py2.x style comparison for
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
75 unorderable types (sorting first comparing the type name and then by
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
76 the obj ids). Does not work recursively, so dict.items() must have
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
77 _safe_key applied to both the key and the value.
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
78
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
79 """
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
80
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
81 __slots__ = ['obj']
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
82
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
83 def __init__(self, obj):
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
84 self.obj = obj
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
85
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
86 def __lt__(self, other):
78212
03cda5360dc6 Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64583
diff changeset
87 try:
03cda5360dc6 Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64583
diff changeset
88 rv = self.obj.__lt__(other.obj)
03cda5360dc6 Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64583
diff changeset
89 except TypeError:
03cda5360dc6 Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64583
diff changeset
90 rv = NotImplemented
03cda5360dc6 Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64583
diff changeset
91
57340
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
92 if rv is NotImplemented:
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
93 rv = (str(type(self.obj)), id(self.obj)) < \
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
94 (str(type(other.obj)), id(other.obj))
57340
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
95 return rv
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
96
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
97 def _safe_tuple(t):
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
98 "Helper function for comparing 2-tuples"
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
99 return _safe_key(t[0]), _safe_key(t[1])
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
100
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
101 class PrettyPrinter:
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
102 def __init__(self, indent=1, width=80, depth=None, stream=None, *,
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
103 compact=False):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
104 """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
105 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
106
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
107 indent
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
108 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
109
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
110 width
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
111 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
112
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
113 depth
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
114 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
115
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
116 stream
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
117 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
118 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
119
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
120 compact
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
121 If true, several items will be combined in one line.
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
122
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
123 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
124 indent = int(indent)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
125 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
126 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
127 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
128 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
129 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
130 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
131 self._width = width
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
132 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
133 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
134 else:
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
135 self._stream = _sys.stdout
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
136 self._compact = bool(compact)
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
138 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
139 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
140 self._stream.write("\n")
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
141
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
142 def pformat(self, object):
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
143 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
144 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
145 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
146
5837
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
147 def isrecursive(self, object):
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
148 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
149
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
150 def isreadable(self, object):
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
151 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
152 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
153
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
154 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
155 level = level + 1
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
156 objid = id(object)
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
157 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
158 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
159 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
160 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
161 return
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
162 rep = self._repr(object, context, level - 1)
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
163 typ = type(object)
82919
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
164 max_width = self._width - 1 - indent - allowance
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
165 sepLines = len(rep) > max_width
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
166 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
167
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
168 if sepLines:
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
169 r = getattr(typ, "__repr__", None)
64583
9659e8c40079 Have pprint() respect the order in an OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 57340
diff changeset
170 if issubclass(typ, dict):
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
171 write('{')
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
172 if self._indent_per_level > 1:
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
173 write((self._indent_per_level - 1) * ' ')
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
174 length = len(object)
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
175 if length:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
176 context[objid] = 1
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
177 indent = indent + self._indent_per_level
64583
9659e8c40079 Have pprint() respect the order in an OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 57340
diff changeset
178 if issubclass(typ, _OrderedDict):
9659e8c40079 Have pprint() respect the order in an OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 57340
diff changeset
179 items = list(object.items())
9659e8c40079 Have pprint() respect the order in an OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 57340
diff changeset
180 else:
9659e8c40079 Have pprint() respect the order in an OrderedDict.
Raymond Hettinger <python@rcn.com>
parents: 57340
diff changeset
181 items = sorted(object.items(), key=_safe_tuple)
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
182 key, ent = items[0]
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
183 rep = self._repr(key, context, level)
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
184 write(rep)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
185 write(': ')
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
186 self._format(ent, stream, indent + len(rep) + 2,
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
187 allowance + 1, context, level)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
188 if length > 1:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
189 for key, ent in items[1:]:
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
190 rep = self._repr(key, context, level)
21061
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20967
diff changeset
191 write(',\n%s%s: ' % (' '*indent, rep))
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
192 self._format(ent, stream, indent + len(rep) + 2,
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
193 allowance + 1, context, level)
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
194 indent = indent - self._indent_per_level
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
195 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
196 write('}')
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
197 return
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
44885
fefaa0e66fdd Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42501
diff changeset
199 if ((issubclass(typ, list) and r is list.__repr__) or
fefaa0e66fdd Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42501
diff changeset
200 (issubclass(typ, tuple) and r is tuple.__repr__) or
fefaa0e66fdd Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42501
diff changeset
201 (issubclass(typ, set) and r is set.__repr__) or
fefaa0e66fdd Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42501
diff changeset
202 (issubclass(typ, frozenset) and r is frozenset.__repr__)
fefaa0e66fdd Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42501
diff changeset
203 ):
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
204 length = len(object)
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
205 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
206 write('[')
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
207 endchar = ']'
85919
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
208 elif issubclass(typ, tuple):
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
209 write('(')
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
210 endchar = ')'
85919
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
211 else:
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
212 if not length:
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
213 write(rep)
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
214 return
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
215 if typ is set:
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
216 write('{')
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
217 endchar = '}'
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
218 else:
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
219 write(typ.__name__)
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
220 write('({')
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
221 endchar = '})'
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
222 indent += len(typ.__name__) + 1
85919
65943dc15afc Issue #19137: The pprint module now correctly formats instances of set and
Serhiy Storchaka <storchaka@gmail.com>
parents: 78212
diff changeset
223 object = sorted(object, key=_safe_key)
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
224 if self._indent_per_level > 1:
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
225 write((self._indent_per_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
226 if length:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
227 context[objid] = 1
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
228 self._format_items(object, stream,
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
229 indent + self._indent_per_level,
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
230 allowance + 1, context, level)
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
231 del context[objid]
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
232 if issubclass(typ, tuple) and length == 1:
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
233 write(',')
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
234 write(endchar)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
235 return
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
237 if issubclass(typ, str) and len(object) > 0 and r is str.__repr__:
82919
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
238 def _str_parts(s):
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
239 """
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
240 Return a list of string literals comprising the repr()
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
241 of the given string using literal concatenation.
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
242 """
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
243 lines = s.splitlines(True)
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
244 for i, line in enumerate(lines):
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
245 rep = repr(line)
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
246 if len(rep) <= max_width:
82919
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
247 yield rep
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
248 else:
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
249 # A list of alternating (non-space, space) strings
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
250 parts = re.split(r'(\s+)', line) + ['']
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
251 current = ''
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
252 for i in range(0, len(parts), 2):
82919
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
253 part = parts[i] + parts[i+1]
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
254 candidate = current + part
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
255 if len(repr(candidate)) > max_width:
82919
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
256 if current:
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
257 yield repr(current)
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
258 current = part
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
259 else:
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
260 current = candidate
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
261 if current:
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
262 yield repr(current)
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
263 for i, rep in enumerate(_str_parts(object)):
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
264 if i > 0:
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
265 write('\n' + ' '*indent)
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
266 write(rep)
5a2296093645 Issue #17150: pprint now uses line continuations to wrap long string literals.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78212
diff changeset
267 return
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
268 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
269
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
270 def _format_items(self, items, stream, indent, allowance, context, level):
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
271 write = stream.write
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
272 delimnl = ',\n' + ' ' * indent
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
273 delim = ''
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
274 width = max_width = self._width - indent - allowance + 2
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
275 for ent in items:
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
276 if self._compact:
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
277 rep = self._repr(ent, context, level)
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
278 w = len(rep) + 2
85921
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
279 if width < w:
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
280 width = max_width
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
281 if delim:
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
282 delim = delimnl
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
283 if width >= w:
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
284 width -= w
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
285 write(delim)
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
286 delim = ', '
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
287 write(rep)
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
288 continue
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
289 write(delim)
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
290 delim = delimnl
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
291 self._format(ent, stream, indent, allowance, context, level)
ae0306c8f7a3 Issue #19132: The pprint module now supports compact mode.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85920
diff changeset
292
24131
e5ecef5a123a Change the "__ private" names to "_ protected"; this has been a pain for
Fred Drake <fdrake@acm.org>
parents: 23622
diff changeset
293 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
294 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
295 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
296 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
297 self._readable = False
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
298 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
299 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
300 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
301
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
302 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
303 """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
304 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
305 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
306 """
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
307 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
308
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
309
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
310 # 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
311
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
312 def _safe_repr(object, context, maxlevels, level):
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
313 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
314 if typ is str:
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
315 if 'locale' not in _sys.modules:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
316 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
317 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
318 closure = '"'
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
319 quotes = {'"': '\\"'}
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
320 else:
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
321 closure = "'"
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
322 quotes = {"'": "\\'"}
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
323 qget = quotes.get
26929
ffa4ab37036f - PrettyPrinter.isreadable(), .isrecursive():
Fred Drake <fdrake@acm.org>
parents: 24131
diff changeset
324 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
325 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
326 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
327 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
328 write(char)
19697
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
329 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
330 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
331 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
332
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
333 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
334 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
335 if not object:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
336 return "{}", True, False
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
337 objid = id(object)
47080
58a0d5524f9a Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 45067
diff changeset
338 if maxlevels and level >= maxlevels:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
339 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
340 if objid in context:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
341 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
342 context[objid] = 1
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
343 readable = True
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
344 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
345 components = []
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
346 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
347 level += 1
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
348 saferepr = _safe_repr
57340
b31a33c36f08 Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Raymond Hettinger <python@rcn.com>
parents: 47080
diff changeset
349 items = sorted(object.items(), key=_safe_tuple)
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 38546
diff changeset
350 for k, v in items:
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
351 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
352 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
353 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
354 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
355 if krecur or vrecur:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
356 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
357 del context[objid]
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
358 return "{%s}" % ", ".join(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
359
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 29379
diff changeset
360 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
361 (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
362 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
363 if not object:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
364 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
365 format = "[%s]"
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
366 elif len(object) == 1:
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
367 format = "(%s,)"
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
368 else:
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
369 if not object:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
370 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
371 format = "(%s)"
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
372 objid = id(object)
47080
58a0d5524f9a Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 45067
diff changeset
373 if maxlevels and level >= maxlevels:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
374 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
375 if objid in context:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
376 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
377 context[objid] = 1
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
378 readable = True
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
379 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
380 components = []
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
381 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
382 level += 1
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
383 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
384 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
385 append(orepr)
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
386 if not oreadable:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
387 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
388 if orecur:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
389 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
390 del context[objid]
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
391 return format % ", ".join(components), readable, recursive
20967
bb8716095a81 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20819
diff changeset
392
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
393 rep = repr(object)
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22766
diff changeset
394 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
395
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
396
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
397 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
398 return ("<Recursion on %s with id=%s>"
85944
d926f78cd055 Remove builtin "caching" in pprint
Antoine Pitrou <solipsis@pitrou.net>
parents: 85931
diff changeset
399 % (type(object).__name__, id(object)))
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
400
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
401
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
402 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
403 import time
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
404 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
405 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
406 p = PrettyPrinter()
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
407 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
408 _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
409 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
410 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
411 t3 = time.time()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
412 print("_safe_repr:", t2 - t1)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
413 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
414
20819
261a70c0aa79 Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents: 20348
diff changeset
415 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
416 _perfcheck()