Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions Lib/_pyrepl/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ def __init__(
) -> None:
super().__init__(locals=locals, filename=filename, local_exit=local_exit) # type: ignore[call-arg]
self.can_colorize = _colorize.can_colorize()
self.barry_as_FLUFL = False

def check_barry_as_FLUFL(self, tree):
for node in ast.walk(tree):
if isinstance(node, ast.ImportFrom) and node.module == "__future__":
if any(alias.name == "barry_as_FLUFL" for alias in node.names):
self.barry_as_FLUFL = True

def showsyntaxerror(self, filename=None, **kwargs):
super().showsyntaxerror(filename=filename, **kwargs)
Expand All @@ -173,8 +180,11 @@ def _excepthook(self, typ, value, tb):
self.write(''.join(lines))

def runsource(self, source, filename="<input>", symbol="single"):
if self.barry_as_FLUFL:
source = source.replace("<>", "!=")
try:
tree = ast.parse(source)
self.check_barry_as_FLUFL(tree)
except (SyntaxError, OverflowError, ValueError):
self.showsyntaxerror(filename, source=source)
return False
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_pyrepl/test_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ def f(x, x): ...
SyntaxError: duplicate argument 'x' in function definition"""
self.assertIn(r, f.getvalue())

@force_not_colorized
def test_check_barry_as_FLUFL_show_syntax_error(self):
console = InteractiveColoredConsole()
source = "1 <> 2"
f = io.StringIO()
with contextlib.redirect_stderr(f):
result = console.runsource(source)
self.assertFalse(result)
r = """
1 <> 2
^^
SyntaxError: invalid syntax"""
self.assertIn(r, f.getvalue())

@force_not_colorized
def test_check_barry_as_FLUFL(self):
console = InteractiveColoredConsole()
source = "from __future__ import barry_as_FLUFL; 1 <> 2"
f = io.StringIO()
with contextlib.redirect_stderr(f):
result = console.runsource(source)
self.assertEqual(result, True)

def test_runsource_shows_syntax_error_for_failed_compilation(self):
console = InteractiveColoredConsole()
source = "print('Hello, world!'"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed barry_as_FLUFL future flag does not work in new REPL. Patch by Jiahao
Li
Loading