gh-128231: Use runcode() return value for failing early (GH-129488)

This commit is contained in:
Bartosz Sławecki 2025-02-24 15:50:13 +01:00 committed by GitHub
parent 9f25c1f012
commit 7ed3dc6392
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 40 additions and 3 deletions

View file

@ -152,6 +152,8 @@ class Console(ABC):
class InteractiveColoredConsole(code.InteractiveConsole):
STATEMENT_FAILED = object()
def __init__(
self,
locals: dict[str, object] | None = None,
@ -173,6 +175,16 @@ class InteractiveColoredConsole(code.InteractiveConsole):
limit=traceback.BUILTIN_EXCEPTION_LIMIT)
self.write(''.join(lines))
def runcode(self, code):
try:
exec(code, self.locals)
except SystemExit:
raise
except BaseException:
self.showtraceback()
return self.STATEMENT_FAILED
return None
def runsource(self, source, filename="<input>", symbol="single"):
try:
tree = self.compile.compiler(
@ -209,5 +221,7 @@ class InteractiveColoredConsole(code.InteractiveConsole):
if code is None:
return True
self.runcode(code)
result = self.runcode(code)
if result is self.STATEMENT_FAILED:
break
return False