_utils: don't crash trying to flatten() strings.
authorwhitequark <whitequark@whitequark.org>
Sat, 11 Dec 2021 07:39:35 +0000 (07:39 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 31 Dec 2021 20:04:08 +0000 (20:04 +0000)
Fixes #614.

nmigen/_utils.py
tests/test_hdl_ast.py

index d04d720328cbef018f131bac7695cb257d0161d7..3d05ff20c5da7c50915ed322408d01d920e0e88d 100644 (file)
@@ -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):
index 90f3d26d7d7cbd509ff502bce661203a0891a070..fb25f39a7478dfc6808424a5ec47cfb46c55654c 100644 (file)
@@ -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):