From b1a0b36582a0daab85325bbc8ed98d42cce13c03 Mon Sep 17 00:00:00 2001 From: Wanda Date: Tue, 6 Feb 2024 19:43:47 +0100 Subject: [PATCH] sim: fix simulation loop when process catches an injected exception. --- amaranth/sim/_pycoro.py | 21 ++++++++++++++------- tests/test_sim.py | 1 - 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/amaranth/sim/_pycoro.py b/amaranth/sim/_pycoro.py index e9500c47f..6a0487e6b 100644 --- a/amaranth/sim/_pycoro.py +++ b/amaranth/sim/_pycoro.py @@ -59,12 +59,23 @@ def run(self): self.clear_triggers() response = None + exception = None while True: try: - command = self.coroutine.send(response) + if exception is None: + command = self.coroutine.send(response) + else: + command = self.coroutine.throw(exception) + except StopIteration: + self.passive = True + self.coroutine = None + return + + try: if command is None: command = self.default_cmd response = None + exception = None if isinstance(command, ValueCastable): command = Value.cast(command) @@ -118,10 +129,6 @@ def run(self): raise TypeError("Received unsupported command {!r} from process {!r}" .format(command, self.src_loc())) - except StopIteration: - self.passive = True - self.coroutine = None - return - except Exception as exn: - self.coroutine.throw(exn) + response = None + exception = exn diff --git a/tests/test_sim.py b/tests/test_sim.py index 5614259dd..5eaf6df35 100644 --- a/tests/test_sim.py +++ b/tests/test_sim.py @@ -685,7 +685,6 @@ def process(): with self.assertRaisesRegex(TypeError, r"Received unsupported command 1 from process .+?"): yield 1 - yield Settle() survived = True sim.add_process(process) self.assertTrue(survived)