aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/code.py
diff options
context:
space:
mode:
authorCF Bolz-Tereick <cfbolz@gmx.de>2024-07-31 12:33:29 +0200
committerGitHub <noreply@github.com>2024-07-31 10:33:29 +0000
commitbd3d31f380cd451a4ab6da5fbfde463fed95b5b5 (patch)
treeeef67c37d5ff5ef20ac7b8a972bd9fe73d910b00 /Lib/code.py
parente60ee11cb51b87deeb22ad125717bd0d0dc10fa8 (diff)
downloadcpython-bd3d31f380cd451a4ab6da5fbfde463fed95b5b5.tar.gz
cpython-bd3d31f380cd451a4ab6da5fbfde463fed95b5b5.zip
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.
Diffstat (limited to 'Lib/code.py')
-rw-r--r--Lib/code.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/Lib/code.py b/Lib/code.py
index a55fced0704..cd3889067d0 100644
--- a/Lib/code.py
+++ b/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.