Skip to content

Commit d4f4b7d

Browse files
committed
More special handling of SyntaxErrors
SyntaxErrors: - Do NOT include the text ", in" followed by a module - DO include the offending line of code Other Exceptions - DO include the text ", in" followed by a module - Do NOT include the offending line of code if from stdIn (ie a workbox)
1 parent 45f51fe commit d4f4b7d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

preditor/gui/console.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,21 @@ class ConsolePrEdit(QTextEdit):
4848
def __init__(self, parent):
4949
super(ConsolePrEdit, self).__init__(parent)
5050

51-
# For workboxes, use this regex pattern, so we can extract workboxName
52-
# and lineNum
51+
# For Traceback workbox lines, use this regex pattern, so we can extract
52+
# workboxName and lineNum. Note that Syntax errors present slightly
53+
# differently than other Exceptions.
54+
# SyntaxErrors:
55+
# - Do NOT include the text ", in" followed by a module
56+
# - DO include the offending line of code
57+
# Other Exceptions
58+
# - DO include the text ", in" followed by a module
59+
# - Do NOT include the offending line of code if from stdIn (ie
60+
# a workbox)
61+
# So we will use the presence of the text ", in" to tell use whether to
62+
# fake the offending code line or not.
5363
pattern = r'File "<Workbox(?:Selection)?>:(?P<workboxName>.*)", '
54-
pattern += r'line (?P<lineNum>\d{1,6}), in'
64+
pattern += r'line (?P<lineNum>\d{1,6})'
65+
pattern += r'(?P<inStr>, in)?'
5566
self.workbox_pattern = re.compile(pattern)
5667

5768
# Define a pattern to capture info from tracebacks. The newline/$ section
@@ -974,14 +985,17 @@ def write(self, msg, error=False):
974985

975986
# Starting in Python 3, tracebacks don't include the code executed
976987
# for stdin, so workbox code won't appear. This attempts to include
977-
# it.
988+
# it. There is an exception for SyntaxErrors, which DO include the
989+
# offending line of code, so in those cases (indicated by lack of
990+
# inStr from the regex search) we skip faking the code line.
978991
if isWorkbox:
979992
match = self.workbox_pattern.search(msg)
980993
workboxName = match.groupdict().get("workboxName")
981994
lineNum = int(match.groupdict().get("lineNum")) - 1
995+
inStr = match.groupdict().get("inStr", "")
982996

983997
workboxLine = self.getWorkboxLine(workboxName, lineNum)
984-
if workboxLine:
998+
if workboxLine and inStr:
985999
indent = self.getIndentForCodeTracebackLine(msg)
9861000
msg = "{}{}{}".format(msg, indent, workboxLine)
9871001

0 commit comments

Comments
 (0)