| 1 |
# *********************************************************** |
| 2 |
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ****** |
| 3 |
# ************************************************************* |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import sys |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
|
| 9 |
from ._ki import enable_ki_protection |
| 10 |
from ._run import GLOBAL_RUN_CONTEXT |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from contextlib import AbstractContextManager |
| 14 |
|
| 15 |
from typing_extensions import Buffer |
| 16 |
|
| 17 |
from .._file_io import _HasFileNo |
| 18 |
from ._unbounded_queue import UnboundedQueue |
| 19 |
from ._windows_cffi import CData, Handle |
| 20 |
|
| 21 |
assert not TYPE_CHECKING or sys.platform == "win32" |
| 22 |
|
| 23 |
|
| 24 |
__all__ = [ |
| 25 |
"current_iocp", |
| 26 |
"monitor_completion_key", |
| 27 |
"notify_closing", |
| 28 |
"readinto_overlapped", |
| 29 |
"register_with_iocp", |
| 30 |
"wait_overlapped", |
| 31 |
"wait_readable", |
| 32 |
"wait_writable", |
| 33 |
"write_overlapped", |
| 34 |
] |
| 35 |
|
| 36 |
|
| 37 |
@enable_ki_protection |
| 38 |
async def wait_readable(sock: _HasFileNo | int) -> None: |
| 39 |
"""Block until the kernel reports that the given object is readable. |
| 40 |
|
| 41 |
On Unix systems, ``sock`` must either be an integer file descriptor, |
| 42 |
or else an object with a ``.fileno()`` method which returns an |
| 43 |
integer file descriptor. Any kind of file descriptor can be passed, |
| 44 |
though the exact semantics will depend on your kernel. For example, |
| 45 |
this probably won't do anything useful for on-disk files. |
| 46 |
|
| 47 |
On Windows systems, ``sock`` must either be an integer ``SOCKET`` |
| 48 |
handle, or else an object with a ``.fileno()`` method which returns |
| 49 |
an integer ``SOCKET`` handle. File descriptors aren't supported, |
| 50 |
and neither are handles that refer to anything besides a |
| 51 |
``SOCKET``. |
| 52 |
|
| 53 |
:raises trio.BusyResourceError: |
| 54 |
if another task is already waiting for the given socket to |
| 55 |
become readable. |
| 56 |
:raises trio.ClosedResourceError: |
| 57 |
if another task calls :func:`notify_closing` while this |
| 58 |
function is still working. |
| 59 |
""" |
| 60 |
try: |
| 61 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_readable(sock) |
| 62 |
except AttributeError: |
| 63 |
raise RuntimeError("must be called from async context") from None |
| 64 |
|
| 65 |
|
| 66 |
@enable_ki_protection |
| 67 |
async def wait_writable(sock: _HasFileNo | int) -> None: |
| 68 |
"""Block until the kernel reports that the given object is writable. |
| 69 |
|
| 70 |
See `wait_readable` for the definition of ``sock``. |
| 71 |
|
| 72 |
:raises trio.BusyResourceError: |
| 73 |
if another task is already waiting for the given socket to |
| 74 |
become writable. |
| 75 |
:raises trio.ClosedResourceError: |
| 76 |
if another task calls :func:`notify_closing` while this |
| 77 |
function is still working. |
| 78 |
""" |
| 79 |
try: |
| 80 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_writable(sock) |
| 81 |
except AttributeError: |
| 82 |
raise RuntimeError("must be called from async context") from None |
| 83 |
|
| 84 |
|
| 85 |
@enable_ki_protection |
| 86 |
def notify_closing(handle: Handle | int | _HasFileNo) -> None: |
| 87 |
"""Notify waiters of the given object that it will be closed. |
| 88 |
|
| 89 |
Call this before closing a file descriptor (on Unix) or socket (on |
| 90 |
Windows). This will cause any `wait_readable` or `wait_writable` |
| 91 |
calls on the given object to immediately wake up and raise |
| 92 |
`~trio.ClosedResourceError`. |
| 93 |
|
| 94 |
This doesn't actually close the object – you still have to do that |
| 95 |
yourself afterwards. Also, you want to be careful to make sure no |
| 96 |
new tasks start waiting on the object in between when you call this |
| 97 |
and when it's actually closed. So to close something properly, you |
| 98 |
usually want to do these steps in order: |
| 99 |
|
| 100 |
1. Explicitly mark the object as closed, so that any new attempts |
| 101 |
to use it will abort before they start. |
| 102 |
2. Call `notify_closing` to wake up any already-existing users. |
| 103 |
3. Actually close the object. |
| 104 |
|
| 105 |
It's also possible to do them in a different order if that's more |
| 106 |
convenient, *but only if* you make sure not to have any checkpoints in |
| 107 |
between the steps. This way they all happen in a single atomic |
| 108 |
step, so other tasks won't be able to tell what order they happened |
| 109 |
in anyway. |
| 110 |
""" |
| 111 |
try: |
| 112 |
return GLOBAL_RUN_CONTEXT.runner.io_manager.notify_closing(handle) |
| 113 |
except AttributeError: |
| 114 |
raise RuntimeError("must be called from async context") from None |
| 115 |
|
| 116 |
|
| 117 |
@enable_ki_protection |
| 118 |
def register_with_iocp(handle: int | CData) -> None: |
| 119 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 120 |
anything real. See `#26 |
| 121 |
<https://github.com/python-trio/trio/issues/26>`__ and `#52 |
| 122 |
<https://github.com/python-trio/trio/issues/52>`__. |
| 123 |
""" |
| 124 |
try: |
| 125 |
return GLOBAL_RUN_CONTEXT.runner.io_manager.register_with_iocp(handle) |
| 126 |
except AttributeError: |
| 127 |
raise RuntimeError("must be called from async context") from None |
| 128 |
|
| 129 |
|
| 130 |
@enable_ki_protection |
| 131 |
async def wait_overlapped(handle_: int | CData, lpOverlapped: CData | int) -> object: |
| 132 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 133 |
anything real. See `#26 |
| 134 |
<https://github.com/python-trio/trio/issues/26>`__ and `#52 |
| 135 |
<https://github.com/python-trio/trio/issues/52>`__. |
| 136 |
""" |
| 137 |
try: |
| 138 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_overlapped( |
| 139 |
handle_, lpOverlapped |
| 140 |
) |
| 141 |
except AttributeError: |
| 142 |
raise RuntimeError("must be called from async context") from None |
| 143 |
|
| 144 |
|
| 145 |
@enable_ki_protection |
| 146 |
async def write_overlapped( |
| 147 |
handle: int | CData, data: Buffer, file_offset: int = 0 |
| 148 |
) -> int: |
| 149 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 150 |
anything real. See `#26 |
| 151 |
<https://github.com/python-trio/trio/issues/26>`__ and `#52 |
| 152 |
<https://github.com/python-trio/trio/issues/52>`__. |
| 153 |
""" |
| 154 |
try: |
| 155 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.write_overlapped( |
| 156 |
handle, data, file_offset |
| 157 |
) |
| 158 |
except AttributeError: |
| 159 |
raise RuntimeError("must be called from async context") from None |
| 160 |
|
| 161 |
|
| 162 |
@enable_ki_protection |
| 163 |
async def readinto_overlapped( |
| 164 |
handle: int | CData, buffer: Buffer, file_offset: int = 0 |
| 165 |
) -> int: |
| 166 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 167 |
anything real. See `#26 |
| 168 |
<https://github.com/python-trio/trio/issues/26>`__ and `#52 |
| 169 |
<https://github.com/python-trio/trio/issues/52>`__. |
| 170 |
""" |
| 171 |
try: |
| 172 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.readinto_overlapped( |
| 173 |
handle, buffer, file_offset |
| 174 |
) |
| 175 |
except AttributeError: |
| 176 |
raise RuntimeError("must be called from async context") from None |
| 177 |
|
| 178 |
|
| 179 |
@enable_ki_protection |
| 180 |
def current_iocp() -> int: |
| 181 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 182 |
anything real. See `#26 |
| 183 |
<https://github.com/python-trio/trio/issues/26>`__ and `#52 |
| 184 |
<https://github.com/python-trio/trio/issues/52>`__. |
| 185 |
""" |
| 186 |
try: |
| 187 |
return GLOBAL_RUN_CONTEXT.runner.io_manager.current_iocp() |
| 188 |
except AttributeError: |
| 189 |
raise RuntimeError("must be called from async context") from None |
| 190 |
|
| 191 |
|
| 192 |
@enable_ki_protection |
| 193 |
def monitor_completion_key() -> ( |
| 194 |
AbstractContextManager[tuple[int, UnboundedQueue[object]]] |
| 195 |
): |
| 196 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 197 |
anything real. See `#26 |
| 198 |
<https://github.com/python-trio/trio/issues/26>`__ and `#52 |
| 199 |
<https://github.com/python-trio/trio/issues/52>`__. |
| 200 |
""" |
| 201 |
try: |
| 202 |
return GLOBAL_RUN_CONTEXT.runner.io_manager.monitor_completion_key() |
| 203 |
except AttributeError: |
| 204 |
raise RuntimeError("must be called from async context") from None |