Skip to content

Commit c3ca843

Browse files
arachsysdpgeorge
authored andcommitted
rp2/modules/rp2.py: Don't corrupt globals on asm_pio() exception.
The rp2.asm_pio() decorator saves and temporarily replaces the globals dictionary to allow the wrapped functions to reference PIO instructions and register names in a natural way, restoring them before returning the assembled program. However, if an exception occurs while assembling the program, the globals are not changed back, leaving them corrupted. Wrap with try/finally to ensure they are always restored correctly. Signed-off-by: Chris Webb <chris@arachsys.com>
1 parent 06bfefe commit c3ca843

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

ports/rp2/modules/rp2.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,21 @@ def dec(f):
251251
old_gl = gl.copy()
252252
gl.clear()
253253

254-
gl.update(_pio_funcs)
255-
for name in _pio_directives:
256-
gl[name] = getattr(emit, name)
257-
for name in _pio_instructions:
258-
gl[name] = getattr(emit, name)
259-
260-
emit.start_pass(0)
261-
f()
262-
263-
emit.start_pass(1)
264-
f()
265-
266-
gl.clear()
267-
gl.update(old_gl)
254+
try:
255+
gl.update(_pio_funcs)
256+
for name in _pio_directives:
257+
gl[name] = getattr(emit, name)
258+
for name in _pio_instructions:
259+
gl[name] = getattr(emit, name)
260+
261+
emit.start_pass(0)
262+
f()
263+
264+
emit.start_pass(1)
265+
f()
266+
finally:
267+
gl.clear()
268+
gl.update(old_gl)
268269

269270
return emit.prog
270271

0 commit comments

Comments
 (0)