Skip to content

Commit 4974c99

Browse files
wanda-phiwhitequark
authored andcommitted
sim: improve error messages for weird objects passed as testbenches.
1 parent ebeffbe commit 4974c99

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

amaranth/sim/core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ def add_clock(self, period, *, phase=None, domain="sync", if_exists=False):
132132

133133
@staticmethod
134134
def _check_function(function, *, kind):
135+
if inspect.isasyncgenfunction(function):
136+
raise TypeError(
137+
f"Cannot add a {kind} {function!r} because it is an async generator function "
138+
f"(there is likely a stray `yield` in the function)")
139+
if inspect.iscoroutine(function):
140+
raise TypeError(
141+
f"Cannot add a {kind} {function!r} because it is a coroutine object instead "
142+
f"of a function (pass the function itself instead of calling it)")
143+
if inspect.isgenerator(function) or inspect.isasyncgen(function):
144+
raise TypeError(
145+
f"Cannot add a {kind} {function!r} because it is a generator object instead "
146+
f"of a function (pass the function itself instead of calling it)")
135147
if not (inspect.isgeneratorfunction(function) or inspect.iscoroutinefunction(function)):
136148
raise TypeError(
137149
f"Cannot add a {kind} {function!r} because it is not an async function or "

tests/test_sim.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,8 @@ def test_add_process_wrong(self):
727727
def test_add_process_wrong_generator(self):
728728
with self.assertSimulation(Module()) as sim:
729729
with self.assertRaisesRegex(TypeError,
730-
r"^Cannot add a process <.+?> because it is not an async function or "
731-
r"generator function$"):
730+
r"^Cannot add a process <.+?> because it is a generator object instead of "
731+
r"a function \(pass the function itself instead of calling it\)$"):
732732
def process():
733733
yield Delay()
734734
sim.add_process(process())
@@ -743,12 +743,39 @@ def test_add_testbench_wrong(self):
743743
def test_add_testbench_wrong_generator(self):
744744
with self.assertSimulation(Module()) as sim:
745745
with self.assertRaisesRegex(TypeError,
746-
r"^Cannot add a testbench <.+?> because it is not an async function or "
747-
r"generator function$"):
746+
r"^Cannot add a testbench <.+?> because it is a generator object instead of "
747+
r"a function \(pass the function itself instead of calling it\)$"):
748748
def testbench():
749749
yield Delay()
750750
sim.add_testbench(testbench())
751751

752+
def test_add_testbench_wrong_coroutine(self):
753+
with self.assertSimulation(Module()) as sim:
754+
with self.assertRaisesRegex(TypeError,
755+
r"^Cannot add a testbench <.+?> because it is a coroutine object instead of "
756+
r"a function \(pass the function itself instead of calling it\)$"):
757+
async def testbench():
758+
pass
759+
sim.add_testbench(testbench())
760+
761+
def test_add_testbench_wrong_async_generator(self):
762+
with self.assertSimulation(Module()) as sim:
763+
with self.assertRaisesRegex(TypeError,
764+
r"^Cannot add a testbench <.+?> because it is a generator object instead of "
765+
r"a function \(pass the function itself instead of calling it\)$"):
766+
async def testbench():
767+
yield Delay()
768+
sim.add_testbench(testbench())
769+
770+
def test_add_testbench_wrong_async_generator_func(self):
771+
with self.assertSimulation(Module()) as sim:
772+
with self.assertRaisesRegex(TypeError,
773+
r"^Cannot add a testbench <.+?> because it is an async generator function "
774+
r"\(there is likely a stray `yield` in the function\)$"):
775+
async def testbench():
776+
yield Delay()
777+
sim.add_testbench(testbench)
778+
752779
def test_add_clock_wrong_twice(self):
753780
m = Module()
754781
s = Signal()
@@ -2026,15 +2053,6 @@ async def testbench(ctx):
20262053
self.assertTrue(reached_tb)
20272054
self.assertTrue(reached_proc)
20282055

2029-
def test_bug_1363(self):
2030-
sim = Simulator(Module())
2031-
with self.assertRaisesRegex(TypeError,
2032-
r"^Cannot add a testbench <.+?> because it is not an async function or "
2033-
r"generator function$"):
2034-
async def testbench():
2035-
yield Delay()
2036-
sim.add_testbench(testbench())
2037-
20382056
def test_issue_1368(self):
20392057
sim = Simulator(Module())
20402058
async def testbench(ctx):

0 commit comments

Comments
 (0)