Page 1 of 1
Catch Keyboard Interrupt, Finally section incomplete?
Posted: Thu Aug 18, 2022 5:42 pm
by Jibun no kage
Catch Keyboard Interrupt, Finally section incomplete? Is this something unique to MP? After I except a keyboard interrupt, the finally section following starts but is not completely executed. This is on Pico port, has not tested on ESP yet.
Test...
Code: Select all
import utime
try:
print('test')
while True:
print('wait')
utime.sleep(1)
except KeyboardInterrupt as theInterrupt:
print('interrupt')
finally:
i=0
while (i<10):
print(i)
utime.sleep(1)
print('done')
Output...
Code: Select all
>>> %Run -c $EDITOR_CONTENT
test
wait
wait
wait
interrupt
0
Traceback (most recent call last):
File "<stdin>", line 13, in <module>
KeyboardInterrupt:
>>>
Re: Catch Keyboard Interrupt, Finally section incomplete?
Posted: Thu Aug 18, 2022 11:57 pm
by jimmo
Jibun no kage wrote: ↑Thu Aug 18, 2022 5:42 pm
This is on Pico port, has not tested on ESP yet.
This works for me on a Pico, running both 1.19.1 and latest development version.
I see "wait" "wait" "wait" etc, then hit Ctrl-C, and then it prints "interrupt", then "0", "0", etc until I press Ctrl-C again.
Are you sure that whatever terminal you're using isn't sending more than one Ctrl-C. Try using mpremote or screen.
Re: Catch Keyboard Interrupt, Finally section incomplete?
Posted: Fri Aug 19, 2022 6:12 pm
by Jibun no kage
Other than I forgot the i=i+1, 0,0,0 as output is what should happen for code as written. So why do I only get a single 0. Interesting. How would I know I am or am not getting more than a single Control-C? That would definitely explain the results I am getting. Is there a way I can mask the Control-C? REPL overrides the typical kbd_intr(-1) right? I am using Thonny REPL at the moment. Will try something else.
Re: Catch Keyboard Interrupt, Finally section incomplete?
Posted: Sat Aug 20, 2022 1:42 pm
by jimmo
Jibun no kage wrote: ↑Fri Aug 19, 2022 6:12 pm
I am using Thonny REPL at the moment. Will try something else.
This wouldn't be suprising - In order to facilitate all the extra features, Thonny's REPL isn't at all like a regular console. (For a start, it's line buffered).
Re: Catch Keyboard Interrupt, Finally section incomplete?
Posted: Sat Aug 20, 2022 7:12 pm
by Jibun no kage
I figured out part of the issue, or a similar scenario. If you Control-C (in Thonny) while asyncio.run(main()) has control, the finally section in main() never executes, even if you have a valid except stanza in main(). If you wrap a try except finally around asyncio.run(main()), that interrupt is processed. This makes complete sense, given the context of the interrupt is typically during an asyncio.sleep() (in the main(), while loop) which is within the asyncio run frame. It is just a bit counter intuitive.
None of this critical, since I only use Control-C during code development, but clearly there is a scope/control issue that comes into play when the Control-C is invoked.