Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing for _wait_for_object.py #2755

Merged
merged 20 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
23 changes: 17 additions & 6 deletions trio/_core/_windows_cffi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from __future__ import annotations

Check warning on line 1 in trio/_core/_windows_cffi.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_windows_cffi.py#L1

Added line #L1 was not covered by tests

import enum
import re
from typing import TYPE_CHECKING

Check warning on line 5 in trio/_core/_windows_cffi.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_windows_cffi.py#L5

Added line #L5 was not covered by tests

if TYPE_CHECKING:
from typing_extensions import NoReturn, TypeAlias

import cffi

Expand Down Expand Up @@ -215,7 +221,8 @@
# being _MSC_VER >= 800)
LIB = re.sub(r"\bPASCAL\b", "__stdcall", LIB)

ffi = cffi.FFI()
ffi = cffi.api.FFI()
CData: TypeAlias = cffi.api.FFI.CData

Check warning on line 225 in trio/_core/_windows_cffi.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_windows_cffi.py#L224-L225

Added lines #L224 - L225 were not covered by tests
ffi.cdef(LIB)

kernel32 = ffi.dlopen("kernel32.dll")
Expand Down Expand Up @@ -302,20 +309,24 @@
################################################################


def _handle(obj):
def _handle(obj: int | CData) -> CData:

Check warning on line 312 in trio/_core/_windows_cffi.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_windows_cffi.py#L312

Added line #L312 was not covered by tests
# For now, represent handles as either cffi HANDLEs or as ints. If you
# try to pass in a file descriptor instead, it's not going to work
# out. (For that msvcrt.get_osfhandle does the trick, but I don't know if
# we'll actually need that for anything...) For sockets this doesn't
# matter, Python never allocates an fd. So let's wait until we actually
# encounter the problem before worrying about it.
if type(obj) is int:
if isinstance(obj, int):
return ffi.cast("HANDLE", obj)
else:
return obj
return obj

Check warning on line 321 in trio/_core/_windows_cffi.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_windows_cffi.py#L321

Added line #L321 was not covered by tests


def raise_winerror(winerror=None, *, filename=None, filename2=None):
def raise_winerror(

Check warning on line 324 in trio/_core/_windows_cffi.py

View check run for this annotation

Codecov / codecov/patch

trio/_core/_windows_cffi.py#L324

Added line #L324 was not covered by tests
winerror: int | None = None,
*,
filename: str | None = None,
filename2: str | None = None,
) -> NoReturn:
if winerror is None:
winerror, msg = ffi.getwinerror()
else:
Expand Down
15 changes: 12 additions & 3 deletions trio/_wait_for_object.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from __future__ import annotations

Check warning on line 1 in trio/_wait_for_object.py

View check run for this annotation

Codecov / codecov/patch

trio/_wait_for_object.py#L1

Added line #L1 was not covered by tests

import math

import trio

from ._core._windows_cffi import ErrorCodes, _handle, ffi, kernel32, raise_winerror
from ._core._windows_cffi import (

Check warning on line 7 in trio/_wait_for_object.py

View check run for this annotation

Codecov / codecov/patch

trio/_wait_for_object.py#L7

Added line #L7 was not covered by tests
CData,
ErrorCodes,
_handle,
ffi,
kernel32,
raise_winerror,
)


async def WaitForSingleObject(obj):
async def WaitForSingleObject(obj: int | CData) -> None:

Check warning on line 17 in trio/_wait_for_object.py

View check run for this annotation

Codecov / codecov/patch

trio/_wait_for_object.py#L17

Added line #L17 was not covered by tests
"""Async and cancellable variant of WaitForSingleObject. Windows only.

Args:
Expand Down Expand Up @@ -45,7 +54,7 @@
kernel32.CloseHandle(cancel_handle)


def WaitForMultipleObjects_sync(*handles):
def WaitForMultipleObjects_sync(*handles: int | CData) -> None:

Check warning on line 57 in trio/_wait_for_object.py

View check run for this annotation

Codecov / codecov/patch

trio/_wait_for_object.py#L57

Added line #L57 was not covered by tests
"""Wait for any of the given Windows handles to be signaled."""
n = len(handles)
handle_arr = ffi.new(f"HANDLE[{n}]")
Expand Down