[pypy-svn] r75082 - in pypy/branch/fast-forward/pypy/lib: . app_test

benjamin at codespeak.net benjamin at codespeak.net
Thu Jun 3 23:17:22 CEST 2010


Author: benjamin
Date: Thu Jun  3 23:17:21 2010
New Revision: 75082

Modified:
   pypy/branch/fast-forward/pypy/lib/_structseq.py
   pypy/branch/fast-forward/pypy/lib/app_test/test_structseq.py
Log:
give structseq a proper repr

Modified: pypy/branch/fast-forward/pypy/lib/_structseq.py
==============================================================================
--- pypy/branch/fast-forward/pypy/lib/_structseq.py	(original)
+++ pypy/branch/fast-forward/pypy/lib/_structseq.py	Thu Jun  3 23:17:21 2010
@@ -61,6 +61,8 @@
         dict['__new__'] = structseq_new
         dict['__reduce__'] = structseq_reduce
         dict['__setattr__'] = structseq_setattr
+        dict['__repr__'] = structseq_repr
+        dict['_name'] = dict.get('name', '')
         return type.__new__(metacls, classname, (tuple,), dict)
 
 
@@ -105,3 +107,12 @@
 def structseq_setattr(self, attr, value):
     raise AttributeError("%r object has no attribute %r" % (
         self.__class__.__name__, attr))
+
+def structseq_repr(self):
+    fields = {}
+    for field in type(self).__dict__.values():
+        if isinstance(field, structseqfield):
+            fields[field._index] = field
+    parts = ["%s=%r" % (fields[index].__name__, value)
+             for index, value in enumerate(self)]
+    return "%s(%s)" % (self._name, ", ".join(parts))

Modified: pypy/branch/fast-forward/pypy/lib/app_test/test_structseq.py
==============================================================================
--- pypy/branch/fast-forward/pypy/lib/app_test/test_structseq.py	(original)
+++ pypy/branch/fast-forward/pypy/lib/app_test/test_structseq.py	Thu Jun  3 23:17:21 2010
@@ -5,6 +5,8 @@
 class mydata:
     __metaclass__ = structseqtype
 
+    name = "mydata"
+
     st_mode  = structseqfield(0, "protection bits")
     st_ino   = structseqfield(1)
     st_dev   = structseqfield(2)
@@ -77,3 +79,9 @@
 def test_no_extra_assignments():
     x = mydata(range(100, 113))
     py.test.raises((TypeError, AttributeError), "x.some_random_attribute = 1")
+
+def test_repr():
+    x = mydata(range(100, 113))
+    r = repr(x)
+    assert r.startswith("mydata(st_mode=100, ")
+    assert r.endswith(")")



More information about the Pypy-commit mailing list