Skip to content

Commit d1a6ee4

Browse files
Backport PR #526 on branch 3.x (PR: Filter frames that come from Spyder-kernels in tracebacks and fix tracebacks in Python 3.8) (#529)
1 parent 0eb856b commit d1a6ee4

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

spyder_kernels/console/kernel.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,12 @@ def set_configuration(self, conf):
641641
elif key == "color scheme":
642642
self.set_color_scheme(value)
643643
elif key == "traceback_highlight_style":
644-
self.set_traceback_syntax_highlighting(value)
644+
# This doesn't work in Python 3.8 because the last IPython
645+
# version compatible with it doesn't allow to customize the
646+
# syntax highlighting scheme used for tracebacks.
647+
# Fixes spyder-ide/spyder#23484
648+
if sys.version_info >= (3, 9):
649+
self.set_traceback_syntax_highlighting(value)
645650
elif key == "jedi_completer":
646651
self.set_jedi_completer(value)
647652
elif key == "greedy_completer":

spyder_kernels/console/shell.py

+40-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import bdb
1515
import logging
1616
import os
17+
import re
1718
import signal
1819
import sys
1920
import traceback
@@ -58,6 +59,15 @@ def __init__(self, *args, **kwargs):
5859
self.update_gui_frontend = False
5960
self._spyder_theme = 'dark'
6061

62+
# Substrings of the directory where Spyder-kernels is installed
63+
self._package_locations = [
64+
# When the package is properly installed
65+
os.path.join("site-packages", "spyder_kernels"),
66+
# When it's installed from the external-deps subrepo. We need this
67+
# for our tests
68+
os.path.join("external-deps", "spyder-kernels", "spyder_kernels")
69+
]
70+
6171
# register post_execute
6272
self.events.register('post_execute', self.do_post_execute)
6373

@@ -74,16 +84,37 @@ def ask_exit(self):
7484
super().ask_exit()
7585

7686
def _showtraceback(self, etype, evalue, stb):
77-
"""
78-
Don't show a traceback when exiting our debugger after entering
79-
it through a `breakpoint()` call.
80-
81-
This is because calling `!exit` after `breakpoint()` raises
82-
BdbQuit, which throws a long and useless traceback.
83-
"""
87+
"""Handle how tracebacks are displayed in the console."""
88+
spyder_stb = []
8489
if etype is bdb.BdbQuit:
85-
stb = ['']
86-
super(SpyderShell, self)._showtraceback(etype, evalue, stb)
90+
# Don't show a traceback when exiting our debugger after entering
91+
# it through a `breakpoint()` call. This is because calling `!exit`
92+
# after `breakpoint()` raises BdbQuit, which throws a long and
93+
# useless traceback.
94+
spyder_stb.append('')
95+
else:
96+
# Skip internal frames from the traceback's string representation
97+
for line in stb:
98+
if (
99+
# Verbose mode
100+
re.match(r"File (.*)", line)
101+
# Plain mode
102+
or re.match(r"\x1b\[(.*) File (.*)", line)
103+
) and (
104+
# The file line should not contain a location where
105+
# Spyder-kernels is installed
106+
any(
107+
[
108+
location in line
109+
for location in self._package_locations
110+
]
111+
)
112+
):
113+
continue
114+
else:
115+
spyder_stb.append(line)
116+
117+
super()._showtraceback(etype, evalue, spyder_stb)
87118

88119
def set_spyder_theme(self, theme):
89120
"""Set the theme for the console."""

0 commit comments

Comments
 (0)