Skip to content

Commit 6a08a75

Browse files
Wulian233nineteendoambv
authored
gh-124960: Fixed barry_as_FLUFL future flag does not work in new REPL (#124999)
Co-authored-by: Nice Zombies <[email protected]> Co-authored-by: Łukasz Langa <[email protected]>
1 parent 5f4e5b5 commit 6a08a75

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

Lib/_pyrepl/console.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ def _excepthook(self, typ, value, tb):
174174

175175
def runsource(self, source, filename="<input>", symbol="single"):
176176
try:
177-
tree = ast.parse(source)
177+
tree = self.compile.compiler(
178+
source,
179+
filename,
180+
"exec",
181+
ast.PyCF_ONLY_AST,
182+
incomplete_input=False,
183+
)
178184
except (SyntaxError, OverflowError, ValueError):
179185
self.showsyntaxerror(filename, source=source)
180186
return False
@@ -185,7 +191,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
185191
the_symbol = symbol if stmt is last_stmt else "exec"
186192
item = wrapper([stmt])
187193
try:
188-
code = self.compile.compiler(item, filename, the_symbol, dont_inherit=True)
194+
code = self.compile.compiler(item, filename, the_symbol)
189195
except SyntaxError as e:
190196
if e.args[0] == "'await' outside function":
191197
python = os.path.basename(sys.executable)

Lib/codeop.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
# Caveat emptor: These flags are undocumented on purpose and depending
4545
# on their effect outside the standard library is **unsupported**.
4646
PyCF_DONT_IMPLY_DEDENT = 0x200
47+
PyCF_ONLY_AST = 0x400
4748
PyCF_ALLOW_INCOMPLETE_INPUT = 0x4000
4849

4950
def _maybe_compile(compiler, source, filename, symbol):
@@ -109,12 +110,14 @@ class Compile:
109110
def __init__(self):
110111
self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT
111112

112-
def __call__(self, source, filename, symbol, **kwargs):
113-
flags = self.flags
113+
def __call__(self, source, filename, symbol, flags=0, **kwargs):
114+
flags |= self.flags
114115
if kwargs.get('incomplete_input', True) is False:
115116
flags &= ~PyCF_DONT_IMPLY_DEDENT
116117
flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT
117118
codeob = compile(source, filename, symbol, flags, True)
119+
if flags & PyCF_ONLY_AST:
120+
return codeob # this is an ast.Module in this case
118121
for feature in _features:
119122
if codeob.co_flags & feature.compiler_flag:
120123
self.flags |= feature.compiler_flag

Lib/test/test_pyrepl/test_interact.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,38 @@ def test_runsource_shows_syntax_error_for_failed_compilation(self):
119119

120120
def test_no_active_future(self):
121121
console = InteractiveColoredConsole()
122-
source = "x: int = 1; print(__annotate__(1))"
122+
source = dedent("""\
123+
x: int = 1
124+
print(__annotate__(1))
125+
""")
123126
f = io.StringIO()
124127
with contextlib.redirect_stdout(f):
125128
result = console.runsource(source)
126129
self.assertFalse(result)
127130
self.assertEqual(f.getvalue(), "{'x': <class 'int'>}\n")
128131

132+
def test_future_annotations(self):
133+
console = InteractiveColoredConsole()
134+
source = dedent("""\
135+
from __future__ import annotations
136+
def g(x: int): ...
137+
print(g.__annotations__)
138+
""")
139+
f = io.StringIO()
140+
with contextlib.redirect_stdout(f):
141+
result = console.runsource(source)
142+
self.assertFalse(result)
143+
self.assertEqual(f.getvalue(), "{'x': 'int'}\n")
144+
145+
def test_future_barry_as_flufl(self):
146+
console = InteractiveColoredConsole()
147+
f = io.StringIO()
148+
with contextlib.redirect_stdout(f):
149+
result = console.runsource("from __future__ import barry_as_FLUFL\n")
150+
result = console.runsource("""print("black" <> 'blue')\n""")
151+
self.assertFalse(result)
152+
self.assertEqual(f.getvalue(), "True\n")
153+
129154

130155
class TestMoreLines(unittest.TestCase):
131156
def test_invalid_syntax_single_line(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix support for the ``barry_as_FLUFL`` future flag in the new REPL.

0 commit comments

Comments
 (0)