|
1 | 1 | import os |
| 2 | +import subprocess |
2 | 3 | import sys |
3 | 4 | import unittest |
| 5 | +from textwrap import dedent |
4 | 6 |
|
5 | | -from test.support import os_helper |
| 7 | +from test.support import os_helper, requires_resource |
6 | 8 | from test.support.os_helper import TESTFN, TESTFN_ASCII |
7 | 9 |
|
8 | 10 | if sys.platform != "win32": |
9 | 11 | raise unittest.SkipTest("windows related tests") |
10 | 12 |
|
11 | 13 | import _winapi |
12 | | -import msvcrt; |
13 | | - |
14 | | -from _testconsole import write_input, flush_console_input_buffer |
| 14 | +import msvcrt |
15 | 15 |
|
16 | 16 |
|
17 | 17 | class TestFileOperations(unittest.TestCase): |
@@ -61,34 +61,45 @@ def test_get_osfhandle(self): |
61 | 61 |
|
62 | 62 |
|
63 | 63 | class TestConsoleIO(unittest.TestCase): |
| 64 | + # CREATE_NEW_CONSOLE creates a "popup" window. |
| 65 | + @requires_resource('gui') |
| 66 | + def run_in_separated_process(self, code): |
| 67 | + # Run test in a seprated process to avoid stdin conflicts. |
| 68 | + # See: gh-110147 |
| 69 | + cmd = [sys.executable, '-c', code] |
| 70 | + subprocess.run(cmd, check=True, capture_output=True, |
| 71 | + creationflags=subprocess.CREATE_NEW_CONSOLE) |
| 72 | + |
64 | 73 | def test_kbhit(self): |
65 | | - h = msvcrt.get_osfhandle(sys.stdin.fileno()) |
66 | | - flush_console_input_buffer(h) |
67 | | - self.assertEqual(msvcrt.kbhit(), 0) |
| 74 | + code = dedent(''' |
| 75 | + import msvcrt |
| 76 | + assert msvcrt.kbhit() == 0 |
| 77 | + ''') |
| 78 | + self.run_in_separated_process(code) |
68 | 79 |
|
69 | 80 | def test_getch(self): |
70 | 81 | msvcrt.ungetch(b'c') |
71 | 82 | self.assertEqual(msvcrt.getch(), b'c') |
72 | 83 |
|
73 | | - def test_getwch(self): |
74 | | - with open('CONIN$', 'rb', buffering=0) as stdin: |
75 | | - h = msvcrt.get_osfhandle(stdin.fileno()) |
76 | | - flush_console_input_buffer(h) |
| 84 | + def check_getwch(self, funcname): |
| 85 | + code = dedent(f''' |
| 86 | + import msvcrt |
| 87 | + from _testconsole import write_input |
| 88 | + with open("CONIN$", "rb", buffering=0) as stdin: |
| 89 | + write_input(stdin, {ascii(c_encoded)}) |
| 90 | + assert msvcrt.{funcname}() == "{c}" |
| 91 | + ''') |
| 92 | + self.run_in_separated_process(code) |
77 | 93 |
|
78 | | - write_input(stdin, c_encoded) |
79 | | - self.assertEqual(msvcrt.getwch(), c) |
| 94 | + def test_getwch(self): |
| 95 | + self.check_getwch('getwch') |
80 | 96 |
|
81 | 97 | def test_getche(self): |
82 | 98 | msvcrt.ungetch(b'c') |
83 | 99 | self.assertEqual(msvcrt.getche(), b'c') |
84 | 100 |
|
85 | 101 | def test_getwche(self): |
86 | | - with open('CONIN$', 'rb', buffering=0) as stdin: |
87 | | - h = msvcrt.get_osfhandle(stdin.fileno()) |
88 | | - flush_console_input_buffer(h) |
89 | | - |
90 | | - write_input(stdin, c_encoded) |
91 | | - self.assertEqual(msvcrt.getwche(), c) |
| 102 | + self.check_getwch('getwche') |
92 | 103 |
|
93 | 104 | def test_putch(self): |
94 | 105 | msvcrt.putch(b'c') |
|
0 commit comments