MCPcopy Index your code
hub / github.com/RustPython/RustPython / _safe_repr

Method _safe_repr

Lib/pprint.py:569–648  ·  view source on GitHub ↗
(self, object, context, maxlevels, level)

Source from the content-addressed store, hash-verified

567 _dispatch[_collections.UserString.__repr__] = _pprint_user_string
568
569 def _safe_repr(self, object, context, maxlevels, level):
570 # Return triple (repr_string, isreadable, isrecursive).
571 typ = type(object)
572 if typ in _builtin_scalars:
573 return repr(object), True, False
574
575 r = getattr(typ, "__repr__", None)
576
577 if issubclass(typ, int) and r is int.__repr__:
578 if self._underscore_numbers:
579 return f"{object:_d}", True, False
580 else:
581 return repr(object), True, False
582
583 if issubclass(typ, dict) and r is dict.__repr__:
584 if not object:
585 return "{}", True, False
586 objid = id(object)
587 if maxlevels and level >= maxlevels:
588 return "{...}", False, objid in context
589 if objid in context:
590 return _recursion(object), False, True
591 context[objid] = 1
592 readable = True
593 recursive = False
594 components = []
595 append = components.append
596 level += 1
597 if self._sort_dicts:
598 items = sorted(object.items(), key=_safe_tuple)
599 else:
600 items = object.items()
601 for k, v in items:
602 krepr, kreadable, krecur = self.format(
603 k, context, maxlevels, level)
604 vrepr, vreadable, vrecur = self.format(
605 v, context, maxlevels, level)
606 append("%s: %s" % (krepr, vrepr))
607 readable = readable and kreadable and vreadable
608 if krecur or vrecur:
609 recursive = True
610 del context[objid]
611 return "{%s}" % ", ".join(components), readable, recursive
612
613 if (issubclass(typ, list) and r is list.__repr__) or \
614 (issubclass(typ, tuple) and r is tuple.__repr__):
615 if issubclass(typ, list):
616 if not object:
617 return "[]", True, False
618 format = "[%s]"
619 elif len(object) == 1:
620 format = "(%s,)"
621 else:
622 if not object:
623 return "()", True, False
624 format = "(%s)"
625 objid = id(object)
626 if maxlevels and level >= maxlevels:

Callers 4

formatMethod · 0.95
safereprFunction · 0.80
isreadableFunction · 0.80
isrecursiveFunction · 0.80

Calls 11

formatMethod · 0.95
reprFunction · 0.85
getattrFunction · 0.85
issubclassFunction · 0.85
idFunction · 0.85
_recursionFunction · 0.85
sortedFunction · 0.85
lenFunction · 0.85
itemsMethod · 0.45
joinMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected