Skip to content

Commit 94becb5

Browse files
wanda-phiwhitequark
authored andcommitted
sim: improve error messages for weird objects passed as testbenches.
1 parent 7a58b62 commit 94becb5

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

amaranth/sim/core.py

+12
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

+31-13
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ def test_add_process_wrong(self):
716716
def test_add_process_wrong_generator(self):
717717
with self.assertSimulation(Module()) as sim:
718718
with self.assertRaisesRegex(TypeError,
719-
r"^Cannot add a process <.+?> because it is not an async function or "
720-
r"generator function$"):
719+
r"^Cannot add a process <.+?> because it is a generator object instead of "
720+
r"a function \(pass the function itself instead of calling it\)$"):
721721
def process():
722722
yield Delay()
723723
sim.add_process(process())
@@ -732,12 +732,39 @@ def test_add_testbench_wrong(self):
732732
def test_add_testbench_wrong_generator(self):
733733
with self.assertSimulation(Module()) as sim:
734734
with self.assertRaisesRegex(TypeError,
735-
r"^Cannot add a testbench <.+?> because it is not an async function or "
736-
r"generator function$"):
735+
r"^Cannot add a testbench <.+?> because it is a generator object instead of "
736+
r"a function \(pass the function itself instead of calling it\)$"):
737737
def testbench():
738738
yield Delay()
739739
sim.add_testbench(testbench())
740740

741+
def test_add_testbench_wrong_coroutine(self):
742+
with self.assertSimulation(Module()) as sim:
743+
with self.assertRaisesRegex(TypeError,
744+
r"^Cannot add a testbench <.+?> because it is a coroutine object instead of "
745+
r"a function \(pass the function itself instead of calling it\)$"):
746+
async def testbench():
747+
pass
748+
sim.add_testbench(testbench())
749+
750+
def test_add_testbench_wrong_async_generator(self):
751+
with self.assertSimulation(Module()) as sim:
752+
with self.assertRaisesRegex(TypeError,
753+
r"^Cannot add a testbench <.+?> because it is a generator object instead of "
754+
r"a function \(pass the function itself instead of calling it\)$"):
755+
async def testbench():
756+
yield Delay()
757+
sim.add_testbench(testbench())
758+
759+
def test_add_testbench_wrong_async_generator_func(self):
760+
with self.assertSimulation(Module()) as sim:
761+
with self.assertRaisesRegex(TypeError,
762+
r"^Cannot add a testbench <.+?> because it is an async generator function "
763+
r"\(there is likely a stray `yield` in the function\)$"):
764+
async def testbench():
765+
yield Delay()
766+
sim.add_testbench(testbench)
767+
741768
def test_add_clock_wrong_twice(self):
742769
m = Module()
743770
s = Signal()
@@ -2015,15 +2042,6 @@ async def testbench(ctx):
20152042
self.assertTrue(reached_tb)
20162043
self.assertTrue(reached_proc)
20172044

2018-
def test_bug_1363(self):
2019-
sim = Simulator(Module())
2020-
with self.assertRaisesRegex(TypeError,
2021-
r"^Cannot add a testbench <.+?> because it is not an async function or "
2022-
r"generator function$"):
2023-
async def testbench():
2024-
yield Delay()
2025-
sim.add_testbench(testbench())
2026-
20272045
def test_issue_1368(self):
20282046
sim = Simulator(Module())
20292047
async def testbench(ctx):

0 commit comments

Comments
 (0)