mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Issue #21425: Fix flushing of standard streams in the interactive interpreter.
This commit is contained in:
parent
3d1bc608a8
commit
9845c7ebc5
4 changed files with 57 additions and 4 deletions
|
|
@ -1,5 +1,6 @@
|
|||
# tests command line execution of scripts
|
||||
|
||||
import contextlib
|
||||
import importlib
|
||||
import importlib.machinery
|
||||
import zipimport
|
||||
|
|
@ -8,6 +9,7 @@ import sys
|
|||
import os
|
||||
import os.path
|
||||
import py_compile
|
||||
import subprocess
|
||||
|
||||
import textwrap
|
||||
from test import support
|
||||
|
|
@ -173,6 +175,53 @@ class CmdLineTest(unittest.TestCase):
|
|||
expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
|
||||
self.assertIn(expected, out)
|
||||
|
||||
@contextlib.contextmanager
|
||||
def interactive_python(self, separate_stderr=False):
|
||||
if separate_stderr:
|
||||
p = spawn_python('-i', bufsize=1, stderr=subprocess.PIPE)
|
||||
stderr = p.stderr
|
||||
else:
|
||||
p = spawn_python('-i', bufsize=1, stderr=subprocess.STDOUT)
|
||||
stderr = p.stdout
|
||||
try:
|
||||
# Drain stderr until prompt
|
||||
while True:
|
||||
data = stderr.read(4)
|
||||
if data == b">>> ":
|
||||
break
|
||||
stderr.readline()
|
||||
yield p
|
||||
finally:
|
||||
kill_python(p)
|
||||
stderr.close()
|
||||
|
||||
def check_repl_stdout_flush(self, separate_stderr=False):
|
||||
with self.interactive_python(separate_stderr) as p:
|
||||
p.stdin.write(b"print('foo')\n")
|
||||
p.stdin.flush()
|
||||
self.assertEqual(b'foo', p.stdout.readline().strip())
|
||||
|
||||
def check_repl_stderr_flush(self, separate_stderr=False):
|
||||
with self.interactive_python(separate_stderr) as p:
|
||||
p.stdin.write(b"1/0\n")
|
||||
p.stdin.flush()
|
||||
stderr = p.stderr if separate_stderr else p.stdout
|
||||
self.assertIn(b'Traceback ', stderr.readline())
|
||||
self.assertIn(b'File "<stdin>"', stderr.readline())
|
||||
self.assertIn(b'ZeroDivisionError', stderr.readline())
|
||||
|
||||
def test_repl_stdout_flush(self):
|
||||
self.check_repl_stdout_flush()
|
||||
|
||||
def test_repl_stdout_flush_separate_stderr(self):
|
||||
self.check_repl_stdout_flush(True)
|
||||
|
||||
def test_repl_stderr_flush(self):
|
||||
self.check_repl_stderr_flush()
|
||||
|
||||
def test_repl_stderr_flush_separate_stderr(self):
|
||||
self.check_repl_stderr_flush(True)
|
||||
|
||||
def test_basic_script(self):
|
||||
with temp_dir() as script_dir:
|
||||
script_name = _make_test_script(script_dir, 'script')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue