Skip to content

Commit

Permalink
Backport PR #526: PR: Filter frames that come from Spyder-kernels in …
Browse files Browse the repository at this point in the history
…tracebacks and fix tracebacks in Python 3.8
  • Loading branch information
ccordoba12 authored and meeseeksmachine committed Jan 17, 2025
1 parent 0eb856b commit 3dc7b3d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
7 changes: 6 additions & 1 deletion spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
49 changes: 40 additions & 9 deletions spyder_kernels/console/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import bdb
import logging
import os
import re
import signal
import sys
import traceback
Expand Down Expand Up @@ -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)

Expand All @@ -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."""
Expand Down

0 comments on commit 3dc7b3d

Please sign in to comment.