mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.12] gh-111366: Correctly show custom syntax error messages in the codeop module functions (GH-111384). (#111517)
This commit is contained in:
parent
7ac2c53333
commit
ca36426b1c
3 changed files with 31 additions and 6 deletions
|
@ -70,8 +70,7 @@ def _maybe_compile(compiler, source, filename, symbol):
|
||||||
return None
|
return None
|
||||||
# fallthrough
|
# fallthrough
|
||||||
|
|
||||||
return compiler(source, filename, symbol)
|
return compiler(source, filename, symbol, incomplete_input=False)
|
||||||
|
|
||||||
|
|
||||||
def _is_syntax_error(err1, err2):
|
def _is_syntax_error(err1, err2):
|
||||||
rep1 = repr(err1)
|
rep1 = repr(err1)
|
||||||
|
@ -82,8 +81,13 @@ def _is_syntax_error(err1, err2):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _compile(source, filename, symbol):
|
def _compile(source, filename, symbol, incomplete_input=True):
|
||||||
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
|
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"):
|
def compile_command(source, filename="<input>", symbol="single"):
|
||||||
r"""Compile a command and determine whether it is incomplete.
|
r"""Compile a command and determine whether it is incomplete.
|
||||||
|
@ -114,8 +118,12 @@ class Compile:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT
|
self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT
|
||||||
|
|
||||||
def __call__(self, source, filename, symbol):
|
def __call__(self, source, filename, symbol, **kwargs):
|
||||||
codeob = compile(source, filename, symbol, self.flags, True)
|
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:
|
for feature in _features:
|
||||||
if codeob.co_flags & feature.compiler_flag:
|
if codeob.co_flags & feature.compiler_flag:
|
||||||
self.flags |= feature.compiler_flag
|
self.flags |= feature.compiler_flag
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
import warnings
|
||||||
from test.support import warnings_helper
|
from test.support import warnings_helper
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
|
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
|
||||||
|
|
||||||
|
@ -308,6 +309,19 @@ class CodeopTests(unittest.TestCase):
|
||||||
self.assertRegex(str(w[0].message), 'invalid escape sequence')
|
self.assertRegex(str(w[0].message), 'invalid escape sequence')
|
||||||
self.assertEqual(w[0].filename, '<input>')
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue