2023-09-09 00:38:38 +02:00
|
|
|
import os
|
2023-10-05 19:52:26 +02:00
|
|
|
import subprocess
|
2023-09-09 00:38:38 +02:00
|
|
|
import sys
|
|
|
|
import unittest
|
2023-10-05 19:52:26 +02:00
|
|
|
from textwrap import dedent
|
2023-09-09 00:38:38 +02:00
|
|
|
|
2023-10-05 19:52:26 +02:00
|
|
|
from test.support import os_helper, requires_resource
|
2023-09-09 00:38:38 +02:00
|
|
|
from test.support.os_helper import TESTFN, TESTFN_ASCII
|
|
|
|
|
|
|
|
if sys.platform != "win32":
|
|
|
|
raise unittest.SkipTest("windows related tests")
|
|
|
|
|
|
|
|
import _winapi
|
2023-10-05 19:52:26 +02:00
|
|
|
import msvcrt
|
2023-09-09 00:38:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestFileOperations(unittest.TestCase):
|
|
|
|
def test_locking(self):
|
|
|
|
with open(TESTFN, "w") as f:
|
|
|
|
self.addCleanup(os_helper.unlink, TESTFN)
|
|
|
|
|
|
|
|
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
|
|
|
|
self.assertRaises(OSError, msvcrt.locking, f.fileno(), msvcrt.LK_NBLCK, 1)
|
|
|
|
|
|
|
|
def test_unlockfile(self):
|
|
|
|
with open(TESTFN, "w") as f:
|
|
|
|
self.addCleanup(os_helper.unlink, TESTFN)
|
|
|
|
|
|
|
|
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
|
|
|
|
msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
|
|
|
|
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
|
|
|
|
|
|
|
|
def test_setmode(self):
|
|
|
|
with open(TESTFN, "w") as f:
|
|
|
|
self.addCleanup(os_helper.unlink, TESTFN)
|
|
|
|
|
|
|
|
msvcrt.setmode(f.fileno(), os.O_BINARY)
|
|
|
|
msvcrt.setmode(f.fileno(), os.O_TEXT)
|
|
|
|
|
|
|
|
def test_open_osfhandle(self):
|
|
|
|
h = _winapi.CreateFile(TESTFN_ASCII, _winapi.GENERIC_WRITE, 0, 0, 1, 128, 0)
|
|
|
|
self.addCleanup(os_helper.unlink, TESTFN_ASCII)
|
|
|
|
|
|
|
|
try:
|
|
|
|
fd = msvcrt.open_osfhandle(h, os.O_RDONLY)
|
|
|
|
h = None
|
|
|
|
os.close(fd)
|
|
|
|
finally:
|
|
|
|
if h:
|
|
|
|
_winapi.CloseHandle(h)
|
|
|
|
|
|
|
|
def test_get_osfhandle(self):
|
|
|
|
with open(TESTFN, "w") as f:
|
|
|
|
self.addCleanup(os_helper.unlink, TESTFN)
|
|
|
|
|
|
|
|
msvcrt.get_osfhandle(f.fileno())
|
|
|
|
|
|
|
|
|
|
|
|
c = '\u5b57' # unicode CJK char (meaning 'character') for 'wide-char' tests
|
|
|
|
c_encoded = b'\x57\x5b' # utf-16-le (which windows internally used) encoded char for this CJK char
|
|
|
|
|
|
|
|
|
|
|
|
class TestConsoleIO(unittest.TestCase):
|
2023-10-05 19:52:26 +02:00
|
|
|
# CREATE_NEW_CONSOLE creates a "popup" window.
|
|
|
|
@requires_resource('gui')
|
|
|
|
def run_in_separated_process(self, code):
|
2024-09-09 14:58:26 +02:00
|
|
|
# Run test in a separated process to avoid stdin conflicts.
|
2023-10-05 19:52:26 +02:00
|
|
|
# See: gh-110147
|
|
|
|
cmd = [sys.executable, '-c', code]
|
|
|
|
subprocess.run(cmd, check=True, capture_output=True,
|
|
|
|
creationflags=subprocess.CREATE_NEW_CONSOLE)
|
|
|
|
|
2023-09-09 00:38:38 +02:00
|
|
|
def test_kbhit(self):
|
2023-10-05 19:52:26 +02:00
|
|
|
code = dedent('''
|
|
|
|
import msvcrt
|
|
|
|
assert msvcrt.kbhit() == 0
|
|
|
|
''')
|
|
|
|
self.run_in_separated_process(code)
|
2023-09-09 00:38:38 +02:00
|
|
|
|
|
|
|
def test_getch(self):
|
|
|
|
msvcrt.ungetch(b'c')
|
|
|
|
self.assertEqual(msvcrt.getch(), b'c')
|
|
|
|
|
2023-10-05 19:52:26 +02:00
|
|
|
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)
|
2023-09-22 02:19:48 +02:00
|
|
|
|
2023-10-05 19:52:26 +02:00
|
|
|
def test_getwch(self):
|
|
|
|
self.check_getwch('getwch')
|
2023-09-09 00:38:38 +02:00
|
|
|
|
|
|
|
def test_getche(self):
|
|
|
|
msvcrt.ungetch(b'c')
|
|
|
|
self.assertEqual(msvcrt.getche(), b'c')
|
|
|
|
|
|
|
|
def test_getwche(self):
|
2023-10-05 19:52:26 +02:00
|
|
|
self.check_getwch('getwche')
|
2023-09-09 00:38:38 +02:00
|
|
|
|
|
|
|
def test_putch(self):
|
|
|
|
msvcrt.putch(b'c')
|
|
|
|
|
|
|
|
def test_putwch(self):
|
|
|
|
msvcrt.putwch(c)
|
|
|
|
|
|
|
|
|
|
|
|
class TestOther(unittest.TestCase):
|
|
|
|
def test_heap_min(self):
|
|
|
|
try:
|
|
|
|
msvcrt.heapmin()
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|