From ec3a21939e11cb2d4c70f9ad6ea3891964398a55 Mon Sep 17 00:00:00 2001 From: Jaro Habiger Date: Sun, 26 Jan 2020 18:35:41 +0100 Subject: [PATCH] build.dsl: allow strings to be used as connector numbers. Fixes #311. --- nmigen/build/dsl.py | 10 ++++++---- nmigen/test/test_build_dsl.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/nmigen/build/dsl.py b/nmigen/build/dsl.py index 721f980..9924e12 100644 --- a/nmigen/build/dsl.py +++ b/nmigen/build/dsl.py @@ -14,8 +14,9 @@ class Pins: if conn is not None: conn_name, conn_number = conn - if not (isinstance(conn_name, str) and isinstance(conn_number, int)): - raise TypeError("Connector must be None or a pair of string and integer, not {!r}" + if not (isinstance(conn_name, str) and isinstance(conn_number, (int, str))): + raise TypeError("Connector must be None or a pair of string (connector name) and " + "integer/string (connector number), not {!r}" .format(conn)) names = ["{}_{}:{}".format(conn_name, conn_number, name) for name in names] @@ -236,8 +237,9 @@ class Connector: if conn is not None: conn_name, conn_number = conn - if not (isinstance(conn_name, str) and isinstance(conn_number, int)): - raise TypeError("Connector must be None or a pair of string and integer, not {!r}" + if not (isinstance(conn_name, str) and isinstance(conn_number, (int, str))): + raise TypeError("Connector must be None or a pair of string (connector name) and " + "integer/string (connector number), not {!r}" .format(conn)) for conn_pin, plat_pin in mapping.items(): diff --git a/nmigen/test/test_build_dsl.py b/nmigen/test/test_build_dsl.py index a613d12..bf901bf 100644 --- a/nmigen/test/test_build_dsl.py +++ b/nmigen/test/test_build_dsl.py @@ -25,6 +25,8 @@ class PinsTestCase(FHDLTestCase): def test_conn(self): p = Pins("0 1 2", conn=("pmod", 0)) self.assertEqual(list(p), ["pmod_0:0", "pmod_0:1", "pmod_0:2"]) + p = Pins("0 1 2", conn=("pmod", "a")) + self.assertEqual(list(p), ["pmod_a:0", "pmod_a:1", "pmod_a:2"]) def test_map_names(self): p = Pins("0 1 2", conn=("pmod", 0)) @@ -53,6 +55,12 @@ class PinsTestCase(FHDLTestCase): msg="Direction must be one of \"i\", \"o\", \"oe\", or \"io\", not 'wrong'"): p = Pins("A0 A1", dir="wrong") + def test_wrong_conn(self): + with self.assertRaises(TypeError, + msg="Connector must be None or a pair of string (connector name) and " + "integer/string (connector number), not ('foo', None)"): + p = Pins("A0 A1", conn=("foo", None)) + def test_wrong_map_names(self): p = Pins("0 1 2", conn=("pmod", 0)) mapping = { @@ -295,6 +303,17 @@ class ConnectorTestCase(FHDLTestCase): ("10", "expansion_0:7"), ])) + def test_str_name(self): + c = Connector("ext", "A", "0 1 2") + self.assertEqual(c.name, "ext") + self.assertEqual(c.number, "A") + + def test_conn_wrong_name(self): + with self.assertRaises(TypeError, + msg="Connector must be None or a pair of string (connector name) and " + "integer/string (connector number), not ('foo', None)"): + Connector("ext", "A", "0 1 2", conn=("foo", None)) + def test_wrong_io(self): with self.assertRaises(TypeError, msg="Connector I/Os must be a dictionary or a string, not []"): -- 2.30.2