summaryrefslogtreecommitdiffstats
path: root/Lib/code.py
diff options
context:
space:
mode:
authorSergey B Kirpichev <skirpichev@gmail.com>2024-08-19 19:01:58 (GMT)
committerGitHub <noreply@github.com>2024-08-19 19:01:58 (GMT)
commitc8f4069ab1602a1f67239fef0e11cc3e72c0045d (patch)
tree958e6704cd0102ea848a511fec9f7d8f50cc9dd7 /Lib/code.py
parent21399a096302ea577efd9a12c2f08b4458d095bd (diff)
downloadcpython-c8f4069ab1602a1f67239fef0e11cc3e72c0045d.zip
cpython-c8f4069ab1602a1f67239fef0e11cc3e72c0045d.tar.gz
cpython-c8f4069ab1602a1f67239fef0e11cc3e72c0045d.tar.bz2
[3.13] gh-121804: Always show error location for SyntaxError's in new repl (GH-121886) (#123148)
(cherry picked from commit 354d55eb1fa40f272419aa6459ee5d2c4804c8ea)
Diffstat (limited to 'Lib/code.py')
-rw-r--r--Lib/code.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/Lib/code.py b/Lib/code.py
index 7d68517..aec7d61 100644
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -64,7 +64,7 @@ class InteractiveInterpreter:
code = self.compile(source, filename, symbol)
except (OverflowError, SyntaxError, ValueError):
# Case 1
- self.showsyntaxerror(filename)
+ self.showsyntaxerror(filename, source=source)
return False
if code is None:
@@ -123,6 +123,12 @@ class InteractiveInterpreter:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_exc = sys.last_value = value
+ # Set the line of text that the exception refers to
+ source = kwargs.pop('source', '')
+ lines = source.splitlines()
+ if (source and type is SyntaxError
+ and not value.text and len(lines) >= value.lineno):
+ value.text = lines[value.lineno - 1]
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception_only(type, value, colorize=colorize)
self.write(''.join(lines))