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 import support
8 from test.support import os_helper, requires_resource
9 from test.support.os_helper import TESTFN, TESTFN_ASCII
10
11 if sys.platform != "win32":
12 raise unittest.SkipTest("windows related tests")
13
14 import _winapi
15 import msvcrt
16
17
18 class TestFileOperations(unittest.TestCase):
19 def test_locking(self):
20 with open(TESTFN, "w") as f:
21 self.addCleanup(os_helper.unlink, TESTFN)
22
23 msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
24 self.assertRaises(OSError, msvcrt.locking, f.fileno(), msvcrt.LK_NBLCK, 1)
25
26 def test_unlockfile(self):
27 with open(TESTFN, "w") as f:
28 self.addCleanup(os_helper.unlink, TESTFN)
29
30 msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
31 msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
32 msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
33
34 def test_setmode(self):
35 with open(TESTFN, "w") as f:
36 self.addCleanup(os_helper.unlink, TESTFN)
37
38 msvcrt.setmode(f.fileno(), os.O_BINARY)
39 msvcrt.setmode(f.fileno(), os.O_TEXT)
40
41 def test_open_osfhandle(self):
42 h = _winapi.CreateFile(TESTFN_ASCII, _winapi.GENERIC_WRITE, 0, 0, 1, 128, 0)
43 self.addCleanup(os_helper.unlink, TESTFN_ASCII)
44
45 try:
46 fd = msvcrt.open_osfhandle(h, os.O_RDONLY)
47 h = None
48 os.close(fd)
49 finally:
50 if h:
51 _winapi.CloseHandle(h)
52
53 def test_get_osfhandle(self):
54 with open(TESTFN, "w") as f:
55 self.addCleanup(os_helper.unlink, TESTFN)
56
57 msvcrt.get_osfhandle(f.fileno())
58
59
60 c = '\u5b57' # unicode CJK char (meaning 'character') for 'wide-char' tests
61 c_encoded = b'\x57\x5b' # utf-16-le (which windows internally used) encoded char for this CJK char
62
63
64 class TestConsoleIO(unittest.TestCase):
65 # CREATE_NEW_CONSOLE creates a "popup" window.
66 @requires_resource('gui')
67 def run_in_separated_process(self, code):
68 # Run test in a separated process to avoid stdin conflicts.
69 # See: gh-110147
70 cmd = [sys.executable, '-c', code]
71 try:
72 subprocess.run(cmd, check=True, capture_output=True,
73 creationflags=subprocess.CREATE_NEW_CONSOLE)
74 except subprocess.CalledProcessError as exc:
75 support.skip_on_low_desktop_heap_memory_subprocess(exc.returncode)
76 raise
77
78 def test_kbhit(self):
79 code = dedent('''
80 import msvcrt
81 assert msvcrt.kbhit() == 0
82 ''')
83 self.run_in_separated_process(code)
84
85 def test_getch(self):
86 msvcrt.ungetch(b'c')
87 self.assertEqual(msvcrt.getch(), b'c')
88
89 def check_getwch(self, funcname):
90 code = dedent(f'''
91 import msvcrt
92 from _testconsole import write_input
93 with open("CONIN$", "rb", buffering=0) as stdin:
94 write_input(stdin, {ascii(c_encoded)})
95 assert msvcrt.{funcname}() == "{c}"
96 ''')
97 self.run_in_separated_process(code)
98
99 def test_getwch(self):
100 self.check_getwch('getwch')
101
102 def test_getche(self):
103 msvcrt.ungetch(b'c')
104 self.assertEqual(msvcrt.getche(), b'c')
105
106 def test_getwche(self):
107 self.check_getwch('getwche')
108
109 def test_putch(self):
110 msvcrt.putch(b'c')
111
112 def test_putwch(self):
113 msvcrt.putwch(c)
114
115
116 class TestOther(unittest.TestCase):
117 def test_heap_min(self):
118 try:
119 msvcrt.heapmin()
120 except OSError:
121 pass
122
123
124 if __name__ == "__main__":
125 unittest.main()