Show More
@@ -336,21 +336,25 b' class RepresentationPrinter(PrettyPrinter):' | |||
|
336 | 336 | pass |
|
337 | 337 | else: |
|
338 | 338 | return printer(obj, self, cycle) |
|
339 |
# Next |
|
|
339 | # Next walk the mro and check for either: | |
|
340 | # 1) a registered printer | |
|
341 | # 2) a _repr_pretty_ method | |
|
340 | 342 | for cls in _get_mro(obj_class): |
|
341 | 343 | if cls in self.type_pprinters: |
|
344 | # printer registered in self.type_pprinters | |
|
342 | 345 | return self.type_pprinters[cls](obj, self, cycle) |
|
343 | 346 | else: |
|
347 | # deferred printer | |
|
344 | 348 | printer = self._in_deferred_types(cls) |
|
345 | 349 | if printer is not None: |
|
346 | 350 | return printer(obj, self, cycle) |
|
347 | # Finally look for special method names. | |
|
348 | if hasattr(obj_class, '_repr_pretty_'): | |
|
349 | # Some objects automatically create any requested | |
|
350 | # attribute. Try to ignore most of them by checking for | |
|
351 | # callability. | |
|
352 |
if callable(obj_class |
|
|
353 | return obj_class._repr_pretty_(obj, self, cycle) | |
|
351 | else: | |
|
352 | # Finally look for special method names. | |
|
353 | # Some objects automatically create any requested | |
|
354 | # attribute. Try to ignore most of them by checking for | |
|
355 | # callability. | |
|
356 | if callable(getattr(obj_class, '_repr_pretty_')): | |
|
357 | return obj_class._repr_pretty_(obj, self, cycle) | |
|
354 | 358 | return _default_pprint(obj, self, cycle) |
|
355 | 359 | finally: |
|
356 | 360 | self.end_group() |
@@ -40,6 +40,11 b' class MyList(object):' | |||
|
40 | 40 | p.pretty(child) |
|
41 | 41 | |
|
42 | 42 | |
|
43 | class MyDict(dict): | |
|
44 | def _repr_pretty_(self, p, cycle): | |
|
45 | p.text("MyDict(...)") | |
|
46 | ||
|
47 | ||
|
43 | 48 | def test_indentation(): |
|
44 | 49 | """Test correct indentation in groups""" |
|
45 | 50 | count = 40 |
@@ -47,3 +52,14 b' def test_indentation():' | |||
|
47 | 52 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" |
|
48 | 53 | |
|
49 | 54 | nt.assert_equals(gotoutput, expectedoutput) |
|
55 | ||
|
56 | ||
|
57 | def test_dispatch(): | |
|
58 | """ | |
|
59 | Test correct dispatching: The _repr_pretty_ method for MyDict | |
|
60 | must be found before the registered printer for dict. | |
|
61 | """ | |
|
62 | gotoutput = pretty.pretty(MyDict()) | |
|
63 | expectedoutput = "MyDict(...)" | |
|
64 | ||
|
65 | nt.assert_equals(gotoutput, expectedoutput) |
General Comments 0
You need to be logged in to leave comments.
Login now