|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | + |
| 5 | +from test.support import os_helper |
| 6 | +from test.support.os_helper import TESTFN, TESTFN_ASCII |
| 7 | + |
| 8 | +if sys.platform != "win32": |
| 9 | + raise unittest.SkipTest("windows related tests") |
| 10 | + |
| 11 | +import _winapi |
| 12 | +import msvcrt; |
| 13 | + |
| 14 | +from _testconsole import write_input |
| 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 | + def test_kbhit(self): |
| 65 | + self.assertEqual(msvcrt.kbhit(), 0) |
| 66 | + |
| 67 | + def test_getch(self): |
| 68 | + msvcrt.ungetch(b'c') |
| 69 | + self.assertEqual(msvcrt.getch(), b'c') |
| 70 | + |
| 71 | + def test_getwch(self): |
| 72 | + stdin = open('CONIN$', 'r') |
| 73 | + old_stdin = sys.stdin |
| 74 | + try: |
| 75 | + sys.stdin = stdin |
| 76 | + write_input(stdin.buffer.raw, c_encoded) |
| 77 | + self.assertEqual(msvcrt.getwch(), c) |
| 78 | + finally: |
| 79 | + sys.stdin = old_stdin |
| 80 | + |
| 81 | + def test_getche(self): |
| 82 | + msvcrt.ungetch(b'c') |
| 83 | + self.assertEqual(msvcrt.getche(), b'c') |
| 84 | + |
| 85 | + def test_getwche(self): |
| 86 | + stdin = open('CONIN$', 'r') |
| 87 | + old_stdin = sys.stdin |
| 88 | + try: |
| 89 | + sys.stdin = stdin |
| 90 | + write_input(stdin.buffer.raw, c_encoded) |
| 91 | + self.assertEqual(msvcrt.getwche(), c) |
| 92 | + finally: |
| 93 | + sys.stdin = old_stdin |
| 94 | + |
| 95 | + def test_putch(self): |
| 96 | + msvcrt.putch(b'c') |
| 97 | + |
| 98 | + def test_putwch(self): |
| 99 | + msvcrt.putwch(c) |
| 100 | + |
| 101 | + |
| 102 | +class TestOther(unittest.TestCase): |
| 103 | + def test_heap_min(self): |
| 104 | + try: |
| 105 | + msvcrt.heapmin() |
| 106 | + except OSError: |
| 107 | + pass |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + unittest.main() |
0 commit comments