From 489408e8d37a35b19d08354f880d00f85422d591 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 16 Jan 2024 17:09:37 +1100 Subject: [PATCH] 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. --- scripts/modules/console_python.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/modules/console_python.py b/scripts/modules/console_python.py index c3042878d04..474684129b6 100644 --- a/scripts/modules/console_python.py +++ b/scripts/modules/console_python.py @@ -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())