Debian Codegraph @

Lib/test/test_msvcrt.py

raw
1 import os
2 import subprocess
3 import sys
4 import unittest
5 from textwrap import dedent
6
7 from test.support import os_helper, requires_resource
8 from test.support.os_helper import TESTFN, TESTFN_ASCII
9
10 if sys.platform != "win32":
11 raise unittest.SkipTest("windows related tests")
12
13 import _winapi
14 import msvcrt
15
16
17 class TestFileOperations(unittest.TestCase):
18 def test_locking(self):
19 with open(TESTFN, "w") as f:
20 self.addCleanup(os_helper.unlink, TESTFN)
21
22 msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
23 self.assertRaises(OSError, msvcrt.locking, f.fileno(), msvcrt.LK_NBLCK, 1)
24
25 def test_unlockfile(self):
26 with open(TESTFN, "w") as f:
27 self.addCleanup(os_helper.unlink, TESTFN)
28
29 msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
30 msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
31 msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
32
33 def test_setmode(self):
34 with open(TESTFN, "w") as f:
35 self.addCleanup(os_helper.unlink, TESTFN)
36
37 msvcrt.setmode(f.fileno(), os.O_BINARY)
38 msvcrt.setmode(f.fileno(), os.O_TEXT)
39
40 def test_open_osfhandle(self):
41 h = _winapi.CreateFile(TESTFN_ASCII, _winapi.GENERIC_WRITE, 0, 0, 1, 128, 0)
42 self.addCleanup(os_helper.unlink, TESTFN_ASCII)
43
44 try:
45 fd = msvcrt.open_osfhandle(h, os.O_RDONLY)
46 h = None
47 os.close(fd)
48 finally:
49 if h:
50 _winapi.CloseHandle(h)
51
52 def test_get_osfhandle(self):
53 with open(TESTFN, "w") as f:
54 self.addCleanup(os_helper.unlink, TESTFN)
55
56 msvcrt.get_osfhandle(f.fileno())
57
58
59 c = '\u5b57' # unicode CJK char (meaning 'character') for 'wide-char' tests
60 c_encoded = b'\x57\x5b' # utf-16-le (which windows internally used) encoded char for this CJK char
61
62
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 separated 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
73 def test_kbhit(self):
74 code = dedent('''
75 import msvcrt
76 assert msvcrt.kbhit() == 0
77 ''')
78 self.run_in_separated_process(code)
79
80 def test_getch(self):
81 msvcrt.ungetch(b'c')
82 self.assertEqual(msvcrt.getch(), b'c')
83
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)
93
94 def test_getwch(self):
95 self.check_getwch('getwch')
96
97 def test_getche(self):
98 msvcrt.ungetch(b'c')
99 self.assertEqual(msvcrt.getche(), b'c')
100
101 def test_getwche(self):
102 self.check_getwch('getwche')
103
104 def test_putch(self):
105 msvcrt.putch(b'c')
106
107 def test_putwch(self):
108 msvcrt.putwch(c)
109
110
111 class TestOther(unittest.TestCase):
112 def test_heap_min(self):
113 try:
114 msvcrt.heapmin()
115 except OSError:
116 pass
117
118
119 if __name__ == "__main__":
120 unittest.main()