From fc2018d415cba249b5a72b873e343e4296a2e85c Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 11 Dec 2021 07:39:35 +0000 Subject: [PATCH] _utils: don't crash trying to flatten() strings. Fixes #614. --- nmigen/_utils.py | 6 +++--- tests/test_hdl_ast.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/nmigen/_utils.py b/nmigen/_utils.py index d04d720..3d05ff2 100644 --- a/nmigen/_utils.py +++ b/nmigen/_utils.py @@ -15,10 +15,10 @@ __all__ = ["flatten", "union" , "log2_int", "bits_for", "memoize", "final", "dep def flatten(i): for e in i: - if isinstance(e, Iterable): - yield from flatten(e) - else: + if isinstance(e, str) or not isinstance(e, Iterable): yield e + else: + yield from flatten(e) def union(i, start=None): diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index 90f3d26..fb25f39 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -740,6 +740,11 @@ class CatTestCase(FHDLTestCase): def test_cast(self): c = Cat(1, 0) self.assertEqual(repr(c), "(cat (const 1'd1) (const 1'd0))") + + def test_str_wrong(self): + with self.assertRaisesRegex(TypeError, + r"^Object 'foo' cannot be converted to an nMigen value$"): + Cat("foo") class ReplTestCase(FHDLTestCase): -- 2.30.2