Skip to content

Update codeop.py from 3.13.5 #6026

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

Merged
merged 1 commit into from
Jul 25, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions Lib/codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -73,23 +74,13 @@ 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:
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 @@ -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
Expand Down
14 changes: 8 additions & 6 deletions Lib/test/test_codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
Loading