Skip to content

Commit d12632c

Browse files
committed
gh-159672: Restore flags support in codeop.compile_command
1 parent 9d1f704 commit d12632c

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎Lib/codeop.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
PyCF_ONLY_AST = 0x400
4848
PyCF_ALLOW_INCOMPLETE_INPUT = 0x4000
4949

50-
def _maybe_compile(compiler, source, filename, symbol):
50+
def _maybe_compile(compiler, source, filename, symbol, flags):
5151
# Check for source consisting of only blank lines and comments.
5252
for line in source.split("\n"):
5353
line = line.strip()
@@ -61,22 +61,21 @@ def _maybe_compile(compiler, source, filename, symbol):
6161
with warnings.catch_warnings():
6262
warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning))
6363
try:
64-
compiler(source, filename, symbol)
64+
compiler(source, filename, symbol, flags=flags)
6565
except _IncompleteInputError:
6666
return None
6767
except SyntaxError: # Let other compile() errors propagate.
6868
pass
6969

7070
return compiler(source, filename, symbol, incomplete_input=False)
7171

72-
def _compile(source, filename, symbol, incomplete_input=True):
73-
flags = 0
72+
def _compile(source, filename, symbol, incomplete_input=True, *, flags=0):
7473
if incomplete_input:
7574
flags |= PyCF_ALLOW_INCOMPLETE_INPUT
7675
flags |= PyCF_DONT_IMPLY_DEDENT
7776
return compile(source, filename, symbol, flags)
7877

79-
def compile_command(source, filename="<input>", symbol="single"):
78+
def compile_command(source, filename="<input>", symbol="single", flags=0):
8079
r"""Compile a command and determine whether it is incomplete.
8180
8281
Arguments:
@@ -95,7 +94,7 @@ def compile_command(source, filename="<input>", symbol="single"):
9594
syntax error (OverflowError and ValueError can be produced by
9695
malformed literals).
9796
"""
98-
return _maybe_compile(_compile, source, filename, symbol)
97+
return _maybe_compile(_compile, source, filename, symbol, flags)
9998

10099
class Compile:
101100
"""Instances of this class behave much like the built-in compile
@@ -147,4 +146,4 @@ def __call__(self, source, filename="<input>", symbol="single"):
147146
syntax error (OverflowError and ValueError can be produced by
148147
malformed literals).
149148
"""
150-
return _maybe_compile(self.compiler, source, filename, symbol)
149+
return _maybe_compile(self.compiler, source, filename, symbol, flags=self.compiler.flags)

‎Lib/test/test_codeop.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919

2020

2121
class CodeopTests(unittest.TestCase):
22+
def test_compile_command_flags(self):
23+
import __future__
24+
25+
source = "x = 1"
26+
expected = compile(source, "<input>", "single")
27+
self.assertEqual(compile_command(source, "<input>", "single", 0), expected)
28+
self.assertEqual(compile_command(source, flags=0), expected)
29+
30+
flags = __future__.barry_as_FLUFL.compiler_flag
31+
source = "if 1 <> 2:\n"
32+
self.assertIsNone(compile_command(source, "<input>", "single", flags))
33+
self.assertIsNone(compile_command(source, flags=flags))
34+
2235
def assertValid(self, str, symbol='single', *, compiler):
2336
'''succeed iff str is a valid piece of code'''
2437
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)

0 commit comments

Comments
 (0)