Skip to content
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

gh-111366: Correctly show custom syntax error messages in the codeop module functions #111384

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions Lib/codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ def _maybe_compile(compiler, source, filename, symbol):
return None
# fallthrough

return compiler(source, filename, symbol)
return compiler(source, filename, symbol, incomplete_input=False)

def _compile(source, filename, symbol):
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
def _compile(source, filename, symbol, incomplete_input=True):
flags = 0
if incomplete_input:
flags |= PyCF_ALLOW_INCOMPLETE_INPUT
flags |= PyCF_DONT_IMPLY_DEDENT
return compile(source, filename, symbol, flags)

def compile_command(source, filename="<input>", symbol="single"):
r"""Compile a command and determine whether it is incomplete.
Expand Down Expand Up @@ -104,8 +108,12 @@ class Compile:
def __init__(self):
self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT

def __call__(self, source, filename, symbol):
codeob = compile(source, filename, symbol, self.flags, True)
def __call__(self, source, filename, symbol, **kwargs):
flags = self.flags
if kwargs.get('incomplete_input', True) is False:
flags &= ~PyCF_DONT_IMPLY_DEDENT
flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT
codeob = compile(source, filename, symbol, flags, True)
for feature in _features:
if codeob.co_flags & feature.compiler_flag:
self.flags |= feature.compiler_flag
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import warnings
from test.support import warnings_helper
from textwrap import dedent

from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT

Expand Down Expand Up @@ -308,6 +309,19 @@ def test_invalid_warning(self):
self.assertRegex(str(w[0].message), 'invalid escape sequence')
self.assertEqual(w[0].filename, '<input>')

def assertSyntaxErrorMatches(self, code, message):
with self.subTest(code):
with self.assertRaisesRegex(SyntaxError, message):
compile_command(code, symbol='exec')

def test_syntax_errors(self):
self.assertSyntaxErrorMatches(
dedent("""\
def foo(x,x):
pass
"""), "duplicate argument 'x' in function definition")



if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an issue in the :mod:`codeop` that was causing :exc:`SyntaxError`
exceptions raised in the presence of invalid syntax to not contain precise
error messages. Patch by Pablo Galindo