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