[3.11] gh-111366: Correctly show custom syntax error messages in the codeop module functions (GH-111384). (#111516)

(cherry picked from commit cd6e0a04a1)
This commit is contained in:
Pablo Galindo Salgado 2023-10-31 14:41:20 +00:00 committed by GitHub
parent bc9f47097e
commit 19a266ca89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View file

@ -7,6 +7,7 @@ import unittest
import warnings
from test import support
from test.support import warnings_helper
from textwrap import dedent
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
import io
@ -341,6 +342,19 @@ class CodeopTests(unittest.TestCase):
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()