Skip to content
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

gh-108996: fix and enable test_msvcrt #109226

Merged
merged 10 commits into from
Sep 22, 2023
43 changes: 25 additions & 18 deletions Lib/test/test_msvcrt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import ctypes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctypes is not always available. Please use try: import ctypes except ImportError: ctypes = None, and then skip tests using ctypes if ctypes is None.

import os
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 Down Expand Up @@ -64,35 +63,43 @@ def test_get_osfhandle(self):

class TestConsoleIO(unittest.TestCase):
def test_kbhit(self):
h = msvcrt.get_osfhandle(sys.stdin.fileno())
ctypes.windll.kernel32.FlushConsoleInputBuffer(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)
self.assertEqual(msvcrt.getwch(), c)
finally:
sys.stdin = old_stdin
with open('CONIN$', 'rb', buffering=0) as stdin:
h = msvcrt.get_osfhandle(stdin.fileno())
ctypes.windll.kernel32.FlushConsoleInputBuffer(h)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should be exposed in msvcrt, maybe as a private function just for testing.


old_stdin = sys.stdin
try:
sys.stdin = stdin
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)
self.assertEqual(msvcrt.getwche(), c)
finally:
sys.stdin = old_stdin
with open('CONIN$', 'rb', buffering=0) as stdin:
h = msvcrt.get_osfhandle(stdin.fileno())
ctypes.windll.kernel32.FlushConsoleInputBuffer(h)

old_stdin = sys.stdin
try:
sys.stdin = stdin
zooba marked this conversation as resolved.
Show resolved Hide resolved
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``.