summaryrefslogtreecommitdiffstats
path: root/Lib/pprint.py
diff options
context:
space:
mode:
authorCarl Bordum Hansen <carl@bordum.dk>2019-06-26 23:13:18 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-06-26 23:13:18 (GMT)
commit06a8916cf465f139dca958685b0da899364b8a8c (patch)
tree9109f49858378392227318cb05ad9b0ea1a1052c /Lib/pprint.py
parentd52a83a3d471ff3c7e9ebfa1731765e5334f7c24 (diff)
downloadcpython-06a8916cf465f139dca958685b0da899364b8a8c.zip
cpython-06a8916cf465f139dca958685b0da899364b8a8c.tar.gz
cpython-06a8916cf465f139dca958685b0da899364b8a8c.tar.bz2
bpo-37376: pprint support for SimpleNamespace (GH-14318)
https://bugs.python.org/issue37376
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r--Lib/pprint.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 4bfcc31..7c1118a 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -342,6 +342,33 @@ class PrettyPrinter:
_dispatch[_types.MappingProxyType.__repr__] = _pprint_mappingproxy
+ def _pprint_simplenamespace(self, object, stream, indent, allowance, context, level):
+ if type(object) is _types.SimpleNamespace:
+ # The SimpleNamespace repr is "namespace" instead of the class
+ # name, so we do the same here. For subclasses; use the class name.
+ cls_name = 'namespace'
+ else:
+ cls_name = object.__class__.__name__
+ indent += len(cls_name) + 1
+ delimnl = ',\n' + ' ' * indent
+ items = object.__dict__.items()
+ last_index = len(items) - 1
+
+ stream.write(cls_name + '(')
+ for i, (key, ent) in enumerate(items):
+ stream.write(key)
+ stream.write('=')
+
+ last = i == last_index
+ self._format(ent, stream, indent + len(key) + 1,
+ allowance if last else 1,
+ context, level)
+ if not last:
+ stream.write(delimnl)
+ stream.write(')')
+
+ _dispatch[_types.SimpleNamespace.__repr__] = _pprint_simplenamespace
+
def _format_dict_items(self, items, stream, indent, allowance, context,
level):
write = stream.write