| 1 |
# Test the Windows-only _winapi module |
| 2 |
|
| 3 |
import os |
| 4 |
import pathlib |
| 5 |
import re |
| 6 |
import unittest |
| 7 |
from test.support import import_helper, os_helper |
| 8 |
|
| 9 |
_winapi = import_helper.import_module('_winapi', required_on=['win']) |
| 10 |
|
| 11 |
MAXIMUM_WAIT_OBJECTS = 64 |
| 12 |
MAXIMUM_BATCHED_WAIT_OBJECTS = (MAXIMUM_WAIT_OBJECTS - 1) ** 2 |
| 13 |
|
| 14 |
class WinAPIBatchedWaitForMultipleObjectsTests(unittest.TestCase): |
| 15 |
def _events_waitall_test(self, n): |
| 16 |
evts = [_winapi.CreateEventW(0, False, False, None) for _ in range(n)] |
| 17 |
|
| 18 |
with self.assertRaises(TimeoutError): |
| 19 |
_winapi.BatchedWaitForMultipleObjects(evts, True, 100) |
| 20 |
|
| 21 |
# Ensure no errors raised when all are triggered |
| 22 |
for e in evts: |
| 23 |
_winapi.SetEvent(e) |
| 24 |
try: |
| 25 |
_winapi.BatchedWaitForMultipleObjects(evts, True, 100) |
| 26 |
except TimeoutError: |
| 27 |
self.fail("expected wait to complete immediately") |
| 28 |
|
| 29 |
# Choose 8 events to set, distributed throughout the list, to make sure |
| 30 |
# we don't always have them in the first chunk |
| 31 |
chosen = [i * (len(evts) // 8) for i in range(8)] |
| 32 |
|
| 33 |
# Replace events with invalid handles to make sure we fail |
| 34 |
for i in chosen: |
| 35 |
old_evt = evts[i] |
| 36 |
evts[i] = -1 |
| 37 |
with self.assertRaises(OSError): |
| 38 |
_winapi.BatchedWaitForMultipleObjects(evts, True, 100) |
| 39 |
evts[i] = old_evt |
| 40 |
|
| 41 |
|
| 42 |
def _events_waitany_test(self, n): |
| 43 |
evts = [_winapi.CreateEventW(0, False, False, None) for _ in range(n)] |
| 44 |
|
| 45 |
with self.assertRaises(TimeoutError): |
| 46 |
_winapi.BatchedWaitForMultipleObjects(evts, False, 100) |
| 47 |
|
| 48 |
# Choose 8 events to set, distributed throughout the list, to make sure |
| 49 |
# we don't always have them in the first chunk |
| 50 |
chosen = [i * (len(evts) // 8) for i in range(8)] |
| 51 |
|
| 52 |
# Trigger one by one. They are auto-reset events, so will only trigger once |
| 53 |
for i in chosen: |
| 54 |
with self.subTest(f"trigger event {i} of {len(evts)}"): |
| 55 |
_winapi.SetEvent(evts[i]) |
| 56 |
triggered = _winapi.BatchedWaitForMultipleObjects(evts, False, 10000) |
| 57 |
self.assertSetEqual(set(triggered), {i}) |
| 58 |
|
| 59 |
# Trigger all at once. This may require multiple calls |
| 60 |
for i in chosen: |
| 61 |
_winapi.SetEvent(evts[i]) |
| 62 |
triggered = set() |
| 63 |
while len(triggered) < len(chosen): |
| 64 |
triggered.update(_winapi.BatchedWaitForMultipleObjects(evts, False, 10000)) |
| 65 |
self.assertSetEqual(triggered, set(chosen)) |
| 66 |
|
| 67 |
# Replace events with invalid handles to make sure we fail |
| 68 |
for i in chosen: |
| 69 |
with self.subTest(f"corrupt event {i} of {len(evts)}"): |
| 70 |
old_evt = evts[i] |
| 71 |
evts[i] = -1 |
| 72 |
with self.assertRaises(OSError): |
| 73 |
_winapi.BatchedWaitForMultipleObjects(evts, False, 100) |
| 74 |
evts[i] = old_evt |
| 75 |
|
| 76 |
|
| 77 |
def test_few_events_waitall(self): |
| 78 |
self._events_waitall_test(16) |
| 79 |
|
| 80 |
def test_many_events_waitall(self): |
| 81 |
self._events_waitall_test(256) |
| 82 |
|
| 83 |
def test_max_events_waitall(self): |
| 84 |
self._events_waitall_test(MAXIMUM_BATCHED_WAIT_OBJECTS) |
| 85 |
|
| 86 |
|
| 87 |
def test_few_events_waitany(self): |
| 88 |
self._events_waitany_test(16) |
| 89 |
|
| 90 |
def test_many_events_waitany(self): |
| 91 |
self._events_waitany_test(256) |
| 92 |
|
| 93 |
def test_max_events_waitany(self): |
| 94 |
self._events_waitany_test(MAXIMUM_BATCHED_WAIT_OBJECTS) |
| 95 |
|
| 96 |
|
| 97 |
class WinAPITests(unittest.TestCase): |
| 98 |
def test_getlongpathname(self): |
| 99 |
testfn = pathlib.Path(os.getenv("ProgramFiles")).parents[-1] / "PROGRA~1" |
| 100 |
if not os.path.isdir(testfn): |
| 101 |
raise unittest.SkipTest("require x:\\PROGRA~1 to test") |
| 102 |
|
| 103 |
# pathlib.Path will be rejected - only str is accepted |
| 104 |
with self.assertRaises(TypeError): |
| 105 |
_winapi.GetLongPathName(testfn) |
| 106 |
|
| 107 |
actual = _winapi.GetLongPathName(os.fsdecode(testfn)) |
| 108 |
|
| 109 |
# Can't assume that PROGRA~1 expands to any particular variation, so |
| 110 |
# ensure it matches any one of them. |
| 111 |
candidates = set(testfn.parent.glob("Progra*")) |
| 112 |
self.assertIn(pathlib.Path(actual), candidates) |
| 113 |
|
| 114 |
def test_getshortpathname(self): |
| 115 |
testfn = pathlib.Path(os.getenv("ProgramFiles")) |
| 116 |
if not os.path.isdir(testfn): |
| 117 |
raise unittest.SkipTest("require '%ProgramFiles%' to test") |
| 118 |
|
| 119 |
# pathlib.Path will be rejected - only str is accepted |
| 120 |
with self.assertRaises(TypeError): |
| 121 |
_winapi.GetShortPathName(testfn) |
| 122 |
|
| 123 |
actual = _winapi.GetShortPathName(os.fsdecode(testfn)) |
| 124 |
|
| 125 |
# Should contain "PROGRA~" but we can't predict the number |
| 126 |
self.assertIsNotNone(re.match(r".\:\\PROGRA~\d", actual.upper()), actual) |
| 127 |
|
| 128 |
def test_namedpipe(self): |
| 129 |
pipe_name = rf"\\.\pipe\LOCAL\{os_helper.TESTFN}" |
| 130 |
|
| 131 |
# Pipe does not exist, so this raises |
| 132 |
with self.assertRaises(FileNotFoundError): |
| 133 |
_winapi.WaitNamedPipe(pipe_name, 0) |
| 134 |
|
| 135 |
pipe = _winapi.CreateNamedPipe( |
| 136 |
pipe_name, |
| 137 |
_winapi.PIPE_ACCESS_DUPLEX, |
| 138 |
8, # 8=PIPE_REJECT_REMOTE_CLIENTS |
| 139 |
2, # two instances available |
| 140 |
32, 32, 0, 0) |
| 141 |
self.addCleanup(_winapi.CloseHandle, pipe) |
| 142 |
|
| 143 |
# Pipe instance is available, so this passes |
| 144 |
_winapi.WaitNamedPipe(pipe_name, 0) |
| 145 |
|
| 146 |
with open(pipe_name, 'w+b') as pipe2: |
| 147 |
# No instances available, so this times out |
| 148 |
# (WinError 121 does not get mapped to TimeoutError) |
| 149 |
with self.assertRaises(OSError): |
| 150 |
_winapi.WaitNamedPipe(pipe_name, 0) |
| 151 |
|
| 152 |
_winapi.WriteFile(pipe, b'testdata') |
| 153 |
self.assertEqual(b'testdata', pipe2.read(8)) |
| 154 |
|
| 155 |
self.assertEqual((b'', 0), _winapi.PeekNamedPipe(pipe, 8)[:2]) |
| 156 |
pipe2.write(b'testdata') |
| 157 |
pipe2.flush() |
| 158 |
self.assertEqual((b'testdata', 8), _winapi.PeekNamedPipe(pipe, 8)[:2]) |