]> granicus.if.org Git - python/commitdiff
Issue #23502: The pprint module now supports mapping proxies.
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 24 Mar 2015 17:31:50 +0000 (19:31 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 24 Mar 2015 17:31:50 +0000 (19:31 +0200)
In particular the __dict__ attributes of building types.

Lib/pprint.py
Lib/test/test_pprint.py
Misc/NEWS

index 5b65f579d9f5d5a7204232f8abd1b9f3b914b4fd..9031a0b6ef03ed2c3398c0c073e1b72e2051ae3f 100644 (file)
@@ -36,6 +36,7 @@ saferepr()
 
 import re
 import sys as _sys
+import types as _types
 from collections import OrderedDict as _OrderedDict
 from io import StringIO as _StringIO
 
@@ -313,6 +314,14 @@ class PrettyPrinter:
 
     _dispatch[bytearray.__repr__] = _pprint_bytearray
 
+    def _pprint_mappingproxy(self, object, stream, indent, allowance, context, level):
+        stream.write('mappingproxy(')
+        self._format(object.copy(), stream, indent + 13, allowance + 1,
+                     context, level)
+        stream.write(')')
+
+    _dispatch[_types.MappingProxyType.__repr__] = _pprint_mappingproxy
+
     def _format_dict_items(self, items, stream, indent, allowance, context,
                            level):
         write = stream.write
index 50d7d59ecc0396b251b90c6cb74a16b9ae101c4a..3a798d946ca013ab31fce973389bf489b20c59df 100644 (file)
@@ -7,6 +7,7 @@ import test.test_set
 import random
 import collections
 import itertools
+import types
 
 # list, tuple and dict subclasses that do or don't overwrite __repr__
 class list2(list):
@@ -271,6 +272,34 @@ class QueryTestCase(unittest.TestCase):
  'a': 6,
  'lazy': 7,
  'dog': 8}""")
+
+    def test_mapping_proxy(self):
+        words = 'the quick brown fox jumped over a lazy dog'.split()
+        d = dict(zip(words, itertools.count()))
+        m = types.MappingProxyType(d)
+        self.assertEqual(pprint.pformat(m), """\
+mappingproxy({'a': 6,
+              'brown': 2,
+              'dog': 8,
+              'fox': 3,
+              'jumped': 4,
+              'lazy': 7,
+              'over': 5,
+              'quick': 1,
+              'the': 0})""")
+        d = collections.OrderedDict(zip(words, itertools.count()))
+        m = types.MappingProxyType(d)
+        self.assertEqual(pprint.pformat(m), """\
+mappingproxy({'the': 0,
+              'quick': 1,
+              'brown': 2,
+              'fox': 3,
+              'jumped': 4,
+              'over': 5,
+              'a': 6,
+              'lazy': 7,
+              'dog': 8})""")
+
     def test_subclassing(self):
         o = {'names with spaces': 'should be presented using repr()',
              'others.should.not.be': 'like.this'}
index 139a681467e3fa5ed87cbdac7de95ccc049845d8..abd072d7a8c498fa4182af775715e60b5a544180 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #23502: The pprint module now supports mapping proxies.
+
 - Issue #17530: pprint now wraps long bytes objects and bytearrays.
 
 - Issue #22687: Fixed some corner cases in breaking words in tetxtwrap.