Skip to content

gh-136632: use support.captured_std* in test_pyrepl/test_interact.py #136633

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 12 additions & 25 deletions Lib/test/test_pyrepl/test_interact.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import contextlib
import io
import unittest
import warnings
from unittest.mock import patch
from textwrap import dedent

from test.support import force_not_colorized
from test.support import force_not_colorized, captured_stderr, captured_stdout

from _pyrepl.console import InteractiveColoredConsole
from _pyrepl.simple_interact import _more_lines
Expand All @@ -28,11 +26,10 @@ def bar(self):
a
""")
console = InteractiveColoredConsole(namespace, filename="<stdin>")
f = io.StringIO()
with (
patch.object(InteractiveColoredConsole, "showsyntaxerror") as showsyntaxerror,
patch.object(InteractiveColoredConsole, "runsource", wraps=console.runsource) as runsource,
contextlib.redirect_stdout(f),
captured_stdout(),
):
more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
self.assertFalse(more)
Expand All @@ -48,8 +45,7 @@ def test_multiple_statements_output(self):
a
""")
console = InteractiveColoredConsole(namespace, filename="<stdin>")
f = io.StringIO()
with contextlib.redirect_stdout(f):
with captured_stdout() as f:
more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
self.assertFalse(more)
self.assertEqual(f.getvalue(), "1\n")
Expand All @@ -61,8 +57,7 @@ def test_multiple_statements_fail_early(self):
raise Exception('foobar')
print('spam', 'eggs', sep='&')
""")
f = io.StringIO()
with contextlib.redirect_stderr(f):
with captured_stderr() as f:
console.runsource(code)
self.assertIn('Exception: foobar', f.getvalue())
self.assertNotIn('spam&eggs', f.getvalue())
Expand All @@ -71,8 +66,7 @@ def test_empty(self):
namespace = {}
code = ""
console = InteractiveColoredConsole(namespace, filename="<stdin>")
f = io.StringIO()
with contextlib.redirect_stdout(f):
with captured_stdout() as f:
more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
self.assertFalse(more)
self.assertEqual(f.getvalue(), "")
Expand All @@ -87,17 +81,15 @@ def test_runsource_compiles_and_runs_code(self):
def test_runsource_returns_false_for_successful_compilation(self):
console = InteractiveColoredConsole()
source = "print('Hello, world!')"
f = io.StringIO()
with contextlib.redirect_stdout(f):
with captured_stdout():
result = console.runsource(source)
self.assertFalse(result)

@force_not_colorized
def test_runsource_returns_false_for_failed_compilation(self):
console = InteractiveColoredConsole()
source = "print('Hello, world!'"
f = io.StringIO()
with contextlib.redirect_stderr(f):
with captured_stderr() as f:
result = console.runsource(source)
self.assertFalse(result)
self.assertIn('SyntaxError', f.getvalue())
Expand All @@ -106,8 +98,7 @@ def test_runsource_returns_false_for_failed_compilation(self):
def test_runsource_show_syntax_error_location(self):
console = InteractiveColoredConsole()
source = "def f(x, x): ..."
f = io.StringIO()
with contextlib.redirect_stderr(f):
with captured_stderr() as f:
result = console.runsource(source)
self.assertFalse(result)
r = """
Expand All @@ -134,8 +125,7 @@ def test_runsource_shows_syntax_error_for_failed_compilation(self):
def test_runsource_survives_null_bytes(self):
console = InteractiveColoredConsole()
source = "\x00\n"
f = io.StringIO()
with contextlib.redirect_stdout(f), contextlib.redirect_stderr(f):
with captured_stdout(), captured_stderr() as f:
result = console.runsource(source)
self.assertFalse(result)
self.assertIn("source code string cannot contain null bytes", f.getvalue())
Expand All @@ -146,8 +136,7 @@ def test_no_active_future(self):
x: int = 1
print(__annotate__(1))
""")
f = io.StringIO()
with contextlib.redirect_stdout(f):
with captured_stdout() as f:
result = console.runsource(source)
self.assertFalse(result)
self.assertEqual(f.getvalue(), "{'x': <class 'int'>}\n")
Expand All @@ -159,16 +148,14 @@ def test_future_annotations(self):
def g(x: int): ...
print(g.__annotations__)
""")
f = io.StringIO()
with contextlib.redirect_stdout(f):
with captured_stdout() as f:
result = console.runsource(source)
self.assertFalse(result)
self.assertEqual(f.getvalue(), "{'x': 'int'}\n")

def test_future_barry_as_flufl(self):
console = InteractiveColoredConsole()
f = io.StringIO()
with contextlib.redirect_stdout(f):
with captured_stdout() as f:
result = console.runsource("from __future__ import barry_as_FLUFL\n")
result = console.runsource("""print("black" <> 'blue')\n""")
self.assertFalse(result)
Expand Down
Loading