Skip to content

Commit 904d16e

Browse files
committed
gh-111366: Correctly show custom syntax error messages in the codeop module functions
1 parent 8b46f58 commit 904d16e

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

Lib/codeop.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ def _maybe_compile(compiler, source, filename, symbol):
7070
return None
7171
# fallthrough
7272

73-
return compiler(source, filename, symbol)
73+
return compiler(source, filename, symbol, incomplete_input=False)
7474

75-
def _compile(source, filename, symbol):
76-
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
75+
def _compile(source, filename, symbol, incomplete_input=True):
76+
flags = 0
77+
if incomplete_input:
78+
flags |= PyCF_ALLOW_INCOMPLETE_INPUT
79+
flags |= PyCF_DONT_IMPLY_DEDENT
80+
return compile(source, filename, symbol, flags)
7781

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

107-
def __call__(self, source, filename, symbol):
108-
codeob = compile(source, filename, symbol, self.flags, True)
111+
def __call__(self, source, filename, symbol, **kwargs):
112+
flags = self.flags
113+
if kwargs.get('incomplete_input', True) is False:
114+
flags &= ~PyCF_DONT_IMPLY_DEDENT
115+
flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT
116+
codeob = compile(source, filename, symbol, flags, True)
109117
for feature in _features:
110118
if codeob.co_flags & feature.compiler_flag:
111119
self.flags |= feature.compiler_flag

Lib/test/test_codeop.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import unittest
66
import warnings
77
from test.support import warnings_helper
8+
from textwrap import dedent
89

910
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
1011

@@ -307,6 +308,19 @@ def test_invalid_warning(self):
307308
self.assertEqual(w[0].category, SyntaxWarning)
308309
self.assertRegex(str(w[0].message), 'invalid escape sequence')
309310
self.assertEqual(w[0].filename, '<input>')
311+
312+
def assertSyntaxErrorMatches(self, code, message):
313+
with self.subTest(code):
314+
with self.assertRaisesRegex(SyntaxError, message):
315+
compile_command(code, symbol='exec')
316+
317+
def test_syntax_errors(self):
318+
self.assertSyntaxErrorMatches(
319+
dedent("""\
320+
def foo(x,x):
321+
pass
322+
"""), "duplicate argument 'x' in function definition")
323+
310324

311325

312326
if __name__ == "__main__":
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an issue in the :mod:`codeop` that was causing :exc:`SyntaxError`
2+
exceptions raised in the presence of invalid syntax to not contain precise
3+
error messages. Patch by Pablo Galindo

0 commit comments

Comments
 (0)