sim.pysim: refuse to write VCD files with whitespace in signal names.
authorwhitequark <whitequark@whitequark.org>
Sat, 11 Dec 2021 11:12:25 +0000 (11:12 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 31 Dec 2021 20:11:33 +0000 (20:11 +0000)
Closes #595.

nmigen/sim/pysim.py
tests/test_sim.py

index ec98fb0e8b3bf569d14fa0b6b52ed7fd619ab4ea..0d8b65908413acf9d659b0ff6e308c5dd8fecfaf 100644 (file)
@@ -1,5 +1,6 @@
 from contextlib import contextmanager
 import itertools
+import re
 from vcd import VCDWriter
 from vcd.gtkw import GTKWSave
 
@@ -94,6 +95,10 @@ class _VCDWriter:
                 var_init = signal.reset
 
             for (*var_scope, var_name) in names:
+                if re.search(r"[ \t\r\n]", var_name):
+                    raise NameError("Signal '{}.{}' contains a whitespace character"
+                                    .format(".".join(var_scope), var_name))
+
                 suffix = None
                 while True:
                     try:
index a64e90f0d6cc9003ffd8eca5eecfbaccb1036a12..8d29bfb66620b494461fbe55816ca8c3c39bc9c5 100644 (file)
@@ -806,8 +806,9 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
         sim.run_until(1e-5)
         with self.assertRaisesRegex(ValueError,
                 r"^Cannot start writing waveforms after advancing simulation time$"):
-            with sim.write_vcd(open(os.path.devnull, "wt")):
-                pass
+            with open(os.path.devnull, "w") as f:
+                with sim.write_vcd(f):
+                    pass
 
 
 class SimulatorRegressionTestCase(FHDLTestCase):
@@ -827,3 +828,15 @@ class SimulatorRegressionTestCase(FHDLTestCase):
             self.assertEqual((yield -(Const(0b11, 2).as_signed())), 1)
         sim.add_process(process)
         sim.run()
+
+    def test_bug_595(self):
+        dut = Module()
+        with dut.FSM(name="name with space"):
+            with dut.State(0):
+                pass
+        sim = Simulator(dut)
+        with self.assertRaisesRegex(NameError,
+                r"^Signal 'top\.name with space_state' contains a whitespace character$"):
+            with open(os.path.devnull, "w") as f:
+                with sim.write_vcd(f):
+                    sim.run()