hdl.rec: improve repr() for Layout.
authorwhitequark <whitequark@whitequark.org>
Sun, 12 Apr 2020 04:47:40 +0000 (04:47 +0000)
committerwhitequark <whitequark@whitequark.org>
Sun, 12 Apr 2020 04:47:40 +0000 (04:47 +0000)
Fixes #326.

nmigen/hdl/rec.py
nmigen/test/test_hdl_rec.py

index 54b270afeeb799101f5cf7288de1fefa995e7be7..b60fb00fd74b6633912d91afec9ba3528f45be42 100644 (file)
@@ -74,6 +74,15 @@ class Layout:
     def __eq__(self, other):
         return self.fields == other.fields
 
+    def __repr__(self):
+        field_reprs = []
+        for name, shape, dir in self:
+            if dir == DIR_NONE:
+                field_reprs.append("({!r}, {!r})".format(name, shape))
+            else:
+                field_reprs.append("({!r}, {!r}, Direction.{})".format(name, shape, dir.name))
+        return "Layout([{}])".format(", ".join(field_reprs))
+
 
 # Unlike most Values, Record *can* be subclassed.
 class Record(Value):
index dd9dd404c49c08129864f2f4164a453a8743a6bc..09af444bfee169ba875bdba006d58c9e67152c9b 100644 (file)
@@ -59,6 +59,13 @@ class LayoutTestCase(FHDLTestCase):
         ])
         self.assertEqual(layout["a", "c"], expect)
 
+    def test_repr(self):
+        self.assertEqual(repr(Layout([("a", 1), ("b", signed(2))])),
+                         "Layout([('a', unsigned(1)), ('b', signed(2))])")
+        self.assertEqual(repr(Layout([("a", 1), ("b", [("c", signed(3))])])),
+                         "Layout([('a', unsigned(1)), "
+                            "('b', Layout([('c', signed(3))]))])")
+
     def test_wrong_field(self):
         with self.assertRaises(TypeError,
                 msg="Field (1,) has invalid layout: should be either (name, shape) or "