Skip to content

sim: improve error messages for weird objects passed as testbenches. #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions amaranth/sim/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ def add_clock(self, period, *, phase=None, domain="sync", if_exists=False):

@staticmethod
def _check_function(function, *, kind):
if inspect.isasyncgenfunction(function):
raise TypeError(
f"Cannot add a {kind} {function!r} because it is an async generator function "
f"(there is likely a stray `yield` in the function)")
if inspect.iscoroutine(function):
raise TypeError(
f"Cannot add a {kind} {function!r} because it is a coroutine object instead "
f"of a function (pass the function itself instead of calling it)")
if inspect.isgenerator(function) or inspect.isasyncgen(function):
raise TypeError(
f"Cannot add a {kind} {function!r} because it is a generator object instead "
f"of a function (pass the function itself instead of calling it)")
if not (inspect.isgeneratorfunction(function) or inspect.iscoroutinefunction(function)):
raise TypeError(
f"Cannot add a {kind} {function!r} because it is not an async function or "
Expand Down
44 changes: 31 additions & 13 deletions tests/test_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,8 @@ def test_add_process_wrong(self):
def test_add_process_wrong_generator(self):
with self.assertSimulation(Module()) as sim:
with self.assertRaisesRegex(TypeError,
r"^Cannot add a process <.+?> because it is not an async function or "
r"generator function$"):
r"^Cannot add a process <.+?> because it is a generator object instead of "
r"a function \(pass the function itself instead of calling it\)$"):
def process():
yield Delay()
sim.add_process(process())
Expand All @@ -732,12 +732,39 @@ def test_add_testbench_wrong(self):
def test_add_testbench_wrong_generator(self):
with self.assertSimulation(Module()) as sim:
with self.assertRaisesRegex(TypeError,
r"^Cannot add a testbench <.+?> because it is not an async function or "
r"generator function$"):
r"^Cannot add a testbench <.+?> because it is a generator object instead of "
r"a function \(pass the function itself instead of calling it\)$"):
def testbench():
yield Delay()
sim.add_testbench(testbench())

def test_add_testbench_wrong_coroutine(self):
with self.assertSimulation(Module()) as sim:
with self.assertRaisesRegex(TypeError,
r"^Cannot add a testbench <.+?> because it is a coroutine object instead of "
r"a function \(pass the function itself instead of calling it\)$"):
async def testbench():
pass
sim.add_testbench(testbench())

def test_add_testbench_wrong_async_generator(self):
with self.assertSimulation(Module()) as sim:
with self.assertRaisesRegex(TypeError,
r"^Cannot add a testbench <.+?> because it is a generator object instead of "
r"a function \(pass the function itself instead of calling it\)$"):
async def testbench():
yield Delay()
sim.add_testbench(testbench())

def test_add_testbench_wrong_async_generator_func(self):
with self.assertSimulation(Module()) as sim:
with self.assertRaisesRegex(TypeError,
r"^Cannot add a testbench <.+?> because it is an async generator function "
r"\(there is likely a stray `yield` in the function\)$"):
async def testbench():
yield Delay()
sim.add_testbench(testbench)

def test_add_clock_wrong_twice(self):
m = Module()
s = Signal()
Expand Down Expand Up @@ -2015,15 +2042,6 @@ async def testbench(ctx):
self.assertTrue(reached_tb)
self.assertTrue(reached_proc)

def test_bug_1363(self):
sim = Simulator(Module())
with self.assertRaisesRegex(TypeError,
r"^Cannot add a testbench <.+?> because it is not an async function or "
r"generator function$"):
async def testbench():
yield Delay()
sim.add_testbench(testbench())

def test_issue_1368(self):
sim = Simulator(Module())
async def testbench(ctx):
Expand Down
Loading