Show More
@@ -1,871 +1,871 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Python advanced pretty printer. This pretty printer is intended to |
|
4 | 4 | replace the old `pprint` python module which does not allow developers |
|
5 | 5 | to provide their own pretty print callbacks. |
|
6 | 6 | |
|
7 | 7 | This module is based on ruby's `prettyprint.rb` library by `Tanaka Akira`. |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | Example Usage |
|
11 | 11 | ------------- |
|
12 | 12 | |
|
13 | 13 | To directly print the representation of an object use `pprint`:: |
|
14 | 14 | |
|
15 | 15 | from pretty import pprint |
|
16 | 16 | pprint(complex_object) |
|
17 | 17 | |
|
18 | 18 | To get a string of the output use `pretty`:: |
|
19 | 19 | |
|
20 | 20 | from pretty import pretty |
|
21 | 21 | string = pretty(complex_object) |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | Extending |
|
25 | 25 | --------- |
|
26 | 26 | |
|
27 | 27 | The pretty library allows developers to add pretty printing rules for their |
|
28 | 28 | own objects. This process is straightforward. All you have to do is to |
|
29 | 29 | add a `_repr_pretty_` method to your object and call the methods on the |
|
30 | 30 | pretty printer passed:: |
|
31 | 31 | |
|
32 | 32 | class MyObject(object): |
|
33 | 33 | |
|
34 | 34 | def _repr_pretty_(self, p, cycle): |
|
35 | 35 | ... |
|
36 | 36 | |
|
37 | 37 | Here is an example implementation of a `_repr_pretty_` method for a list |
|
38 | 38 | subclass:: |
|
39 | 39 | |
|
40 | 40 | class MyList(list): |
|
41 | 41 | |
|
42 | 42 | def _repr_pretty_(self, p, cycle): |
|
43 | 43 | if cycle: |
|
44 | 44 | p.text('MyList(...)') |
|
45 | 45 | else: |
|
46 | 46 | with p.group(8, 'MyList([', '])'): |
|
47 | 47 | for idx, item in enumerate(self): |
|
48 | 48 | if idx: |
|
49 | 49 | p.text(',') |
|
50 | 50 | p.breakable() |
|
51 | 51 | p.pretty(item) |
|
52 | 52 | |
|
53 | 53 | The `cycle` parameter is `True` if pretty detected a cycle. You *have* to |
|
54 | 54 | react to that or the result is an infinite loop. `p.text()` just adds |
|
55 | 55 | non breaking text to the output, `p.breakable()` either adds a whitespace |
|
56 | 56 | or breaks here. If you pass it an argument it's used instead of the |
|
57 | 57 | default space. `p.pretty` prettyprints another object using the pretty print |
|
58 | 58 | method. |
|
59 | 59 | |
|
60 | 60 | The first parameter to the `group` function specifies the extra indentation |
|
61 | 61 | of the next line. In this example the next item will either be on the same |
|
62 | 62 | line (if the items are short enough) or aligned with the right edge of the |
|
63 | 63 | opening bracket of `MyList`. |
|
64 | 64 | |
|
65 | 65 | If you just want to indent something you can use the group function |
|
66 | 66 | without open / close parameters. You can also use this code:: |
|
67 | 67 | |
|
68 | 68 | with p.indent(2): |
|
69 | 69 | ... |
|
70 | 70 | |
|
71 | 71 | Inheritance diagram: |
|
72 | 72 | |
|
73 | 73 | .. inheritance-diagram:: IPython.lib.pretty |
|
74 | 74 | :parts: 3 |
|
75 | 75 | |
|
76 | 76 | :copyright: 2007 by Armin Ronacher. |
|
77 | 77 | Portions (c) 2009 by Robert Kern. |
|
78 | 78 | :license: BSD License. |
|
79 | 79 | """ |
|
80 | 80 | |
|
81 | 81 | from contextlib import contextmanager |
|
82 | 82 | import datetime |
|
83 | 83 | import os |
|
84 | 84 | import re |
|
85 | 85 | import sys |
|
86 | 86 | import types |
|
87 | 87 | from collections import deque |
|
88 | from inspect import signature | |
|
88 | 89 | from io import StringIO |
|
89 | 90 | from warnings import warn |
|
90 | 91 | |
|
91 | 92 | from IPython.utils.decorators import undoc |
|
92 | 93 | from IPython.utils.py3compat import PYPY |
|
93 | from IPython.utils.signatures import signature | |
|
94 | 94 | |
|
95 | 95 | __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', |
|
96 | 96 | 'for_type', 'for_type_by_name'] |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | MAX_SEQ_LENGTH = 1000 |
|
100 | 100 | # The language spec says that dicts preserve order from 3.7, but CPython |
|
101 | 101 | # does so from 3.6, so it seems likely that people will expect that. |
|
102 | 102 | DICT_IS_ORDERED = sys.version_info >= (3, 6) |
|
103 | 103 | _re_pattern_type = type(re.compile('')) |
|
104 | 104 | |
|
105 | 105 | def _safe_getattr(obj, attr, default=None): |
|
106 | 106 | """Safe version of getattr. |
|
107 | 107 | |
|
108 | 108 | Same as getattr, but will return ``default`` on any Exception, |
|
109 | 109 | rather than raising. |
|
110 | 110 | """ |
|
111 | 111 | try: |
|
112 | 112 | return getattr(obj, attr, default) |
|
113 | 113 | except Exception: |
|
114 | 114 | return default |
|
115 | 115 | |
|
116 | 116 | @undoc |
|
117 | 117 | class CUnicodeIO(StringIO): |
|
118 | 118 | def __init__(self, *args, **kwargs): |
|
119 | 119 | super().__init__(*args, **kwargs) |
|
120 | 120 | warn(("CUnicodeIO is deprecated since IPython 6.0. " |
|
121 | 121 | "Please use io.StringIO instead."), |
|
122 | 122 | DeprecationWarning, stacklevel=2) |
|
123 | 123 | |
|
124 | 124 | def _sorted_for_pprint(items): |
|
125 | 125 | """ |
|
126 | 126 | Sort the given items for pretty printing. Since some predictable |
|
127 | 127 | sorting is better than no sorting at all, we sort on the string |
|
128 | 128 | representation if normal sorting fails. |
|
129 | 129 | """ |
|
130 | 130 | items = list(items) |
|
131 | 131 | try: |
|
132 | 132 | return sorted(items) |
|
133 | 133 | except Exception: |
|
134 | 134 | try: |
|
135 | 135 | return sorted(items, key=str) |
|
136 | 136 | except Exception: |
|
137 | 137 | return items |
|
138 | 138 | |
|
139 | 139 | def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH): |
|
140 | 140 | """ |
|
141 | 141 | Pretty print the object's representation. |
|
142 | 142 | """ |
|
143 | 143 | stream = StringIO() |
|
144 | 144 | printer = RepresentationPrinter(stream, verbose, max_width, newline, max_seq_length=max_seq_length) |
|
145 | 145 | printer.pretty(obj) |
|
146 | 146 | printer.flush() |
|
147 | 147 | return stream.getvalue() |
|
148 | 148 | |
|
149 | 149 | |
|
150 | 150 | def pprint(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH): |
|
151 | 151 | """ |
|
152 | 152 | Like `pretty` but print to stdout. |
|
153 | 153 | """ |
|
154 | 154 | printer = RepresentationPrinter(sys.stdout, verbose, max_width, newline, max_seq_length=max_seq_length) |
|
155 | 155 | printer.pretty(obj) |
|
156 | 156 | printer.flush() |
|
157 | 157 | sys.stdout.write(newline) |
|
158 | 158 | sys.stdout.flush() |
|
159 | 159 | |
|
160 | 160 | class _PrettyPrinterBase(object): |
|
161 | 161 | |
|
162 | 162 | @contextmanager |
|
163 | 163 | def indent(self, indent): |
|
164 | 164 | """with statement support for indenting/dedenting.""" |
|
165 | 165 | self.indentation += indent |
|
166 | 166 | try: |
|
167 | 167 | yield |
|
168 | 168 | finally: |
|
169 | 169 | self.indentation -= indent |
|
170 | 170 | |
|
171 | 171 | @contextmanager |
|
172 | 172 | def group(self, indent=0, open='', close=''): |
|
173 | 173 | """like begin_group / end_group but for the with statement.""" |
|
174 | 174 | self.begin_group(indent, open) |
|
175 | 175 | try: |
|
176 | 176 | yield |
|
177 | 177 | finally: |
|
178 | 178 | self.end_group(indent, close) |
|
179 | 179 | |
|
180 | 180 | class PrettyPrinter(_PrettyPrinterBase): |
|
181 | 181 | """ |
|
182 | 182 | Baseclass for the `RepresentationPrinter` prettyprinter that is used to |
|
183 | 183 | generate pretty reprs of objects. Contrary to the `RepresentationPrinter` |
|
184 | 184 | this printer knows nothing about the default pprinters or the `_repr_pretty_` |
|
185 | 185 | callback method. |
|
186 | 186 | """ |
|
187 | 187 | |
|
188 | 188 | def __init__(self, output, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH): |
|
189 | 189 | self.output = output |
|
190 | 190 | self.max_width = max_width |
|
191 | 191 | self.newline = newline |
|
192 | 192 | self.max_seq_length = max_seq_length |
|
193 | 193 | self.output_width = 0 |
|
194 | 194 | self.buffer_width = 0 |
|
195 | 195 | self.buffer = deque() |
|
196 | 196 | |
|
197 | 197 | root_group = Group(0) |
|
198 | 198 | self.group_stack = [root_group] |
|
199 | 199 | self.group_queue = GroupQueue(root_group) |
|
200 | 200 | self.indentation = 0 |
|
201 | 201 | |
|
202 | 202 | def _break_outer_groups(self): |
|
203 | 203 | while self.max_width < self.output_width + self.buffer_width: |
|
204 | 204 | group = self.group_queue.deq() |
|
205 | 205 | if not group: |
|
206 | 206 | return |
|
207 | 207 | while group.breakables: |
|
208 | 208 | x = self.buffer.popleft() |
|
209 | 209 | self.output_width = x.output(self.output, self.output_width) |
|
210 | 210 | self.buffer_width -= x.width |
|
211 | 211 | while self.buffer and isinstance(self.buffer[0], Text): |
|
212 | 212 | x = self.buffer.popleft() |
|
213 | 213 | self.output_width = x.output(self.output, self.output_width) |
|
214 | 214 | self.buffer_width -= x.width |
|
215 | 215 | |
|
216 | 216 | def text(self, obj): |
|
217 | 217 | """Add literal text to the output.""" |
|
218 | 218 | width = len(obj) |
|
219 | 219 | if self.buffer: |
|
220 | 220 | text = self.buffer[-1] |
|
221 | 221 | if not isinstance(text, Text): |
|
222 | 222 | text = Text() |
|
223 | 223 | self.buffer.append(text) |
|
224 | 224 | text.add(obj, width) |
|
225 | 225 | self.buffer_width += width |
|
226 | 226 | self._break_outer_groups() |
|
227 | 227 | else: |
|
228 | 228 | self.output.write(obj) |
|
229 | 229 | self.output_width += width |
|
230 | 230 | |
|
231 | 231 | def breakable(self, sep=' '): |
|
232 | 232 | """ |
|
233 | 233 | Add a breakable separator to the output. This does not mean that it |
|
234 | 234 | will automatically break here. If no breaking on this position takes |
|
235 | 235 | place the `sep` is inserted which default to one space. |
|
236 | 236 | """ |
|
237 | 237 | width = len(sep) |
|
238 | 238 | group = self.group_stack[-1] |
|
239 | 239 | if group.want_break: |
|
240 | 240 | self.flush() |
|
241 | 241 | self.output.write(self.newline) |
|
242 | 242 | self.output.write(' ' * self.indentation) |
|
243 | 243 | self.output_width = self.indentation |
|
244 | 244 | self.buffer_width = 0 |
|
245 | 245 | else: |
|
246 | 246 | self.buffer.append(Breakable(sep, width, self)) |
|
247 | 247 | self.buffer_width += width |
|
248 | 248 | self._break_outer_groups() |
|
249 | 249 | |
|
250 | 250 | def break_(self): |
|
251 | 251 | """ |
|
252 | 252 | Explicitly insert a newline into the output, maintaining correct indentation. |
|
253 | 253 | """ |
|
254 | 254 | self.flush() |
|
255 | 255 | self.output.write(self.newline) |
|
256 | 256 | self.output.write(' ' * self.indentation) |
|
257 | 257 | self.output_width = self.indentation |
|
258 | 258 | self.buffer_width = 0 |
|
259 | 259 | |
|
260 | 260 | |
|
261 | 261 | def begin_group(self, indent=0, open=''): |
|
262 | 262 | """ |
|
263 | 263 | Begin a group. If you want support for python < 2.5 which doesn't has |
|
264 | 264 | the with statement this is the preferred way: |
|
265 | 265 | |
|
266 | 266 | p.begin_group(1, '{') |
|
267 | 267 | ... |
|
268 | 268 | p.end_group(1, '}') |
|
269 | 269 | |
|
270 | 270 | The python 2.5 expression would be this: |
|
271 | 271 | |
|
272 | 272 | with p.group(1, '{', '}'): |
|
273 | 273 | ... |
|
274 | 274 | |
|
275 | 275 | The first parameter specifies the indentation for the next line (usually |
|
276 | 276 | the width of the opening text), the second the opening text. All |
|
277 | 277 | parameters are optional. |
|
278 | 278 | """ |
|
279 | 279 | if open: |
|
280 | 280 | self.text(open) |
|
281 | 281 | group = Group(self.group_stack[-1].depth + 1) |
|
282 | 282 | self.group_stack.append(group) |
|
283 | 283 | self.group_queue.enq(group) |
|
284 | 284 | self.indentation += indent |
|
285 | 285 | |
|
286 | 286 | def _enumerate(self, seq): |
|
287 | 287 | """like enumerate, but with an upper limit on the number of items""" |
|
288 | 288 | for idx, x in enumerate(seq): |
|
289 | 289 | if self.max_seq_length and idx >= self.max_seq_length: |
|
290 | 290 | self.text(',') |
|
291 | 291 | self.breakable() |
|
292 | 292 | self.text('...') |
|
293 | 293 | return |
|
294 | 294 | yield idx, x |
|
295 | 295 | |
|
296 | 296 | def end_group(self, dedent=0, close=''): |
|
297 | 297 | """End a group. See `begin_group` for more details.""" |
|
298 | 298 | self.indentation -= dedent |
|
299 | 299 | group = self.group_stack.pop() |
|
300 | 300 | if not group.breakables: |
|
301 | 301 | self.group_queue.remove(group) |
|
302 | 302 | if close: |
|
303 | 303 | self.text(close) |
|
304 | 304 | |
|
305 | 305 | def flush(self): |
|
306 | 306 | """Flush data that is left in the buffer.""" |
|
307 | 307 | for data in self.buffer: |
|
308 | 308 | self.output_width += data.output(self.output, self.output_width) |
|
309 | 309 | self.buffer.clear() |
|
310 | 310 | self.buffer_width = 0 |
|
311 | 311 | |
|
312 | 312 | |
|
313 | 313 | def _get_mro(obj_class): |
|
314 | 314 | """ Get a reasonable method resolution order of a class and its superclasses |
|
315 | 315 | for both old-style and new-style classes. |
|
316 | 316 | """ |
|
317 | 317 | if not hasattr(obj_class, '__mro__'): |
|
318 | 318 | # Old-style class. Mix in object to make a fake new-style class. |
|
319 | 319 | try: |
|
320 | 320 | obj_class = type(obj_class.__name__, (obj_class, object), {}) |
|
321 | 321 | except TypeError: |
|
322 | 322 | # Old-style extension type that does not descend from object. |
|
323 | 323 | # FIXME: try to construct a more thorough MRO. |
|
324 | 324 | mro = [obj_class] |
|
325 | 325 | else: |
|
326 | 326 | mro = obj_class.__mro__[1:-1] |
|
327 | 327 | else: |
|
328 | 328 | mro = obj_class.__mro__ |
|
329 | 329 | return mro |
|
330 | 330 | |
|
331 | 331 | |
|
332 | 332 | class RepresentationPrinter(PrettyPrinter): |
|
333 | 333 | """ |
|
334 | 334 | Special pretty printer that has a `pretty` method that calls the pretty |
|
335 | 335 | printer for a python object. |
|
336 | 336 | |
|
337 | 337 | This class stores processing data on `self` so you must *never* use |
|
338 | 338 | this class in a threaded environment. Always lock it or reinstanciate |
|
339 | 339 | it. |
|
340 | 340 | |
|
341 | 341 | Instances also have a verbose flag callbacks can access to control their |
|
342 | 342 | output. For example the default instance repr prints all attributes and |
|
343 | 343 | methods that are not prefixed by an underscore if the printer is in |
|
344 | 344 | verbose mode. |
|
345 | 345 | """ |
|
346 | 346 | |
|
347 | 347 | def __init__(self, output, verbose=False, max_width=79, newline='\n', |
|
348 | 348 | singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None, |
|
349 | 349 | max_seq_length=MAX_SEQ_LENGTH): |
|
350 | 350 | |
|
351 | 351 | PrettyPrinter.__init__(self, output, max_width, newline, max_seq_length=max_seq_length) |
|
352 | 352 | self.verbose = verbose |
|
353 | 353 | self.stack = [] |
|
354 | 354 | if singleton_pprinters is None: |
|
355 | 355 | singleton_pprinters = _singleton_pprinters.copy() |
|
356 | 356 | self.singleton_pprinters = singleton_pprinters |
|
357 | 357 | if type_pprinters is None: |
|
358 | 358 | type_pprinters = _type_pprinters.copy() |
|
359 | 359 | self.type_pprinters = type_pprinters |
|
360 | 360 | if deferred_pprinters is None: |
|
361 | 361 | deferred_pprinters = _deferred_type_pprinters.copy() |
|
362 | 362 | self.deferred_pprinters = deferred_pprinters |
|
363 | 363 | |
|
364 | 364 | def pretty(self, obj): |
|
365 | 365 | """Pretty print the given object.""" |
|
366 | 366 | obj_id = id(obj) |
|
367 | 367 | cycle = obj_id in self.stack |
|
368 | 368 | self.stack.append(obj_id) |
|
369 | 369 | self.begin_group() |
|
370 | 370 | try: |
|
371 | 371 | obj_class = _safe_getattr(obj, '__class__', None) or type(obj) |
|
372 | 372 | # First try to find registered singleton printers for the type. |
|
373 | 373 | try: |
|
374 | 374 | printer = self.singleton_pprinters[obj_id] |
|
375 | 375 | except (TypeError, KeyError): |
|
376 | 376 | pass |
|
377 | 377 | else: |
|
378 | 378 | return printer(obj, self, cycle) |
|
379 | 379 | # Next walk the mro and check for either: |
|
380 | 380 | # 1) a registered printer |
|
381 | 381 | # 2) a _repr_pretty_ method |
|
382 | 382 | for cls in _get_mro(obj_class): |
|
383 | 383 | if cls in self.type_pprinters: |
|
384 | 384 | # printer registered in self.type_pprinters |
|
385 | 385 | return self.type_pprinters[cls](obj, self, cycle) |
|
386 | 386 | else: |
|
387 | 387 | # deferred printer |
|
388 | 388 | printer = self._in_deferred_types(cls) |
|
389 | 389 | if printer is not None: |
|
390 | 390 | return printer(obj, self, cycle) |
|
391 | 391 | else: |
|
392 | 392 | # Finally look for special method names. |
|
393 | 393 | # Some objects automatically create any requested |
|
394 | 394 | # attribute. Try to ignore most of them by checking for |
|
395 | 395 | # callability. |
|
396 | 396 | if '_repr_pretty_' in cls.__dict__: |
|
397 | 397 | meth = cls._repr_pretty_ |
|
398 | 398 | if callable(meth): |
|
399 | 399 | return meth(obj, self, cycle) |
|
400 | 400 | if cls is not object \ |
|
401 | 401 | and callable(cls.__dict__.get('__repr__')): |
|
402 | 402 | return _repr_pprint(obj, self, cycle) |
|
403 | 403 | |
|
404 | 404 | return _default_pprint(obj, self, cycle) |
|
405 | 405 | finally: |
|
406 | 406 | self.end_group() |
|
407 | 407 | self.stack.pop() |
|
408 | 408 | |
|
409 | 409 | def _in_deferred_types(self, cls): |
|
410 | 410 | """ |
|
411 | 411 | Check if the given class is specified in the deferred type registry. |
|
412 | 412 | |
|
413 | 413 | Returns the printer from the registry if it exists, and None if the |
|
414 | 414 | class is not in the registry. Successful matches will be moved to the |
|
415 | 415 | regular type registry for future use. |
|
416 | 416 | """ |
|
417 | 417 | mod = _safe_getattr(cls, '__module__', None) |
|
418 | 418 | name = _safe_getattr(cls, '__name__', None) |
|
419 | 419 | key = (mod, name) |
|
420 | 420 | printer = None |
|
421 | 421 | if key in self.deferred_pprinters: |
|
422 | 422 | # Move the printer over to the regular registry. |
|
423 | 423 | printer = self.deferred_pprinters.pop(key) |
|
424 | 424 | self.type_pprinters[cls] = printer |
|
425 | 425 | return printer |
|
426 | 426 | |
|
427 | 427 | |
|
428 | 428 | class Printable(object): |
|
429 | 429 | |
|
430 | 430 | def output(self, stream, output_width): |
|
431 | 431 | return output_width |
|
432 | 432 | |
|
433 | 433 | |
|
434 | 434 | class Text(Printable): |
|
435 | 435 | |
|
436 | 436 | def __init__(self): |
|
437 | 437 | self.objs = [] |
|
438 | 438 | self.width = 0 |
|
439 | 439 | |
|
440 | 440 | def output(self, stream, output_width): |
|
441 | 441 | for obj in self.objs: |
|
442 | 442 | stream.write(obj) |
|
443 | 443 | return output_width + self.width |
|
444 | 444 | |
|
445 | 445 | def add(self, obj, width): |
|
446 | 446 | self.objs.append(obj) |
|
447 | 447 | self.width += width |
|
448 | 448 | |
|
449 | 449 | |
|
450 | 450 | class Breakable(Printable): |
|
451 | 451 | |
|
452 | 452 | def __init__(self, seq, width, pretty): |
|
453 | 453 | self.obj = seq |
|
454 | 454 | self.width = width |
|
455 | 455 | self.pretty = pretty |
|
456 | 456 | self.indentation = pretty.indentation |
|
457 | 457 | self.group = pretty.group_stack[-1] |
|
458 | 458 | self.group.breakables.append(self) |
|
459 | 459 | |
|
460 | 460 | def output(self, stream, output_width): |
|
461 | 461 | self.group.breakables.popleft() |
|
462 | 462 | if self.group.want_break: |
|
463 | 463 | stream.write(self.pretty.newline) |
|
464 | 464 | stream.write(' ' * self.indentation) |
|
465 | 465 | return self.indentation |
|
466 | 466 | if not self.group.breakables: |
|
467 | 467 | self.pretty.group_queue.remove(self.group) |
|
468 | 468 | stream.write(self.obj) |
|
469 | 469 | return output_width + self.width |
|
470 | 470 | |
|
471 | 471 | |
|
472 | 472 | class Group(Printable): |
|
473 | 473 | |
|
474 | 474 | def __init__(self, depth): |
|
475 | 475 | self.depth = depth |
|
476 | 476 | self.breakables = deque() |
|
477 | 477 | self.want_break = False |
|
478 | 478 | |
|
479 | 479 | |
|
480 | 480 | class GroupQueue(object): |
|
481 | 481 | |
|
482 | 482 | def __init__(self, *groups): |
|
483 | 483 | self.queue = [] |
|
484 | 484 | for group in groups: |
|
485 | 485 | self.enq(group) |
|
486 | 486 | |
|
487 | 487 | def enq(self, group): |
|
488 | 488 | depth = group.depth |
|
489 | 489 | while depth > len(self.queue) - 1: |
|
490 | 490 | self.queue.append([]) |
|
491 | 491 | self.queue[depth].append(group) |
|
492 | 492 | |
|
493 | 493 | def deq(self): |
|
494 | 494 | for stack in self.queue: |
|
495 | 495 | for idx, group in enumerate(reversed(stack)): |
|
496 | 496 | if group.breakables: |
|
497 | 497 | del stack[idx] |
|
498 | 498 | group.want_break = True |
|
499 | 499 | return group |
|
500 | 500 | for group in stack: |
|
501 | 501 | group.want_break = True |
|
502 | 502 | del stack[:] |
|
503 | 503 | |
|
504 | 504 | def remove(self, group): |
|
505 | 505 | try: |
|
506 | 506 | self.queue[group.depth].remove(group) |
|
507 | 507 | except ValueError: |
|
508 | 508 | pass |
|
509 | 509 | |
|
510 | 510 | |
|
511 | 511 | def _default_pprint(obj, p, cycle): |
|
512 | 512 | """ |
|
513 | 513 | The default print function. Used if an object does not provide one and |
|
514 | 514 | it's none of the builtin objects. |
|
515 | 515 | """ |
|
516 | 516 | klass = _safe_getattr(obj, '__class__', None) or type(obj) |
|
517 | 517 | if _safe_getattr(klass, '__repr__', None) is not object.__repr__: |
|
518 | 518 | # A user-provided repr. Find newlines and replace them with p.break_() |
|
519 | 519 | _repr_pprint(obj, p, cycle) |
|
520 | 520 | return |
|
521 | 521 | p.begin_group(1, '<') |
|
522 | 522 | p.pretty(klass) |
|
523 | 523 | p.text(' at 0x%x' % id(obj)) |
|
524 | 524 | if cycle: |
|
525 | 525 | p.text(' ...') |
|
526 | 526 | elif p.verbose: |
|
527 | 527 | first = True |
|
528 | 528 | for key in dir(obj): |
|
529 | 529 | if not key.startswith('_'): |
|
530 | 530 | try: |
|
531 | 531 | value = getattr(obj, key) |
|
532 | 532 | except AttributeError: |
|
533 | 533 | continue |
|
534 | 534 | if isinstance(value, types.MethodType): |
|
535 | 535 | continue |
|
536 | 536 | if not first: |
|
537 | 537 | p.text(',') |
|
538 | 538 | p.breakable() |
|
539 | 539 | p.text(key) |
|
540 | 540 | p.text('=') |
|
541 | 541 | step = len(key) + 1 |
|
542 | 542 | p.indentation += step |
|
543 | 543 | p.pretty(value) |
|
544 | 544 | p.indentation -= step |
|
545 | 545 | first = False |
|
546 | 546 | p.end_group(1, '>') |
|
547 | 547 | |
|
548 | 548 | |
|
549 | 549 | def _seq_pprinter_factory(start, end): |
|
550 | 550 | """ |
|
551 | 551 | Factory that returns a pprint function useful for sequences. Used by |
|
552 | 552 | the default pprint for tuples, dicts, and lists. |
|
553 | 553 | """ |
|
554 | 554 | def inner(obj, p, cycle): |
|
555 | 555 | if cycle: |
|
556 | 556 | return p.text(start + '...' + end) |
|
557 | 557 | step = len(start) |
|
558 | 558 | p.begin_group(step, start) |
|
559 | 559 | for idx, x in p._enumerate(obj): |
|
560 | 560 | if idx: |
|
561 | 561 | p.text(',') |
|
562 | 562 | p.breakable() |
|
563 | 563 | p.pretty(x) |
|
564 | 564 | if len(obj) == 1 and type(obj) is tuple: |
|
565 | 565 | # Special case for 1-item tuples. |
|
566 | 566 | p.text(',') |
|
567 | 567 | p.end_group(step, end) |
|
568 | 568 | return inner |
|
569 | 569 | |
|
570 | 570 | |
|
571 | 571 | def _set_pprinter_factory(start, end): |
|
572 | 572 | """ |
|
573 | 573 | Factory that returns a pprint function useful for sets and frozensets. |
|
574 | 574 | """ |
|
575 | 575 | def inner(obj, p, cycle): |
|
576 | 576 | if cycle: |
|
577 | 577 | return p.text(start + '...' + end) |
|
578 | 578 | if len(obj) == 0: |
|
579 | 579 | # Special case. |
|
580 | 580 | p.text(type(obj).__name__ + '()') |
|
581 | 581 | else: |
|
582 | 582 | step = len(start) |
|
583 | 583 | p.begin_group(step, start) |
|
584 | 584 | # Like dictionary keys, we will try to sort the items if there aren't too many |
|
585 | 585 | if not (p.max_seq_length and len(obj) >= p.max_seq_length): |
|
586 | 586 | items = _sorted_for_pprint(obj) |
|
587 | 587 | else: |
|
588 | 588 | items = obj |
|
589 | 589 | for idx, x in p._enumerate(items): |
|
590 | 590 | if idx: |
|
591 | 591 | p.text(',') |
|
592 | 592 | p.breakable() |
|
593 | 593 | p.pretty(x) |
|
594 | 594 | p.end_group(step, end) |
|
595 | 595 | return inner |
|
596 | 596 | |
|
597 | 597 | |
|
598 | 598 | def _dict_pprinter_factory(start, end): |
|
599 | 599 | """ |
|
600 | 600 | Factory that returns a pprint function used by the default pprint of |
|
601 | 601 | dicts and dict proxies. |
|
602 | 602 | """ |
|
603 | 603 | def inner(obj, p, cycle): |
|
604 | 604 | if cycle: |
|
605 | 605 | return p.text('{...}') |
|
606 | 606 | step = len(start) |
|
607 | 607 | p.begin_group(step, start) |
|
608 | 608 | keys = obj.keys() |
|
609 | 609 | # if dict isn't large enough to be truncated, sort keys before displaying |
|
610 | 610 | # From Python 3.7, dicts preserve order by definition, so we don't sort. |
|
611 | 611 | if not DICT_IS_ORDERED \ |
|
612 | 612 | and not (p.max_seq_length and len(obj) >= p.max_seq_length): |
|
613 | 613 | keys = _sorted_for_pprint(keys) |
|
614 | 614 | for idx, key in p._enumerate(keys): |
|
615 | 615 | if idx: |
|
616 | 616 | p.text(',') |
|
617 | 617 | p.breakable() |
|
618 | 618 | p.pretty(key) |
|
619 | 619 | p.text(': ') |
|
620 | 620 | p.pretty(obj[key]) |
|
621 | 621 | p.end_group(step, end) |
|
622 | 622 | return inner |
|
623 | 623 | |
|
624 | 624 | |
|
625 | 625 | def _super_pprint(obj, p, cycle): |
|
626 | 626 | """The pprint for the super type.""" |
|
627 | 627 | p.begin_group(8, '<super: ') |
|
628 | 628 | p.pretty(obj.__thisclass__) |
|
629 | 629 | p.text(',') |
|
630 | 630 | p.breakable() |
|
631 | 631 | if PYPY: # In PyPy, super() objects don't have __self__ attributes |
|
632 | 632 | dself = obj.__repr__.__self__ |
|
633 | 633 | p.pretty(None if dself is obj else dself) |
|
634 | 634 | else: |
|
635 | 635 | p.pretty(obj.__self__) |
|
636 | 636 | p.end_group(8, '>') |
|
637 | 637 | |
|
638 | 638 | |
|
639 | 639 | def _re_pattern_pprint(obj, p, cycle): |
|
640 | 640 | """The pprint function for regular expression patterns.""" |
|
641 | 641 | p.text('re.compile(') |
|
642 | 642 | pattern = repr(obj.pattern) |
|
643 | 643 | if pattern[:1] in 'uU': |
|
644 | 644 | pattern = pattern[1:] |
|
645 | 645 | prefix = 'ur' |
|
646 | 646 | else: |
|
647 | 647 | prefix = 'r' |
|
648 | 648 | pattern = prefix + pattern.replace('\\\\', '\\') |
|
649 | 649 | p.text(pattern) |
|
650 | 650 | if obj.flags: |
|
651 | 651 | p.text(',') |
|
652 | 652 | p.breakable() |
|
653 | 653 | done_one = False |
|
654 | 654 | for flag in ('TEMPLATE', 'IGNORECASE', 'LOCALE', 'MULTILINE', 'DOTALL', |
|
655 | 655 | 'UNICODE', 'VERBOSE', 'DEBUG'): |
|
656 | 656 | if obj.flags & getattr(re, flag): |
|
657 | 657 | if done_one: |
|
658 | 658 | p.text('|') |
|
659 | 659 | p.text('re.' + flag) |
|
660 | 660 | done_one = True |
|
661 | 661 | p.text(')') |
|
662 | 662 | |
|
663 | 663 | |
|
664 | 664 | def _type_pprint(obj, p, cycle): |
|
665 | 665 | """The pprint for classes and types.""" |
|
666 | 666 | # Heap allocated types might not have the module attribute, |
|
667 | 667 | # and others may set it to None. |
|
668 | 668 | |
|
669 | 669 | # Checks for a __repr__ override in the metaclass. Can't compare the |
|
670 | 670 | # type(obj).__repr__ directly because in PyPy the representation function |
|
671 | 671 | # inherited from type isn't the same type.__repr__ |
|
672 | 672 | if [m for m in _get_mro(type(obj)) if "__repr__" in vars(m)][:1] != [type]: |
|
673 | 673 | _repr_pprint(obj, p, cycle) |
|
674 | 674 | return |
|
675 | 675 | |
|
676 | 676 | mod = _safe_getattr(obj, '__module__', None) |
|
677 | 677 | try: |
|
678 | 678 | name = obj.__qualname__ |
|
679 | 679 | if not isinstance(name, str): |
|
680 | 680 | # This can happen if the type implements __qualname__ as a property |
|
681 | 681 | # or other descriptor in Python 2. |
|
682 | 682 | raise Exception("Try __name__") |
|
683 | 683 | except Exception: |
|
684 | 684 | name = obj.__name__ |
|
685 | 685 | if not isinstance(name, str): |
|
686 | 686 | name = '<unknown type>' |
|
687 | 687 | |
|
688 | 688 | if mod in (None, '__builtin__', 'builtins', 'exceptions'): |
|
689 | 689 | p.text(name) |
|
690 | 690 | else: |
|
691 | 691 | p.text(mod + '.' + name) |
|
692 | 692 | |
|
693 | 693 | |
|
694 | 694 | def _repr_pprint(obj, p, cycle): |
|
695 | 695 | """A pprint that just redirects to the normal repr function.""" |
|
696 | 696 | # Find newlines and replace them with p.break_() |
|
697 | 697 | output = repr(obj) |
|
698 | 698 | for idx,output_line in enumerate(output.splitlines()): |
|
699 | 699 | if idx: |
|
700 | 700 | p.break_() |
|
701 | 701 | p.text(output_line) |
|
702 | 702 | |
|
703 | 703 | |
|
704 | 704 | def _function_pprint(obj, p, cycle): |
|
705 | 705 | """Base pprint for all functions and builtin functions.""" |
|
706 | 706 | name = _safe_getattr(obj, '__qualname__', obj.__name__) |
|
707 | 707 | mod = obj.__module__ |
|
708 | 708 | if mod and mod not in ('__builtin__', 'builtins', 'exceptions'): |
|
709 | 709 | name = mod + '.' + name |
|
710 | 710 | try: |
|
711 | 711 | func_def = name + str(signature(obj)) |
|
712 | 712 | except ValueError: |
|
713 | 713 | func_def = name |
|
714 | 714 | p.text('<function %s>' % func_def) |
|
715 | 715 | |
|
716 | 716 | |
|
717 | 717 | def _exception_pprint(obj, p, cycle): |
|
718 | 718 | """Base pprint for all exceptions.""" |
|
719 | 719 | name = getattr(obj.__class__, '__qualname__', obj.__class__.__name__) |
|
720 | 720 | if obj.__class__.__module__ not in ('exceptions', 'builtins'): |
|
721 | 721 | name = '%s.%s' % (obj.__class__.__module__, name) |
|
722 | 722 | step = len(name) + 1 |
|
723 | 723 | p.begin_group(step, name + '(') |
|
724 | 724 | for idx, arg in enumerate(getattr(obj, 'args', ())): |
|
725 | 725 | if idx: |
|
726 | 726 | p.text(',') |
|
727 | 727 | p.breakable() |
|
728 | 728 | p.pretty(arg) |
|
729 | 729 | p.end_group(step, ')') |
|
730 | 730 | |
|
731 | 731 | |
|
732 | 732 | #: the exception base |
|
733 | 733 | try: |
|
734 | 734 | _exception_base = BaseException |
|
735 | 735 | except NameError: |
|
736 | 736 | _exception_base = Exception |
|
737 | 737 | |
|
738 | 738 | |
|
739 | 739 | #: printers for builtin types |
|
740 | 740 | _type_pprinters = { |
|
741 | 741 | int: _repr_pprint, |
|
742 | 742 | float: _repr_pprint, |
|
743 | 743 | str: _repr_pprint, |
|
744 | 744 | tuple: _seq_pprinter_factory('(', ')'), |
|
745 | 745 | list: _seq_pprinter_factory('[', ']'), |
|
746 | 746 | dict: _dict_pprinter_factory('{', '}'), |
|
747 | 747 | set: _set_pprinter_factory('{', '}'), |
|
748 | 748 | frozenset: _set_pprinter_factory('frozenset({', '})'), |
|
749 | 749 | super: _super_pprint, |
|
750 | 750 | _re_pattern_type: _re_pattern_pprint, |
|
751 | 751 | type: _type_pprint, |
|
752 | 752 | types.FunctionType: _function_pprint, |
|
753 | 753 | types.BuiltinFunctionType: _function_pprint, |
|
754 | 754 | types.MethodType: _repr_pprint, |
|
755 | 755 | datetime.datetime: _repr_pprint, |
|
756 | 756 | datetime.timedelta: _repr_pprint, |
|
757 | 757 | _exception_base: _exception_pprint |
|
758 | 758 | } |
|
759 | 759 | |
|
760 | 760 | # render os.environ like a dict |
|
761 | 761 | _env_type = type(os.environ) |
|
762 | 762 | # future-proof in case os.environ becomes a plain dict? |
|
763 | 763 | if _env_type is not dict: |
|
764 | 764 | _type_pprinters[_env_type] = _dict_pprinter_factory('environ{', '}') |
|
765 | 765 | |
|
766 | 766 | try: |
|
767 | 767 | # In PyPy, types.DictProxyType is dict, setting the dictproxy printer |
|
768 | 768 | # using dict.setdefault avoids overwritting the dict printer |
|
769 | 769 | _type_pprinters.setdefault(types.DictProxyType, |
|
770 | 770 | _dict_pprinter_factory('dict_proxy({', '})')) |
|
771 | 771 | _type_pprinters[types.ClassType] = _type_pprint |
|
772 | 772 | _type_pprinters[types.SliceType] = _repr_pprint |
|
773 | 773 | except AttributeError: # Python 3 |
|
774 | 774 | _type_pprinters[types.MappingProxyType] = \ |
|
775 | 775 | _dict_pprinter_factory('mappingproxy({', '})') |
|
776 | 776 | _type_pprinters[slice] = _repr_pprint |
|
777 | 777 | |
|
778 | 778 | try: |
|
779 | 779 | _type_pprinters[long] = _repr_pprint |
|
780 | 780 | _type_pprinters[unicode] = _repr_pprint |
|
781 | 781 | except NameError: |
|
782 | 782 | _type_pprinters[range] = _repr_pprint |
|
783 | 783 | _type_pprinters[bytes] = _repr_pprint |
|
784 | 784 | |
|
785 | 785 | #: printers for types specified by name |
|
786 | 786 | _deferred_type_pprinters = { |
|
787 | 787 | } |
|
788 | 788 | |
|
789 | 789 | def for_type(typ, func): |
|
790 | 790 | """ |
|
791 | 791 | Add a pretty printer for a given type. |
|
792 | 792 | """ |
|
793 | 793 | oldfunc = _type_pprinters.get(typ, None) |
|
794 | 794 | if func is not None: |
|
795 | 795 | # To support easy restoration of old pprinters, we need to ignore Nones. |
|
796 | 796 | _type_pprinters[typ] = func |
|
797 | 797 | return oldfunc |
|
798 | 798 | |
|
799 | 799 | def for_type_by_name(type_module, type_name, func): |
|
800 | 800 | """ |
|
801 | 801 | Add a pretty printer for a type specified by the module and name of a type |
|
802 | 802 | rather than the type object itself. |
|
803 | 803 | """ |
|
804 | 804 | key = (type_module, type_name) |
|
805 | 805 | oldfunc = _deferred_type_pprinters.get(key, None) |
|
806 | 806 | if func is not None: |
|
807 | 807 | # To support easy restoration of old pprinters, we need to ignore Nones. |
|
808 | 808 | _deferred_type_pprinters[key] = func |
|
809 | 809 | return oldfunc |
|
810 | 810 | |
|
811 | 811 | |
|
812 | 812 | #: printers for the default singletons |
|
813 | 813 | _singleton_pprinters = dict.fromkeys(map(id, [None, True, False, Ellipsis, |
|
814 | 814 | NotImplemented]), _repr_pprint) |
|
815 | 815 | |
|
816 | 816 | |
|
817 | 817 | def _defaultdict_pprint(obj, p, cycle): |
|
818 | 818 | name = obj.__class__.__name__ |
|
819 | 819 | with p.group(len(name) + 1, name + '(', ')'): |
|
820 | 820 | if cycle: |
|
821 | 821 | p.text('...') |
|
822 | 822 | else: |
|
823 | 823 | p.pretty(obj.default_factory) |
|
824 | 824 | p.text(',') |
|
825 | 825 | p.breakable() |
|
826 | 826 | p.pretty(dict(obj)) |
|
827 | 827 | |
|
828 | 828 | def _ordereddict_pprint(obj, p, cycle): |
|
829 | 829 | name = obj.__class__.__name__ |
|
830 | 830 | with p.group(len(name) + 1, name + '(', ')'): |
|
831 | 831 | if cycle: |
|
832 | 832 | p.text('...') |
|
833 | 833 | elif len(obj): |
|
834 | 834 | p.pretty(list(obj.items())) |
|
835 | 835 | |
|
836 | 836 | def _deque_pprint(obj, p, cycle): |
|
837 | 837 | name = obj.__class__.__name__ |
|
838 | 838 | with p.group(len(name) + 1, name + '(', ')'): |
|
839 | 839 | if cycle: |
|
840 | 840 | p.text('...') |
|
841 | 841 | else: |
|
842 | 842 | p.pretty(list(obj)) |
|
843 | 843 | |
|
844 | 844 | |
|
845 | 845 | def _counter_pprint(obj, p, cycle): |
|
846 | 846 | name = obj.__class__.__name__ |
|
847 | 847 | with p.group(len(name) + 1, name + '(', ')'): |
|
848 | 848 | if cycle: |
|
849 | 849 | p.text('...') |
|
850 | 850 | elif len(obj): |
|
851 | 851 | p.pretty(dict(obj)) |
|
852 | 852 | |
|
853 | 853 | for_type_by_name('collections', 'defaultdict', _defaultdict_pprint) |
|
854 | 854 | for_type_by_name('collections', 'OrderedDict', _ordereddict_pprint) |
|
855 | 855 | for_type_by_name('collections', 'deque', _deque_pprint) |
|
856 | 856 | for_type_by_name('collections', 'Counter', _counter_pprint) |
|
857 | 857 | |
|
858 | 858 | if __name__ == '__main__': |
|
859 | 859 | from random import randrange |
|
860 | 860 | class Foo(object): |
|
861 | 861 | def __init__(self): |
|
862 | 862 | self.foo = 1 |
|
863 | 863 | self.bar = re.compile(r'\s+') |
|
864 | 864 | self.blub = dict.fromkeys(range(30), randrange(1, 40)) |
|
865 | 865 | self.hehe = 23424.234234 |
|
866 | 866 | self.list = ["blub", "blah", self] |
|
867 | 867 | |
|
868 | 868 | def get_foo(self): |
|
869 | 869 | print("foo") |
|
870 | 870 | |
|
871 | 871 | pprint(Foo(), verbose=True) |
General Comments 0
You need to be logged in to leave comments.
Login now