summaryrefslogtreecommitdiffstats
path: root/Lib/code.py
diff options
context:
space:
mode:
authorCF Bolz-Tereick <cfbolz@gmx.de>2024-08-22 23:58:05 (GMT)
committerGitHub <noreply@github.com>2024-08-22 23:58:05 (GMT)
commit30eee22d3f5b637d2a9536c2d7bf448f6ac14118 (patch)
tree8fad67da57c5fd989f878d4ee0a42a6e52c969ec /Lib/code.py
parent5271f8feadc198407449514348c34e3fe77de448 (diff)
downloadcpython-30eee22d3f5b637d2a9536c2d7bf448f6ac14118.zip
cpython-30eee22d3f5b637d2a9536c2d7bf448f6ac14118.tar.gz
cpython-30eee22d3f5b637d2a9536c2d7bf448f6ac14118.tar.bz2
[3.13] gh-122478: Remove internal frames from tracebacks in REPL (GH-122528) (#123227)
Frames of methods in code and codeop modules was show with non-default sys.excepthook. Save correct tracebacks in sys.last_traceback and update __traceback__ attribute of sys.last_value and sys.last_exc. (cherry picked from commit e73e7a7abdc3fed252affcb1629df1b3c8fff2ef) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/code.py')
-rw-r--r--Lib/code.py86
1 files changed, 42 insertions, 44 deletions
diff --git a/Lib/code.py b/Lib/code.py
index ccf2d9d..5f7a2d3 100644
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -13,6 +13,7 @@ from codeop import CommandCompiler, compile_command
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
"compile_command"]
+
class InteractiveInterpreter:
"""Base class for InteractiveConsole.
@@ -107,26 +108,14 @@ class InteractiveInterpreter:
"""
colorize = kwargs.pop('colorize', False)
- type, value, tb = sys.exc_info()
- sys.last_exc = value
- sys.last_type = type
- sys.last_value = value
- sys.last_traceback = tb
- if filename and type is SyntaxError:
- value.filename = filename
- # Set the line of text that the exception refers to
- source = kwargs.pop('source', '')
- lines = source.splitlines()
- if (source and type is SyntaxError
- and not value.text and len(lines) >= value.lineno):
- value.text = lines[value.lineno - 1]
- if sys.excepthook is sys.__excepthook__:
- lines = traceback.format_exception_only(type, value, colorize=colorize)
- self.write(''.join(lines))
- else:
- # If someone has set sys.excepthook, we let that take precedence
- # over self.write
- self._call_excepthook(type, value, tb)
+ try:
+ typ, value, tb = sys.exc_info()
+ if filename and typ is SyntaxError:
+ value.filename = filename
+ source = kwargs.pop('source', "")
+ self._showtraceback(typ, value, None, colorize, source)
+ finally:
+ typ = value = tb = None
def showtraceback(self, **kwargs):
"""Display the exception that just occurred.
@@ -137,32 +126,41 @@ class InteractiveInterpreter:
"""
colorize = kwargs.pop('colorize', False)
- sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
- sys.last_traceback = last_tb
- sys.last_exc = ei[1]
try:
- 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
- self._call_excepthook(ei[0], ei[1], last_tb)
+ typ, value, tb = sys.exc_info()
+ self._showtraceback(typ, value, tb.tb_next, colorize, '')
finally:
- last_tb = ei = None
+ typ = value = tb = 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 _showtraceback(self, typ, value, tb, colorize, source):
+ sys.last_type = typ
+ sys.last_traceback = tb
+ value = value.with_traceback(tb)
+ # Set the line of text that the exception refers to
+ lines = source.splitlines()
+ if (source and typ is SyntaxError
+ and not value.text and len(lines) >= value.lineno):
+ value.text = lines[value.lineno - 1]
+ sys.last_exc = sys.last_value = value = value.with_traceback(tb)
+ if sys.excepthook is sys.__excepthook__:
+ lines = traceback.format_exception(typ, value, tb,
+ colorize=colorize)
+ self.write(''.join(lines))
+ else:
+ # If someone has set sys.excepthook, we let that take precedence
+ # over self.write
+ try:
+ sys.excepthook(typ, value, tb)
+ except SystemExit:
+ raise
+ except BaseException as e:
+ e.__context__ = None
+ e = e.with_traceback(e.__traceback__.tb_next)
+ print('Error in sys.excepthook:', file=sys.stderr)
+ sys.__excepthook__(type(e), e, e.__traceback__)
+ print(file=sys.stderr)
+ print('Original exception was:', file=sys.stderr)
+ sys.__excepthook__(typ, value, tb)
def write(self, data):
"""Write a string.
@@ -376,7 +374,7 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-q', action='store_true',
- help="don't print version and copyright messages")
+ help="don't print version and copyright messages")
args = parser.parse_args()
if args.q or sys.flags.quiet:
banner = ''