From 31fa8a4046f4905f8ef4dfabfaf58a48e1cac54c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 24 Jul 2025 15:59:56 +0200 Subject: [PATCH] Update `codeop.py` from 3.13.5 --- Lib/codeop.py | 17 +++++------------ Lib/test/test_codeop.py | 14 ++++++++------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/Lib/codeop.py b/Lib/codeop.py index eea6cbc701..adf000ba29 100644 --- a/Lib/codeop.py +++ b/Lib/codeop.py @@ -44,6 +44,7 @@ # Caveat emptor: These flags are undocumented on purpose and depending # on their effect outside the standard library is **unsupported**. PyCF_DONT_IMPLY_DEDENT = 0x200 +PyCF_ONLY_AST = 0x400 PyCF_ALLOW_INCOMPLETE_INPUT = 0x4000 def _maybe_compile(compiler, source, filename, symbol): @@ -73,15 +74,6 @@ def _maybe_compile(compiler, source, filename, symbol): return compiler(source, filename, symbol, incomplete_input=False) -def _is_syntax_error(err1, err2): - rep1 = repr(err1) - rep2 = repr(err2) - if "was never closed" in rep1 and "was never closed" in rep2: - return False - if rep1 == rep2: - return True - return False - def _compile(source, filename, symbol, incomplete_input=True): flags = 0 if incomplete_input: @@ -89,7 +81,6 @@ def _compile(source, filename, symbol, incomplete_input=True): flags |= PyCF_DONT_IMPLY_DEDENT return compile(source, filename, symbol, flags) - def compile_command(source, filename="", symbol="single"): r"""Compile a command and determine whether it is incomplete. @@ -119,12 +110,14 @@ class Compile: def __init__(self): self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT - def __call__(self, source, filename, symbol, **kwargs): - flags = self.flags + def __call__(self, source, filename, symbol, flags=0, **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) + if flags & PyCF_ONLY_AST: + return codeob # this is an ast.Module in this case for feature in _features: if codeob.co_flags & feature.compiler_flag: self.flags |= feature.compiler_flag diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py index 1036b970cd..c62e3748e6 100644 --- a/Lib/test/test_codeop.py +++ b/Lib/test/test_codeop.py @@ -227,6 +227,9 @@ def test_incomplete(self): ai("(x for x in") ai("(x for x in (") + ai('a = f"""') + ai('a = \\') + def test_invalid(self): ai = self.assertInvalid ai("a b") @@ -300,12 +303,11 @@ def test_warning(self): warnings.simplefilter('error', SyntaxWarning) compile_command(r"'\e'", symbol='exec') - # TODO: RUSTPYTHON - #def test_incomplete_warning(self): - # with warnings.catch_warnings(record=True) as w: - # warnings.simplefilter('always') - # self.assertIncomplete("'\\e' + (") - # self.assertEqual(w, []) + def test_incomplete_warning(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + self.assertIncomplete("'\\e' + (") + self.assertEqual(w, []) # TODO: RUSTPYTHON @unittest.expectedFailure