From c3ca84349373e40dc7d3b9e344f562b5f1fcef7e Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Sun, 19 Oct 2025 14:27:32 +0100 Subject: [PATCH] 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 --- ports/rp2/modules/rp2.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/ports/rp2/modules/rp2.py b/ports/rp2/modules/rp2.py index 442a802b3e..831f3416b2 100644 --- a/ports/rp2/modules/rp2.py +++ b/ports/rp2/modules/rp2.py @@ -251,20 +251,21 @@ def asm_pio(**kw): old_gl = gl.copy() gl.clear() - gl.update(_pio_funcs) - for name in _pio_directives: - gl[name] = getattr(emit, name) - for name in _pio_instructions: - gl[name] = getattr(emit, name) + try: + gl.update(_pio_funcs) + for name in _pio_directives: + gl[name] = getattr(emit, name) + for name in _pio_instructions: + gl[name] = getattr(emit, name) - emit.start_pass(0) - f() + emit.start_pass(0) + f() - emit.start_pass(1) - f() - - gl.clear() - gl.update(old_gl) + emit.start_pass(1) + f() + finally: + gl.clear() + gl.update(old_gl) return emit.prog