diff --git a/spyder_kernels/console/kernel.py b/spyder_kernels/console/kernel.py index 391c7392..477fcd95 100644 --- a/spyder_kernels/console/kernel.py +++ b/spyder_kernels/console/kernel.py @@ -641,7 +641,12 @@ def set_configuration(self, conf): elif key == "color scheme": self.set_color_scheme(value) elif key == "traceback_highlight_style": - self.set_traceback_syntax_highlighting(value) + # This doesn't work in Python 3.8 because the last IPython + # version compatible with it doesn't allow to customize the + # syntax highlighting scheme used for tracebacks. + # Fixes spyder-ide/spyder#23484 + if sys.version_info >= (3, 9): + self.set_traceback_syntax_highlighting(value) elif key == "jedi_completer": self.set_jedi_completer(value) elif key == "greedy_completer": diff --git a/spyder_kernels/console/shell.py b/spyder_kernels/console/shell.py index 0de22d43..219e221b 100644 --- a/spyder_kernels/console/shell.py +++ b/spyder_kernels/console/shell.py @@ -14,6 +14,7 @@ import bdb import logging import os +import re import signal import sys import traceback @@ -58,6 +59,15 @@ def __init__(self, *args, **kwargs): self.update_gui_frontend = False self._spyder_theme = 'dark' + # Substrings of the directory where Spyder-kernels is installed + self._package_locations = [ + # When the package is properly installed + os.path.join("site-packages", "spyder_kernels"), + # When it's installed from the external-deps subrepo. We need this + # for our tests + os.path.join("external-deps", "spyder-kernels", "spyder_kernels") + ] + # register post_execute self.events.register('post_execute', self.do_post_execute) @@ -74,16 +84,37 @@ def ask_exit(self): super().ask_exit() def _showtraceback(self, etype, evalue, stb): - """ - Don't show a traceback when exiting our debugger after entering - it through a `breakpoint()` call. - - This is because calling `!exit` after `breakpoint()` raises - BdbQuit, which throws a long and useless traceback. - """ + """Handle how tracebacks are displayed in the console.""" + spyder_stb = [] if etype is bdb.BdbQuit: - stb = [''] - super(SpyderShell, self)._showtraceback(etype, evalue, stb) + # Don't show a traceback when exiting our debugger after entering + # it through a `breakpoint()` call. This is because calling `!exit` + # after `breakpoint()` raises BdbQuit, which throws a long and + # useless traceback. + spyder_stb.append('') + else: + # Skip internal frames from the traceback's string representation + for line in stb: + if ( + # Verbose mode + re.match(r"File (.*)", line) + # Plain mode + or re.match(r"\x1b\[(.*) File (.*)", line) + ) and ( + # The file line should not contain a location where + # Spyder-kernels is installed + any( + [ + location in line + for location in self._package_locations + ] + ) + ): + continue + else: + spyder_stb.append(line) + + super()._showtraceback(etype, evalue, spyder_stb) def set_spyder_theme(self, theme): """Set the theme for the console."""