[pypy-commit] pypy py3.3: add SimpleNamespace.__repr__

pjenvey noreply at buildbot.pypy.org
Sat Aug 2 00:23:27 CEST 2014


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3.3
Changeset: r72640:6b3258e026e3
Date: 2014-08-01 15:22 -0700
http://bitbucket.org/pypy/pypy/changeset/6b3258e026e3/

Log:	add SimpleNamespace.__repr__

diff --git a/pypy/module/sys/app.py b/pypy/module/sys/app.py
--- a/pypy/module/sys/app.py
+++ b/pypy/module/sys/app.py
@@ -129,6 +129,17 @@
     def __dict__(self):
         return self._ns
 
+    def __repr__(self, recurse=set()):
+        ident = id(self)
+        if ident in recurse:
+            return "namespace(...)"
+        recurse.add(ident)
+        try:
+            pairs = ('%s=%r' % item for item in sorted(self._ns.items()))
+            return "namespace(%s)" % ', '.join(pairs)
+        finally:
+            recurse.remove(ident)
+
 
 implementation = SimpleNamespace(
     name='pypy',
diff --git a/pypy/module/sys/test/test_sysmodule.py b/pypy/module/sys/test/test_sysmodule.py
--- a/pypy/module/sys/test/test_sysmodule.py
+++ b/pypy/module/sys/test/test_sysmodule.py
@@ -519,6 +519,9 @@
         # PEP 421 requires that .name be lower case.
         assert sys.implementation.name == sys.implementation.name.lower()
 
+        ns1 = type(sys.implementation)(x=1, y=2, w=3)
+        assert repr(ns1) == "namespace(w=3, x=1, y=2)"
+
     def test_settrace(self):
         import sys
         counts = []


More information about the pypy-commit mailing list