PyAPI: handle sys.exit() from the Python console

This would raise a SystemExit, showing it's stack trace into Python's
code module, then a syntax error on any input immediately afterwards.

Now exception is printed & the input for the Python console is reset.

Address some issues raised by #109435.
This commit is contained in:
Campbell Barton 2024-01-16 17:09:37 +11:00
parent a294a9083e
commit 489408e8d3

View File

@ -164,8 +164,14 @@ def execute(context, is_interactive):
line_exec = line if line.strip() else "\n"
is_multiline = console.push(line_exec)
except SystemExit as ex:
# Occurs when `exit(..)` is called, this raises an exception instead of exiting.
# The trace-back isn't helpful in this case, just print the exception.
stderr.write("%r\n" % ex)
# Without this, entering new commands may include the previous command, see: #109435.
console.resetbuffer()
except:
# unlikely, but this can happen with unicode errors for example.
# Unlikely, but this can happen with unicode errors accessing `line_object.body`.
import traceback
stderr.write(traceback.format_exc())