diff options
author | Georg Brandl <georg@python.org> | 2007-09-12 19:00:10 +0000 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-09-12 19:00:10 +0000 |
commit | fed6bb7d7061f940c4fe4ed43526ea5bbf968de2 (patch) | |
tree | bdba37e297d643c200e406e08f9dcab6a9b9fc2f /Lib/repr.py | |
parent | 226910f52d94beededad90e9de644c0da663d72a (diff) | |
download | cpython-git-fed6bb7d7061f940c4fe4ed43526ea5bbf968de2.tar.gz |
Bug #1153: repr.repr() now doesn't require set and dictionary items
to be orderable to properly represent them.
(backport from rev. 58122)
Diffstat (limited to 'Lib/repr.py')
-rw-r--r-- | Lib/repr.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/Lib/repr.py b/Lib/repr.py index 59015b1a9b..3c26cc39a2 100644 --- a/Lib/repr.py +++ b/Lib/repr.py @@ -1,4 +1,4 @@ -"""Redo the `...` (representation) but with limits on most sizes.""" +"""Redo the builtin repr() (representation) but with limits on most sizes.""" __all__ = ["Repr","repr"] @@ -62,11 +62,11 @@ class Repr: return self._repr_iterable(x, level, header, '])', self.maxarray) def repr_set(self, x, level): - x = sorted(x) + x = _possibly_sorted(x) return self._repr_iterable(x, level, 'set([', '])', self.maxset) def repr_frozenset(self, x, level): - x = sorted(x) + x = _possibly_sorted(x) return self._repr_iterable(x, level, 'frozenset([', '])', self.maxfrozenset) @@ -80,7 +80,7 @@ class Repr: newlevel = level - 1 repr1 = self.repr1 pieces = [] - for key in islice(sorted(x), self.maxdict): + for key in islice(_possibly_sorted(x), self.maxdict): keyrepr = repr1(key, newlevel) valrepr = repr1(x[key], newlevel) pieces.append('%s: %s' % (keyrepr, valrepr)) @@ -110,7 +110,7 @@ class Repr: s = __builtin__.repr(x) # Bugs in x.__repr__() can cause arbitrary # exceptions -- then make up something - except: + except Exception: return '<%s instance at %x>' % (x.__class__.__name__, id(x)) if len(s) > self.maxstring: i = max(0, (self.maxstring-3)//2) @@ -118,5 +118,15 @@ class Repr: s = s[:i] + '...' + s[len(s)-j:] return s + +def _possibly_sorted(x): + # Since not all sequences of items can be sorted and comparison + # functions may raise arbitrary exceptions, return an unsorted + # sequence in that case. + try: + return sorted(x) + except Exception: + return list(x) + aRepr = Repr() repr = aRepr.repr |