From: whitequark Date: Sun, 12 Apr 2020 04:47:40 +0000 (+0000) Subject: hdl.rec: improve repr() for Layout. X-Git-Tag: working_23jun2020~12 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7a08901117b54f7a31b82d053fae2a4541d1e52f;p=nmigen.git hdl.rec: improve repr() for Layout. Fixes #326. --- diff --git a/nmigen/hdl/rec.py b/nmigen/hdl/rec.py index 54b270a..b60fb00 100644 --- a/nmigen/hdl/rec.py +++ b/nmigen/hdl/rec.py @@ -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): diff --git a/nmigen/test/test_hdl_rec.py b/nmigen/test/test_hdl_rec.py index dd9dd40..09af444 100644 --- a/nmigen/test/test_hdl_rec.py +++ b/nmigen/test/test_hdl_rec.py @@ -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 "