| 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 |
import select |
| 14 |
from collections.abc import Callable |
| 15 |
from contextlib import AbstractContextManager |
| 16 |
|
| 17 |
from .. import _core |
| 18 |
from .._file_io import _HasFileNo |
| 19 |
from ._traps import Abort, RaiseCancelT |
| 20 |
|
| 21 |
assert not TYPE_CHECKING or sys.platform == "darwin" |
| 22 |
|
| 23 |
|
| 24 |
__all__ = [ |
| 25 |
"current_kqueue", |
| 26 |
"monitor_kevent", |
| 27 |
"notify_closing", |
| 28 |
"wait_kevent", |
| 29 |
"wait_readable", |
| 30 |
"wait_writable", |
| 31 |
] |
| 32 |
|
| 33 |
|
| 34 |
@enable_ki_protection |
| 35 |
def current_kqueue() -> select.kqueue: |
| 36 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 37 |
anything real. See `#26 |
| 38 |
<https://github.com/python-trio/trio/issues/26>`__. |
| 39 |
""" |
| 40 |
try: |
| 41 |
return GLOBAL_RUN_CONTEXT.runner.io_manager.current_kqueue() |
| 42 |
except AttributeError: |
| 43 |
raise RuntimeError("must be called from async context") from None |
| 44 |
|
| 45 |
|
| 46 |
@enable_ki_protection |
| 47 |
def monitor_kevent( |
| 48 |
ident: int, filter: int |
| 49 |
) -> AbstractContextManager[_core.UnboundedQueue[select.kevent]]: |
| 50 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 51 |
anything real. See `#26 |
| 52 |
<https://github.com/python-trio/trio/issues/26>`__. |
| 53 |
""" |
| 54 |
try: |
| 55 |
return GLOBAL_RUN_CONTEXT.runner.io_manager.monitor_kevent(ident, filter) |
| 56 |
except AttributeError: |
| 57 |
raise RuntimeError("must be called from async context") from None |
| 58 |
|
| 59 |
|
| 60 |
@enable_ki_protection |
| 61 |
async def wait_kevent( |
| 62 |
ident: int, filter: int, abort_func: Callable[[RaiseCancelT], Abort] |
| 63 |
) -> Abort: |
| 64 |
"""TODO: these are implemented, but are currently more of a sketch than |
| 65 |
anything real. See `#26 |
| 66 |
<https://github.com/python-trio/trio/issues/26>`__. |
| 67 |
""" |
| 68 |
try: |
| 69 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_kevent( |
| 70 |
ident, filter, abort_func |
| 71 |
) |
| 72 |
except AttributeError: |
| 73 |
raise RuntimeError("must be called from async context") from None |
| 74 |
|
| 75 |
|
| 76 |
@enable_ki_protection |
| 77 |
async def wait_readable(fd: int | _HasFileNo) -> None: |
| 78 |
"""Block until the kernel reports that the given object is readable. |
| 79 |
|
| 80 |
On Unix systems, ``fd`` must either be an integer file descriptor, |
| 81 |
or else an object with a ``.fileno()`` method which returns an |
| 82 |
integer file descriptor. Any kind of file descriptor can be passed, |
| 83 |
though the exact semantics will depend on your kernel. For example, |
| 84 |
this probably won't do anything useful for on-disk files. |
| 85 |
|
| 86 |
On Windows systems, ``fd`` must either be an integer ``SOCKET`` |
| 87 |
handle, or else an object with a ``.fileno()`` method which returns |
| 88 |
an integer ``SOCKET`` handle. File descriptors aren't supported, |
| 89 |
and neither are handles that refer to anything besides a |
| 90 |
``SOCKET``. |
| 91 |
|
| 92 |
:raises trio.BusyResourceError: |
| 93 |
if another task is already waiting for the given socket to |
| 94 |
become readable. |
| 95 |
:raises trio.ClosedResourceError: |
| 96 |
if another task calls :func:`notify_closing` while this |
| 97 |
function is still working. |
| 98 |
""" |
| 99 |
try: |
| 100 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_readable(fd) |
| 101 |
except AttributeError: |
| 102 |
raise RuntimeError("must be called from async context") from None |
| 103 |
|
| 104 |
|
| 105 |
@enable_ki_protection |
| 106 |
async def wait_writable(fd: int | _HasFileNo) -> None: |
| 107 |
"""Block until the kernel reports that the given object is writable. |
| 108 |
|
| 109 |
See `wait_readable` for the definition of ``fd``. |
| 110 |
|
| 111 |
:raises trio.BusyResourceError: |
| 112 |
if another task is already waiting for the given socket to |
| 113 |
become writable. |
| 114 |
:raises trio.ClosedResourceError: |
| 115 |
if another task calls :func:`notify_closing` while this |
| 116 |
function is still working. |
| 117 |
""" |
| 118 |
try: |
| 119 |
return await GLOBAL_RUN_CONTEXT.runner.io_manager.wait_writable(fd) |
| 120 |
except AttributeError: |
| 121 |
raise RuntimeError("must be called from async context") from None |
| 122 |
|
| 123 |
|
| 124 |
@enable_ki_protection |
| 125 |
def notify_closing(fd: int | _HasFileNo) -> None: |
| 126 |
"""Notify waiters of the given object that it will be closed. |
| 127 |
|
| 128 |
Call this before closing a file descriptor (on Unix) or socket (on |
| 129 |
Windows). This will cause any `wait_readable` or `wait_writable` |
| 130 |
calls on the given object to immediately wake up and raise |
| 131 |
`~trio.ClosedResourceError`. |
| 132 |
|
| 133 |
This doesn't actually close the object – you still have to do that |
| 134 |
yourself afterwards. Also, you want to be careful to make sure no |
| 135 |
new tasks start waiting on the object in between when you call this |
| 136 |
and when it's actually closed. So to close something properly, you |
| 137 |
usually want to do these steps in order: |
| 138 |
|
| 139 |
1. Explicitly mark the object as closed, so that any new attempts |
| 140 |
to use it will abort before they start. |
| 141 |
2. Call `notify_closing` to wake up any already-existing users. |
| 142 |
3. Actually close the object. |
| 143 |
|
| 144 |
It's also possible to do them in a different order if that's more |
| 145 |
convenient, *but only if* you make sure not to have any checkpoints in |
| 146 |
between the steps. This way they all happen in a single atomic |
| 147 |
step, so other tasks won't be able to tell what order they happened |
| 148 |
in anyway. |
| 149 |
""" |
| 150 |
try: |
| 151 |
return GLOBAL_RUN_CONTEXT.runner.io_manager.notify_closing(fd) |
| 152 |
except AttributeError: |
| 153 |
raise RuntimeError("must be called from async context") from None |