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