Show More
@@ -336,21 +336,25 b' class RepresentationPrinter(PrettyPrinter):' | |||||
336 | pass |
|
336 | pass | |
337 | else: |
|
337 | else: | |
338 | return printer(obj, self, cycle) |
|
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 | for cls in _get_mro(obj_class): |
|
342 | for cls in _get_mro(obj_class): | |
341 | if cls in self.type_pprinters: |
|
343 | if cls in self.type_pprinters: | |
|
344 | # printer registered in self.type_pprinters | |||
342 | return self.type_pprinters[cls](obj, self, cycle) |
|
345 | return self.type_pprinters[cls](obj, self, cycle) | |
343 | else: |
|
346 | else: | |
|
347 | # deferred printer | |||
344 | printer = self._in_deferred_types(cls) |
|
348 | printer = self._in_deferred_types(cls) | |
345 | if printer is not None: |
|
349 | if printer is not None: | |
346 | return printer(obj, self, cycle) |
|
350 | return printer(obj, self, cycle) | |
347 | # Finally look for special method names. |
|
351 | else: | |
348 | if hasattr(obj_class, '_repr_pretty_'): |
|
352 | # Finally look for special method names. | |
349 | # Some objects automatically create any requested |
|
353 | # Some objects automatically create any requested | |
350 | # attribute. Try to ignore most of them by checking for |
|
354 | # attribute. Try to ignore most of them by checking for | |
351 | # callability. |
|
355 | # callability. | |
352 |
if callable(obj_class |
|
356 | if callable(getattr(obj_class, '_repr_pretty_')): | |
353 | return obj_class._repr_pretty_(obj, self, cycle) |
|
357 | return obj_class._repr_pretty_(obj, self, cycle) | |
354 | return _default_pprint(obj, self, cycle) |
|
358 | return _default_pprint(obj, self, cycle) | |
355 | finally: |
|
359 | finally: | |
356 | self.end_group() |
|
360 | self.end_group() |
@@ -40,6 +40,11 b' class MyList(object):' | |||||
40 | p.pretty(child) |
|
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 | def test_indentation(): |
|
48 | def test_indentation(): | |
44 | """Test correct indentation in groups""" |
|
49 | """Test correct indentation in groups""" | |
45 | count = 40 |
|
50 | count = 40 | |
@@ -47,3 +52,14 b' def test_indentation():' | |||||
47 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" |
|
52 | expectedoutput = "MyList(\n" + ",\n".join(" %d" % i for i in range(count)) + ")" | |
48 |
|
53 | |||
49 | nt.assert_equals(gotoutput, expectedoutput) |
|
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