[Python-checkins] cpython (merge 3.2 -> default): Merge

raymond.hettinger python-checkins at python.org
Sat Nov 5 21:43:36 CET 2011


http://hg.python.org/cpython/rev/7144da4c2d26
changeset:   73387:7144da4c2d26
parent:      73385:733e1074e9cd
parent:      73386:970ec22ab368
user:        Raymond Hettinger <python at rcn.com>
date:        Sat Nov 05 13:39:57 2011 -0700
summary:
  Merge

files:
  Lib/collections/__init__.py  |  8 ++++++--
  Lib/test/test_collections.py |  6 ++++++
  2 files changed, 12 insertions(+), 2 deletions(-)


diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -585,8 +585,12 @@
     def __repr__(self):
         if not self:
             return '%s()' % self.__class__.__name__
-        items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
-        return '%s({%s})' % (self.__class__.__name__, items)
+        try:
+            items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
+            return '%s({%s})' % (self.__class__.__name__, items)
+        except TypeError:
+            # handle case where values are not orderable
+            return '{0}({1!r})'.format(self.__class__.__name__, dict(self))
 
     # Multiset-style mathematical operations discussed in:
     #       Knuth TAOCP Volume II section 4.6.3 exercise 19
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -969,6 +969,12 @@
         self.assertEqual(dict(+c), dict(c=5, d=10, e=15, g=40))
         self.assertEqual(dict(-c), dict(a=5))
 
+    def test_repr_nonsortable(self):
+        c = Counter(a=2, b=None)
+        r = repr(c)
+        self.assertIn("'a': 2", r)
+        self.assertIn("'b': None", r)
+    
     def test_helper_function(self):
         # two paths, one for real dicts and one for other mappings
         elems = list('abracadabra')

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list