Skip to content

Commit f48b865

Browse files
wanda-phiwhitequark
authored andcommitted
sim: fix simulation loop when process catches an injected exception.
1 parent 15b6068 commit f48b865

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

amaranth/sim/_pycoro.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ def run(self):
5959
self.clear_triggers()
6060

6161
response = None
62+
exception = None
6263
while True:
6364
try:
64-
command = self.coroutine.send(response)
65+
if exception is None:
66+
command = self.coroutine.send(response)
67+
else:
68+
command = self.coroutine.throw(exception)
69+
except StopIteration:
70+
self.passive = True
71+
self.coroutine = None
72+
return
73+
74+
try:
6575
if command is None:
6676
command = self.default_cmd
6777
response = None
78+
exception = None
6879

6980
if isinstance(command, ValueCastable):
7081
command = Value.cast(command)
@@ -118,10 +129,6 @@ def run(self):
118129
raise TypeError("Received unsupported command {!r} from process {!r}"
119130
.format(command, self.src_loc()))
120131

121-
except StopIteration:
122-
self.passive = True
123-
self.coroutine = None
124-
return
125-
126132
except Exception as exn:
127-
self.coroutine.throw(exn)
133+
response = None
134+
exception = exn

tests/test_sim.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,6 @@ def process():
685685
with self.assertRaisesRegex(TypeError,
686686
r"Received unsupported command 1 from process .+?"):
687687
yield 1
688-
yield Settle()
689688
survived = True
690689
sim.add_process(process)
691690
self.assertTrue(survived)

0 commit comments

Comments
 (0)