gh-119690: Adds Unicode support for named pipes in _winapi (GH-119717) · python/cpython@78d697b · GitHub
Skip to content

Commit 78d697b

Browse files
authored
gh-119690: Adds Unicode support for named pipes in _winapi (GH-119717)
1 parent 34f9b3e commit 78d697b

6 files changed

Lines changed: 92 additions & 29 deletions

File tree

Lib/test/audit-tests.py

Lines changed: 11 additions & 0 deletions

Lib/test/test_audit.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,20 @@ def test_sys_monitoring_register_callback(self):
291291

292292
self.assertEqual(actual, expected)
293293

294+
def test_winapi_createnamedpipe(self):
295+
winapi = import_helper.import_module("_winapi")
296+
297+
pipe_name = r"\\.\pipe\LOCAL\test_winapi_createnamed_pipe"
298+
returncode, events, stderr = self.run_python("test_winapi_createnamedpipe", pipe_name)
299+
if returncode:
300+
self.fail(stderr)
301+
302+
if support.verbose:
303+
print(*events, sep='\n')
304+
actual = [(ev[0], ev[2]) for ev in events]
305+
expected = [("_winapi.CreateNamedPipe", f"({pipe_name!r}, 3, 8)")]
306+
307+
self.assertEqual(actual, expected)
294308

295309
if __name__ == "__main__":
296310
unittest.main()

Lib/test/test_winapi.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import threading
88
import time
99
import unittest
10-
from test.support import import_helper
10+
from test.support import import_helper, os_helper
1111

1212
_winapi = import_helper.import_module('_winapi', required_on=['win'])
1313

@@ -127,3 +127,35 @@ def test_getshortpathname(self):
127127

128128
# Should contain "PROGRA~" but we can't predict the number
129129
self.assertIsNotNone(re.match(r".\:\\PROGRA~\d", actual.upper()), actual)
130+
131+
def test_namedpipe(self):
132+
pipe_name = rf"\\.\pipe\LOCAL\{os_helper.TESTFN}"
133+
134+
# Pipe does not exist, so this raises
135+
with self.assertRaises(FileNotFoundError):
136+
_winapi.WaitNamedPipe(pipe_name, 0)
137+
138+
pipe = _winapi.CreateNamedPipe(
139+
pipe_name,
140+
_winapi.PIPE_ACCESS_DUPLEX,
141+
8, # 8=PIPE_REJECT_REMOTE_CLIENTS
142+
2, # two instances available
143+
32, 32, 0, 0)
144+
self.addCleanup(_winapi.CloseHandle, pipe)
145+
146+
# Pipe instance is available, so this passes
147+
_winapi.WaitNamedPipe(pipe_name, 0)
148+
149+
with open(pipe_name, 'w+b') as pipe2:
150+
# No instances available, so this times out
151+
# (WinError 121 does not get mapped to TimeoutError)
152+
with self.assertRaises(OSError):
153+
_winapi.WaitNamedPipe(pipe_name, 0)
154+
155+
_winapi.WriteFile(pipe, b'testdata')
156+
self.assertEqual(b'testdata', pipe2.read(8))
157+
158+
self.assertEqual((b'', 0), _winapi.PeekNamedPipe(pipe, 8)[:2])
159+
pipe2.write(b'testdata')
160+
pipe2.flush()
161+
self.assertEqual((b'testdata', 8), _winapi.PeekNamedPipe(pipe, 8)[:2])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adds Unicode support and fixes audit events for ``_winapi.CreateNamedPipe``.

Modules/_winapi.c

Lines changed: 18 additions & 19 deletions

Modules/clinic/_winapi.c.h

Lines changed: 15 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)