Implement exit() to exit for ./micropython shell
I know that currently CTRL-D exits the shell.
For consistency with other python shells i.e CPython, let us implement exit() as an option to also exit the shell.
If this sounds good, i will track this for myself so that I can implement this as part of google season of docs.
runtime/exitcodes: Set exit code according to the SystemExit exception.
<!-- Thanks for submitting a Pull Request! We appreciate you spending the
time to improve MicroPython. Please provide enough information so that
others can review your Pull Request.
Before submitting, please read:
https://github.com/micropython/micropython/blob/master/CODEOFCONDUCT.md
https://github.com/micropython/micropython/wiki/ContributorGuidelines
Please check any CI failures that appear after your Pull Request is opened.
-->
Summary
Add abort setup code (nlr_set_abort) to standard runtime. This makes the standard runtime respond to abort signal without any further modifications.
- When aborted, the program exits with 137 exit code (configurable, same as posix sig abort), to differentiate from a normal shutdown.
- When exited by exception/crash, the program will exit with exit code 1 (configurable)
- When exited by exception KeyboardInterrupt, the program will exit with exit code 130 (configurable, same as posix sig int)
- When exited with a exit code (from python environment), this code is propagated. When a different object is passed, exit code is set to 1 and the value printed, to be consistent with python docs: https://python.readthedocs.io/en/latest/library/exceptions.html#SystemExit
Testing
Tested on ESP32, should work in other places. The changes are guarded by #if abort is enabled in the runtime -> only people who want the abort functionality will get this change.
Trade-offs and Alternatives
This code may return non-zero exit code in scenarios where previous version did return zero exit code. The exit codes are configurable and the defaults are zero, so not to introduce any breaking changes.