1414import bdb
1515import logging
1616import os
17+ import re
1718import signal
1819import sys
1920import 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