Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-121804: always show error location for SyntaxError's in new repl #121886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-121804: always show error location for SyntaxError's in new repl
>>> def good(x, y): ...
... def bad(x, x): ...
  File "<python-input-13>", line 2
    def bad(x, x): ...
               ^
SyntaxError: duplicate argument 'x' in function definition
  • Loading branch information
skirpichev committed Jul 17, 2024
commit f5f0d537e3215c278fcf732aefb3c45ebd219698
10 changes: 5 additions & 5 deletions 10 Lib/_pyrepl/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def __init__(
super().__init__(locals=locals, filename=filename, local_exit=local_exit) # type: ignore[call-arg]
self.can_colorize = _colorize.can_colorize()

def showsyntaxerror(self, filename=None):
super().showsyntaxerror(colorize=self.can_colorize)
def showsyntaxerror(self, filename=None, **kwargs):
super().showsyntaxerror(colorize=self.can_colorize, **kwargs)

def showtraceback(self):
super().showtraceback(colorize=self.can_colorize)
Expand All @@ -171,7 +171,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
try:
tree = ast.parse(source)
except (SyntaxError, OverflowError, ValueError):
self.showsyntaxerror(filename)
self.showsyntaxerror(filename, source=source)
return False
if tree.body:
*_, last_stmt = tree.body
Expand All @@ -188,10 +188,10 @@ def runsource(self, source, filename="<input>", symbol="single"):
f"Try the asyncio REPL ({python} -m asyncio) to use"
f" top-level 'await' and run background asyncio tasks."
)
self.showsyntaxerror(filename)
self.showsyntaxerror(filename, source=source)
return False
except (OverflowError, ValueError):
self.showsyntaxerror(filename)
self.showsyntaxerror(filename, source=source)
return False

if code is None:
Expand Down
6 changes: 5 additions & 1 deletion 6 Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
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:
Expand Down Expand Up @@ -123,6 +123,10 @@ def showsyntaxerror(self, filename=None, **kwargs):
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_exc = sys.last_value = value
source = kwargs.pop('source', "")
if source and not value.text and type is SyntaxError:
# Set the line of text that the exception refers to
value.text = source.splitlines()[value.lineno - 1]
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception_only(type, value, colorize=colorize)
self.write(''.join(lines))
Expand Down
2 changes: 1 addition & 1 deletion 2 Lib/idlelib/pyshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ def prepend_syspath(self, filename):
del _filename, _sys, _dirname, _dir
\n""".format(filename))

def showsyntaxerror(self, filename=None):
def showsyntaxerror(self, filename=None, **kwargs):
"""Override Interactive Interpreter method: Use Colorizing

Color the offending position instead of printing it and pointing at it
Expand Down
14 changes: 14 additions & 0 deletions 14 Lib/test/test_pyrepl/test_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ def test_runsource_returns_false_for_failed_compilation(self):
self.assertFalse(result)
self.assertIn('SyntaxError', f.getvalue())

@force_not_colorized
def test_runsource_show_syntax_error_location(self):
console = InteractiveColoredConsole()
source = "def f(x, x): ..."
f = io.StringIO()
with contextlib.redirect_stderr(f):
result = console.runsource(source)
self.assertFalse(result)
r = """
def f(x, x): ...
^
SyntaxError: duplicate argument 'x' in function definition"""
self.assertIn(r, f.getvalue())

def test_runsource_shows_syntax_error_for_failed_compilation(self):
console = InteractiveColoredConsole()
source = "print('Hello, world!'"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add new kwarg ``source`` for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to make this argument public so let's not mention it in the change log . Instead, mention what we are fixing in the repl

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should make it underscore-prefixed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our experience is that if it appears in the signature, people will use it and then changing it will be more challenging. Also some times the underscore prefixed argument is used to avoid collision with keywords or other reserved identifiers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our experience is that if it appears in the signature, people will use it and then changing it will be more challenging.

Yeah, I did a proposal to hide this per default: https://discuss.python.org/t/25543.

Also some times the underscore prefixed argument is used to avoid collision with keywords

I was thinking that it's pep8 violation. "it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption." (c)

:meth:`InteractiveInterpreter.showsyntaxerror()` to support enhanced error
messages, including error locations. Patch by Sergey B Kirpichev.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.