(self, object, context, maxlevels, level)
| 736 | _dispatch[_collections.UserString.__repr__] = _pprint_user_string |
| 737 | |
| 738 | def _safe_repr(self, object, context, maxlevels, level): |
| 739 | # Return triple (repr_string, isreadable, isrecursive). |
| 740 | typ = type(object) |
| 741 | if typ in _builtin_scalars: |
| 742 | return repr(object), True, False |
| 743 | |
| 744 | r = getattr(typ, "__repr__", None) |
| 745 | |
| 746 | if issubclass(typ, int) and r is int.__repr__: |
| 747 | if self._underscore_numbers: |
| 748 | return f"{object:_d}", True, False |
| 749 | else: |
| 750 | return repr(object), True, False |
| 751 | |
| 752 | if ((issubclass(typ, dict) and r is dict.__repr__) |
| 753 | or (issubclass(typ, frozendict) and r is frozendict.__repr__)): |
| 754 | is_frozendict = issubclass(typ, frozendict) |
| 755 | if not object: |
| 756 | if is_frozendict: |
| 757 | rep = f"{object.__class__.__name__}()" |
| 758 | else: |
| 759 | rep = "{}" |
| 760 | return rep, True, False |
| 761 | objid = id(object) |
| 762 | if maxlevels and level >= maxlevels: |
| 763 | rep = "{...}" |
| 764 | if is_frozendict: |
| 765 | rep = f"{object.__class__.__name__}({rep})" |
| 766 | return rep, False, objid in context |
| 767 | if objid in context: |
| 768 | return _recursion(object), False, True |
| 769 | context[objid] = 1 |
| 770 | readable = True |
| 771 | recursive = False |
| 772 | components = [] |
| 773 | append = components.append |
| 774 | level += 1 |
| 775 | if self._sort_dicts: |
| 776 | items = sorted(object.items(), key=_safe_tuple) |
| 777 | else: |
| 778 | items = object.items() |
| 779 | for k, v in items: |
| 780 | krepr, kreadable, krecur = self.format( |
| 781 | k, context, maxlevels, level) |
| 782 | vrepr, vreadable, vrecur = self.format( |
| 783 | v, context, maxlevels, level) |
| 784 | append("%s: %s" % (krepr, vrepr)) |
| 785 | readable = readable and kreadable and vreadable |
| 786 | if krecur or vrecur: |
| 787 | recursive = True |
| 788 | del context[objid] |
| 789 | rep = "{%s}" % ", ".join(components) |
| 790 | if is_frozendict: |
| 791 | rep = f"{object.__class__.__name__}({rep})" |
| 792 | return rep, readable, recursive |
| 793 | |
| 794 | if (issubclass(typ, list) and r is list.__repr__) or \ |
| 795 | (issubclass(typ, tuple) and r is tuple.__repr__): |
no test coverage detected