summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_msvcrt.py
diff options
context:
space:
mode:
authorAN Long <aisk@users.noreply.github.com>2023-10-05 17:52:26 (GMT)
committerGitHub <noreply@github.com>2023-10-05 17:52:26 (GMT)
commit1f3af03f83fead5cd86d54e1b2f47fc5866e635c (patch)
tree5c0ea54ef7283de3d08f0bf37143e996a54f01ae /Lib/test/test_msvcrt.py
parenta13620685f68957c965fca89343a0e91f95f1bab (diff)
downloadcpython-1f3af03f83fead5cd86d54e1b2f47fc5866e635c.zip
cpython-1f3af03f83fead5cd86d54e1b2f47fc5866e635c.tar.gz
cpython-1f3af03f83fead5cd86d54e1b2f47fc5866e635c.tar.bz2
gh-110147: test_msvcrt: run console I/O tests in new processes (#110268)
Diffstat (limited to 'Lib/test/test_msvcrt.py')
-rw-r--r--Lib/test/test_msvcrt.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py
index 81ec130..600c444 100644
--- a/Lib/test/test_msvcrt.py
+++ b/Lib/test/test_msvcrt.py
@@ -1,17 +1,17 @@
import os
+import subprocess
import sys
import unittest
+from textwrap import dedent
-from test.support import os_helper
+from test.support import os_helper, requires_resource
from test.support.os_helper import TESTFN, TESTFN_ASCII
if sys.platform != "win32":
raise unittest.SkipTest("windows related tests")
import _winapi
-import msvcrt;
-
-from _testconsole import write_input, flush_console_input_buffer
+import msvcrt
class TestFileOperations(unittest.TestCase):
@@ -61,34 +61,45 @@ c_encoded = b'\x57\x5b' # utf-16-le (which windows internally used) encoded char
class TestConsoleIO(unittest.TestCase):
+ # CREATE_NEW_CONSOLE creates a "popup" window.
+ @requires_resource('gui')
+ def run_in_separated_process(self, code):
+ # Run test in a seprated process to avoid stdin conflicts.
+ # See: gh-110147
+ cmd = [sys.executable, '-c', code]
+ subprocess.run(cmd, check=True, capture_output=True,
+ creationflags=subprocess.CREATE_NEW_CONSOLE)
+
def test_kbhit(self):
- h = msvcrt.get_osfhandle(sys.stdin.fileno())
- flush_console_input_buffer(h)
- self.assertEqual(msvcrt.kbhit(), 0)
+ code = dedent('''
+ import msvcrt
+ assert msvcrt.kbhit() == 0
+ ''')
+ self.run_in_separated_process(code)
def test_getch(self):
msvcrt.ungetch(b'c')
self.assertEqual(msvcrt.getch(), b'c')
- def test_getwch(self):
- with open('CONIN$', 'rb', buffering=0) as stdin:
- h = msvcrt.get_osfhandle(stdin.fileno())
- flush_console_input_buffer(h)
+ def check_getwch(self, funcname):
+ code = dedent(f'''
+ import msvcrt
+ from _testconsole import write_input
+ with open("CONIN$", "rb", buffering=0) as stdin:
+ write_input(stdin, {ascii(c_encoded)})
+ assert msvcrt.{funcname}() == "{c}"
+ ''')
+ self.run_in_separated_process(code)
- write_input(stdin, c_encoded)
- self.assertEqual(msvcrt.getwch(), c)
+ def test_getwch(self):
+ self.check_getwch('getwch')
def test_getche(self):
msvcrt.ungetch(b'c')
self.assertEqual(msvcrt.getche(), b'c')
def test_getwche(self):
- with open('CONIN$', 'rb', buffering=0) as stdin:
- h = msvcrt.get_osfhandle(stdin.fileno())
- flush_console_input_buffer(h)
-
- write_input(stdin, c_encoded)
- self.assertEqual(msvcrt.getwche(), c)
+ self.check_getwch('getwche')
def test_putch(self):
msvcrt.putch(b'c')