mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +00:00
gh-87320: In the code module, handle exceptions raised in sys.excepthook (GH-122456)
Before, the exception caused by calling non-default sys.excepthook in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
This commit is contained in:
parent
e60ee11cb5
commit
bd3d31f380
4 changed files with 76 additions and 3 deletions
19
Lib/code.py
19
Lib/code.py
|
@ -129,7 +129,7 @@ class InteractiveInterpreter:
|
|||
else:
|
||||
# If someone has set sys.excepthook, we let that take precedence
|
||||
# over self.write
|
||||
sys.excepthook(type, value, tb)
|
||||
self._call_excepthook(type, value, tb)
|
||||
|
||||
def showtraceback(self, **kwargs):
|
||||
"""Display the exception that just occurred.
|
||||
|
@ -144,16 +144,29 @@ class InteractiveInterpreter:
|
|||
sys.last_traceback = last_tb
|
||||
sys.last_exc = ei[1]
|
||||
try:
|
||||
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next, colorize=colorize)
|
||||
if sys.excepthook is sys.__excepthook__:
|
||||
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next, colorize=colorize)
|
||||
self.write(''.join(lines))
|
||||
else:
|
||||
# If someone has set sys.excepthook, we let that take precedence
|
||||
# over self.write
|
||||
sys.excepthook(ei[0], ei[1], last_tb)
|
||||
self._call_excepthook(ei[0], ei[1], last_tb)
|
||||
finally:
|
||||
last_tb = ei = None
|
||||
|
||||
def _call_excepthook(self, typ, value, tb):
|
||||
try:
|
||||
sys.excepthook(typ, value, tb)
|
||||
except SystemExit:
|
||||
raise
|
||||
except BaseException as e:
|
||||
e.__context__ = None
|
||||
print('Error in sys.excepthook:', file=sys.stderr)
|
||||
sys.__excepthook__(type(e), e, e.__traceback__.tb_next)
|
||||
print(file=sys.stderr)
|
||||
print('Original exception was:', file=sys.stderr)
|
||||
sys.__excepthook__(typ, value, tb)
|
||||
|
||||
def write(self, data):
|
||||
"""Write a string.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue