mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-128231: Use runcode()
return value for failing early (GH-129488)
This commit is contained in:
parent
9f25c1f012
commit
7ed3dc6392
5 changed files with 40 additions and 3 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue