Skip to content

Commit 4230d7c

Browse files
authored
gh-108996: fix and enable test_msvcrt (#109226)
* Add _testconsole.flush_console_input_buffer() function. * test_kbhit(), test_getwch() and test_getwche() now call flush_console_input_buffer(). * Don't override sys.stdin anymore (not needed).
1 parent 3f5c564 commit 4230d7c

File tree

4 files changed

+120
-18
lines changed

4 files changed

+120
-18
lines changed

Lib/test/test_msvcrt.py

+13-17
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import sys
33
import unittest
44

5-
raise unittest.SkipTest("FIXME! broken test see: https://github.com/python/cpython/pull/109004")
6-
75
from test.support import os_helper
86
from test.support.os_helper import TESTFN, TESTFN_ASCII
97

@@ -13,7 +11,7 @@
1311
import _winapi
1412
import msvcrt;
1513

16-
from _testconsole import write_input
14+
from _testconsole import write_input, flush_console_input_buffer
1715

1816

1917
class TestFileOperations(unittest.TestCase):
@@ -64,35 +62,33 @@ def test_get_osfhandle(self):
6462

6563
class TestConsoleIO(unittest.TestCase):
6664
def test_kbhit(self):
65+
h = msvcrt.get_osfhandle(sys.stdin.fileno())
66+
flush_console_input_buffer(h)
6767
self.assertEqual(msvcrt.kbhit(), 0)
6868

6969
def test_getch(self):
7070
msvcrt.ungetch(b'c')
7171
self.assertEqual(msvcrt.getch(), b'c')
7272

7373
def test_getwch(self):
74-
stdin = open('CONIN$', 'r')
75-
old_stdin = sys.stdin
76-
try:
77-
sys.stdin = stdin
78-
write_input(stdin.buffer.raw, c_encoded)
74+
with open('CONIN$', 'rb', buffering=0) as stdin:
75+
h = msvcrt.get_osfhandle(stdin.fileno())
76+
flush_console_input_buffer(h)
77+
78+
write_input(stdin, c_encoded)
7979
self.assertEqual(msvcrt.getwch(), c)
80-
finally:
81-
sys.stdin = old_stdin
8280

8381
def test_getche(self):
8482
msvcrt.ungetch(b'c')
8583
self.assertEqual(msvcrt.getche(), b'c')
8684

8785
def test_getwche(self):
88-
stdin = open('CONIN$', 'r')
89-
old_stdin = sys.stdin
90-
try:
91-
sys.stdin = stdin
92-
write_input(stdin.buffer.raw, c_encoded)
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)
9391
self.assertEqual(msvcrt.getwche(), c)
94-
finally:
95-
sys.stdin = old_stdin
9692

9793
def test_putch(self):
9894
msvcrt.putch(b'c')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix and enable ``test_msvcrt``.

PC/_testconsole.c

+37
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ PyModuleDef_Slot testconsole_slots[] = {
3535
{0, NULL},
3636
};
3737

38+
/*[python input]
39+
class HANDLE_converter(CConverter):
40+
type = 'void *'
41+
format_unit = '"_Py_PARSE_UINTPTR"'
42+
43+
def parse_arg(self, argname, displayname, *, limited_capi):
44+
return self.format_code("""
45+
{paramname} = PyLong_AsVoidPtr({argname});
46+
if (!{paramname} && PyErr_Occurred()) {{{{
47+
goto exit;
48+
}}}}
49+
""",
50+
argname=argname)
51+
[python start generated code]*/
52+
/*[python end generated code: output=da39a3ee5e6b4b0d input=380aa5c91076742b]*/
53+
/*[python end generated code:]*/
54+
3855
/*[clinic input]
3956
module _testconsole
4057
@@ -116,11 +133,31 @@ _testconsole_read_output_impl(PyObject *module, PyObject *file)
116133
Py_RETURN_NONE;
117134
}
118135

136+
/*[clinic input]
137+
_testconsole.flush_console_input_buffer
138+
handle: HANDLE
139+
140+
Flushes the console input buffer.
141+
142+
All input records currently in the input buffer are discarded.
143+
[clinic start generated code]*/
144+
145+
static PyObject *
146+
_testconsole_flush_console_input_buffer_impl(PyObject *module, void *handle)
147+
/*[clinic end generated code: output=1f923a81331465ce input=be8203ae84a288f5]*/
148+
/*[clinic end generated code:]*/
149+
{
150+
FlushConsoleInputBuffer(handle);
151+
152+
Py_RETURN_NONE;
153+
}
154+
119155
#include "clinic\_testconsole.c.h"
120156

121157
PyMethodDef testconsole_methods[] = {
122158
_TESTCONSOLE_WRITE_INPUT_METHODDEF
123159
_TESTCONSOLE_READ_OUTPUT_METHODDEF
160+
_TESTCONSOLE_FLUSH_CONSOLE_INPUT_BUFFER_METHODDEF
124161
{NULL, NULL}
125162
};
126163

PC/clinic/_testconsole.c.h

+69-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)