Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def test_windows_libpython_is_not_exposed():
assert "LIBPYTHON" not in sysconfig.get_config_vars()


@unittest.skipUnless(sys.platform == "win32", "Windows-specific sysconfig behavior")
def test_windows_hash_algorithm():
import sysconfig
assert sysconfig.get_config_var("Py_HASH_ALGORITHM") == 0


@unittest.skipUnless(sys.platform == "win32", "Windows-specific sysconfig behavior")
def test_windows_library_names():
import sysconfig
Expand Down
152 changes: 152 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_winapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
#
# Subject to the condition set forth below, permission is hereby granted to any
# person obtaining a copy of this software, associated documentation and/or
# data (collectively the "Software"), free of charge and under any and all
# copyright rights in the Software, and any and all patent rights owned or
# freely licensable by each licensor hereunder covering either (i) the
# unmodified Software as contributed to or provided by such licensor, or (ii)
# the Larger Works (as defined below), to deal in both
#
# (a) the Software, and
#
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
# one is included with the Software each a "Larger Work" to which the Software
# is contributed by such licensors),
#
# without restriction, including without limitation the rights to copy, create
# derivative works of, display, perform, and distribute the Software and make,
# use, sell, offer for sale, import, export, have made, and have sold the
# Software and the Larger Work(s), and to sublicense the foregoing rights on
# either these or other terms.
#
# This license is subject to the following condition:
#
# The above copyright notice and either this complete permission notice or at a
# minimum a reference to the UPL must be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys
import unittest


NATIVE_WINDOWS = (
sys.platform == "win32"
and (sys.implementation.name != "graalpy" or __graalpython__.posix_module_backend() == "native")
)


@unittest.skipUnless(NATIVE_WINDOWS, "requires native Windows pipes")
class WinapiPipeTests(unittest.TestCase):
def setUp(self):
import _winapi
import multiprocessing

self.api = _winapi
self.reader, self.writer = multiprocessing.Pipe(duplex=False)
self.addCleanup(self.reader.close)
self.addCleanup(self.writer.close)

def test_peek_named_pipe(self):
peek = self.api.PeekNamedPipe
handle = self.reader.fileno()
self.assertEqual(peek(handle), (0, 0))
self.assertEqual(peek(handle, 0), (0, 0))
self.assertEqual(peek(handle, 10), (b"", 0, 0))
self.writer.send_bytes(b"a" * 200)
self.writer.send_bytes(b"b" * 20)
self.assertEqual(peek(handle), (220, 200))
self.assertEqual(peek(handle, 0), (220, 200))
self.assertEqual(peek(handle, 10), (b"a" * 10, 220, 190))

def check_partial_read(self, pending):
api = self.api
handle = self.reader.fileno()
payload = bytes(range(200))
if not pending:
self.writer.send_bytes(payload)
ov, err = api.ReadFile(handle, 128, overlapped=True)
try:
if pending:
self.assertEqual(err, api.ERROR_IO_PENDING)
self.writer.send_bytes(payload)
self.assertEqual(api.WaitForSingleObject(ov.event, 5000), api.WAIT_OBJECT_0)
else:
self.assertEqual(err, api.ERROR_MORE_DATA)
self.assertEqual(ov.GetOverlappedResult(False), (128, api.ERROR_MORE_DATA))
self.assertEqual(ov.getbuffer(), payload[:128])
self.assertEqual(api.PeekNamedPipe(handle), (72, 72))
tail, err = api.ReadFile(handle, 72, overlapped=True)
self.assertEqual(tail.GetOverlappedResult(True), (72, 0))
self.assertEqual(tail.getbuffer(), payload[128:])
finally:
ov.cancel()
ov.GetOverlappedResult(True)

def test_immediate_partial_read(self):
self.check_partial_read(pending=False)

def test_pending_partial_read(self):
self.check_partial_read(pending=True)

def test_message_boundaries(self):
# Queue multiple messages so total available differs from the bytes
# remaining in the first message after the initial 128-byte read.
messages = [bytes(range(256)) * 2, b"next message", b"", b"last"]
for _ in range(10):
self.assertFalse(self.reader.poll(0))
for message in messages:
self.writer.send_bytes(message)
for message in messages:
self.assertTrue(self.reader.poll(0))
self.assertEqual(self.reader.recv_bytes(len(message)), message)
self.assertFalse(self.reader.poll(0))

def check_connect_named_pipe(self, client_first):
import os
import time
from multiprocessing.connection import PipeClient, PipeListener

address = rf"\\.\pipe\graalpy-connect-{os.getpid()}-{time.time_ns()}"
listener = PipeListener(address)
self.addCleanup(listener.close)
if client_first:
client = PipeClient(address)
self.addCleanup(client.close)
ov = self.api.ConnectNamedPipe(listener._handle_queue[0], overlapped=True)
try:
if not client_first:
client = PipeClient(address)
self.addCleanup(client.close)
# PipeListener.accept waits on this event even when the client
# connected before ConnectNamedPipe (ERROR_PIPE_CONNECTED).
self.assertEqual(self.api.WaitForSingleObject(ov.event, 5000), self.api.WAIT_OBJECT_0)
self.assertEqual(ov.GetOverlappedResult(True), (0, 0))
finally:
ov.cancel()
ov.GetOverlappedResult(True)

def test_connect_named_pipe_client_first(self):
self.check_connect_named_pipe(client_first=True)

def test_connect_named_pipe_pending(self):
self.check_connect_named_pipe(client_first=False)

def test_pickled_messages(self):
for _ in range(10):
messages = [list(range(200)), {"payload": "x" * 512}]
for message in messages:
self.writer.send(message)
for message in messages:
self.assertEqual(self.reader.recv(), message)
3 changes: 3 additions & 0 deletions graalpython/lib-graalpython/_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def _update_posix_vars(config_vars=None):
get_toolchain = __graalpython__.determine_system_toolchain().get

if win32_native:
# Windows no longer loads _sysconfigdata, so provide the public hash
# algorithm configuration variable here instead of losing it.
_setdefault(config_vars, 'Py_HASH_ALGORITHM', 0)
python_inc = os.path.join(os.path.normpath(sys.base_prefix), 'Include')
else:
python_inc = os.path.join(
Expand Down
14 changes: 11 additions & 3 deletions graalpython/lib-graalpython/modules/_winapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def _native():
kernel32.GetOverlappedResult.restype = wintypes.BOOL
kernel32.CreateEventW.argtypes = [wintypes.LPVOID, wintypes.BOOL, wintypes.BOOL, wintypes.LPCWSTR]
kernel32.CreateEventW.restype = wintypes.HANDLE
kernel32.SetEvent.argtypes = [wintypes.HANDLE]
kernel32.SetEvent.restype = wintypes.BOOL
kernel32.GetLongPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
kernel32.GetLongPathNameW.restype = wintypes.DWORD
kernel32.GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
Expand Down Expand Up @@ -881,6 +883,9 @@ def ConnectNamedPipe(handle, overlapped=False):
ov.pending = True
return ov
if code == ERROR_PIPE_CONNECTED:
# A client connected before this call, so Windows does not signal
# the event. Callers still wait on it for connection completion.
_raise_if_zero(kernel32.SetEvent(wintypes.HANDLE(ov.event)))
ov._completed_result = (0, 0)
return ov
raise _winerror(code)
Expand Down Expand Up @@ -926,8 +931,9 @@ def ReadFile(handle, size, overlapped=False):
ov.pending = True
return ov, code
if code == ERROR_MORE_DATA:
ov._completed_result = (transferred.value, code)
ov._last_result = ov._completed_result
# lpNumberOfBytesRead may be zero for an overlapped partial read,
# even though data was consumed. GetOverlappedResult supplies the
# actual byte count from the OVERLAPPED structure.
return ov, code
raise _winerror(code)

Expand Down Expand Up @@ -1000,7 +1006,9 @@ def PeekNamedPipe(handle, size=0):
ctypes.byref(bytes_left),
)
)
return (buffer.raw[: bytes_read.value] if buffer is not None else b""), total_available.value, bytes_left.value
if buffer is None:
return total_available.value, bytes_left.value
return buffer.raw[: bytes_read.value], total_available.value, bytes_left.value


def SetNamedPipeHandleState(named_pipe, mode, max_collection_count, collect_data_timeout):
Expand Down
3 changes: 3 additions & 0 deletions graalpython/lib-python/3/sysconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ def _init_non_posix(vars):

# Add EXT_SUFFIX, SOABI, and Py_GIL_DISABLED
vars.update(_sysconfig.config_vars())
# Windows does not load _sysconfigdata; retain the hash algorithm value
# required by sys.hash_info compatibility checks.
vars.setdefault('Py_HASH_ALGORITHM', 0)

vars['LIBDIR'] = _safe_realpath(os.path.join(get_config_var('installed_base'), 'libs'))
# GraalPy change: expose the stable public DLL name. sys.dllhandle refers to
Expand Down
Loading