Skip to content

gh-108996: fix and enable test_msvcrt #109226

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

Merged
merged 10 commits into from
Sep 22, 2023
30 changes: 13 additions & 17 deletions Lib/test/test_msvcrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import sys
import unittest

raise unittest.SkipTest("FIXME! broken test see: https://github.com/python/cpython/pull/109004")

from test.support import os_helper
from test.support.os_helper import TESTFN, TESTFN_ASCII

Expand All @@ -13,7 +11,7 @@
import _winapi
import msvcrt;

from _testconsole import write_input
from _testconsole import write_input, flush_console_input_buffer


class TestFileOperations(unittest.TestCase):
Expand Down Expand Up @@ -64,35 +62,33 @@ def test_get_osfhandle(self):

class TestConsoleIO(unittest.TestCase):
def test_kbhit(self):
h = msvcrt.get_osfhandle(sys.stdin.fileno())
flush_console_input_buffer(h)
self.assertEqual(msvcrt.kbhit(), 0)

def test_getch(self):
msvcrt.ungetch(b'c')
self.assertEqual(msvcrt.getch(), b'c')

def test_getwch(self):
stdin = open('CONIN$', 'r')
old_stdin = sys.stdin
try:
sys.stdin = stdin
write_input(stdin.buffer.raw, c_encoded)
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.getwch(), c)
finally:
sys.stdin = old_stdin

def test_getche(self):
msvcrt.ungetch(b'c')
self.assertEqual(msvcrt.getche(), b'c')

def test_getwche(self):
stdin = open('CONIN$', 'r')
old_stdin = sys.stdin
try:
sys.stdin = stdin
write_input(stdin.buffer.raw, c_encoded)
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)
finally:
sys.stdin = old_stdin

def test_putch(self):
msvcrt.putch(b'c')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix and enable ``test_msvcrt``.
37 changes: 37 additions & 0 deletions PC/_testconsole.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ PyModuleDef_Slot testconsole_slots[] = {
{0, NULL},
};

/*[python input]
class HANDLE_converter(CConverter):
type = 'void *'
format_unit = '"_Py_PARSE_UINTPTR"'

def parse_arg(self, argname, displayname, *, limited_capi):
return self.format_code("""
{paramname} = PyLong_AsVoidPtr({argname});
if (!{paramname} && PyErr_Occurred()) {{{{
goto exit;
}}}}
""",
argname=argname)
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=380aa5c91076742b]*/
/*[python end generated code:]*/

/*[clinic input]
module _testconsole

Expand Down Expand Up @@ -116,11 +133,31 @@ _testconsole_read_output_impl(PyObject *module, PyObject *file)
Py_RETURN_NONE;
}

/*[clinic input]
_testconsole.flush_console_input_buffer
handle: HANDLE

Flushes the console input buffer.

All input records currently in the input buffer are discarded.
[clinic start generated code]*/

static PyObject *
_testconsole_flush_console_input_buffer_impl(PyObject *module, void *handle)
/*[clinic end generated code: output=1f923a81331465ce input=be8203ae84a288f5]*/
/*[clinic end generated code:]*/
{
FlushConsoleInputBuffer(handle);

Py_RETURN_NONE;
}

#include "clinic\_testconsole.c.h"

PyMethodDef testconsole_methods[] = {
_TESTCONSOLE_WRITE_INPUT_METHODDEF
_TESTCONSOLE_READ_OUTPUT_METHODDEF
_TESTCONSOLE_FLUSH_CONSOLE_INPUT_BUFFER_METHODDEF
{NULL, NULL}
};

Expand Down
70 changes: 69 additions & 1 deletion PC/clinic/_testconsole.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.