[Python-checkins] r58123 - in python/branches/release25-maint: Lib/repr.py Lib/test/test_repr.py Misc/NEWS

georg.brandl python-checkins at python.org
Wed Sep 12 21:00:10 CEST 2007


Author: georg.brandl
Date: Wed Sep 12 21:00:10 2007
New Revision: 58123

Modified:
   python/branches/release25-maint/Lib/repr.py
   python/branches/release25-maint/Lib/test/test_repr.py
   python/branches/release25-maint/Misc/NEWS
Log:
Bug #1153: repr.repr() now doesn't require set and dictionary items
to be orderable to properly represent them.
 (backport from rev. 58122)

Modified: python/branches/release25-maint/Lib/repr.py
==============================================================================
--- python/branches/release25-maint/Lib/repr.py	(original)
+++ python/branches/release25-maint/Lib/repr.py	Wed Sep 12 21:00:10 2007
@@ -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 @@
         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 @@
         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 @@
             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 @@
             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

Modified: python/branches/release25-maint/Lib/test/test_repr.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_repr.py	(original)
+++ python/branches/release25-maint/Lib/test/test_repr.py	Wed Sep 12 21:00:10 2007
@@ -197,6 +197,16 @@
         x = classmethod(C.foo)
         self.failUnless(repr(x).startswith('<classmethod object at 0x'))
 
+    def test_unsortable(self):
+        # Repr.repr() used to call sorted() on sets, frozensets and dicts
+        # without taking into account that not all objects are comparable
+        x = set([1j, 2j, 3j])
+        y = frozenset(x)
+        z = {1j: 1, 2j: 2}
+        r(x)
+        r(y)
+        r(z)
+
 def touch(path, text=''):
     fp = open(path, 'w')
     fp.write(text)

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Wed Sep 12 21:00:10 2007
@@ -32,6 +32,9 @@
 Library
 -------
 
+- Bug #1153: repr.repr() now doesn't require set and dictionary items
+  to be orderable to properly represent them.
+
 - Bug #1709599: Run test_1565150 only if the file system is NTFS.
 
 - When encountering a password-protected robots.txt file the RobotFileParser


More information about the Python-checkins mailing list