Skip to content

py/nlrthumb.c: restore r1,r2,r3 clobbers to nlr_push(); fixes many Thonny issues#10805

Merged
tannewt merged 1 commit intoadafruit:mainfrom
dhalbert:nlrthumb-clobbers
Feb 6, 2026
Merged

py/nlrthumb.c: restore r1,r2,r3 clobbers to nlr_push(); fixes many Thonny issues#10805
tannewt merged 1 commit intoadafruit:mainfrom
dhalbert:nlrthumb-clobbers

Conversation

@dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Feb 6, 2026

Many issues were reported when using Thonny. If the code that Thonny sent to the REPL, while in raw paste mode, raised an exception, then there could be a hard crash causing safe mode, and causing the Thonny connection to fail. This problem was exercised inside parse_compile_execute() in pyexec.c when in raw paste mode, when exec_flags included EXEC_FLAG_SOURCE_IS_READER. The reader struct would get smashed, causing reader->close() to be 0x0.

A long time ago, in #506, "r1", "r2", "r3" were added to the "clobbers" list in nlr_push(), in py/nlrthumb.c. That change was lost during a MicroPython merge. #7368 tried to fix that in a different way, using returns_twice, as suggested in micropython#3889. But that change was not merged into MicroPython: instead, the nlr routines were compiled with -fno-lto instead.

However, #7368 did not really fix the register clobbering problem. It was introduced between 8.0.0-beta.5 and 8.0.0-beta.6. 8.0.0-beta.6 is when the Thonny problems started.

So this PR restores the original #506 fix, and discards the returns_twice. I tested with a Metro M0 (SAMD21), a Metro M4 (SAMD51), a PCA10059 (nRF52840), and a Micro:bit V2, all with Thonny.

It's possible this bug caused other problems, but I don't see as clear a lineage as with all these similar Thonny problems.

This PR also includes adafruit/samd-peripherals#48, which was needed when testing some debug builds.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Thank you!

@tannewt tannewt merged commit ac9e846 into adafruit:main Feb 6, 2026
646 checks passed
@dhalbert dhalbert deleted the nlrthumb-clobbers branch February 6, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment