66import sys
77from typing import TYPE_CHECKING , ContextManager
88
9- from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
9+ from ._ki import enable_ki_protection
1010from ._run import GLOBAL_RUN_CONTEXT
1111
1212if TYPE_CHECKING :
3232]
3333
3434
35+ @enable_ki_protection
3536async def wait_readable (sock : _HasFileNo | int ) -> None :
3637 """Block until the kernel reports that the given object is readable.
3738
@@ -54,13 +55,13 @@ async def wait_readable(sock: _HasFileNo | int) -> None:
5455 if another task calls :func:`notify_closing` while this
5556 function is still working.
5657 """
57- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
5858 try :
5959 return await GLOBAL_RUN_CONTEXT .runner .io_manager .wait_readable (sock )
6060 except AttributeError :
6161 raise RuntimeError ("must be called from async context" ) from None
6262
6363
64+ @enable_ki_protection
6465async def wait_writable (sock : _HasFileNo | int ) -> None :
6566 """Block until the kernel reports that the given object is writable.
6667
@@ -73,13 +74,13 @@ async def wait_writable(sock: _HasFileNo | int) -> None:
7374 if another task calls :func:`notify_closing` while this
7475 function is still working.
7576 """
76- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
7777 try :
7878 return await GLOBAL_RUN_CONTEXT .runner .io_manager .wait_writable (sock )
7979 except AttributeError :
8080 raise RuntimeError ("must be called from async context" ) from None
8181
8282
83+ @enable_ki_protection
8384def notify_closing (handle : Handle | int | _HasFileNo ) -> None :
8485 """Notify waiters of the given object that it will be closed.
8586
@@ -105,33 +106,32 @@ def notify_closing(handle: Handle | int | _HasFileNo) -> None:
105106 step, so other tasks won't be able to tell what order they happened
106107 in anyway.
107108 """
108- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
109109 try :
110110 return GLOBAL_RUN_CONTEXT .runner .io_manager .notify_closing (handle )
111111 except AttributeError :
112112 raise RuntimeError ("must be called from async context" ) from None
113113
114114
115+ @enable_ki_protection
115116def register_with_iocp (handle : int | CData ) -> None :
116117 """TODO: these are implemented, but are currently more of a sketch than
117118 anything real. See `#26
118119 <https://github.com/python-trio/trio/issues/26>`__ and `#52
119120 <https://github.com/python-trio/trio/issues/52>`__.
120121 """
121- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
122122 try :
123123 return GLOBAL_RUN_CONTEXT .runner .io_manager .register_with_iocp (handle )
124124 except AttributeError :
125125 raise RuntimeError ("must be called from async context" ) from None
126126
127127
128+ @enable_ki_protection
128129async def wait_overlapped (handle_ : int | CData , lpOverlapped : CData | int ) -> object :
129130 """TODO: these are implemented, but are currently more of a sketch than
130131 anything real. See `#26
131132 <https://github.com/python-trio/trio/issues/26>`__ and `#52
132133 <https://github.com/python-trio/trio/issues/52>`__.
133134 """
134- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
135135 try :
136136 return await GLOBAL_RUN_CONTEXT .runner .io_manager .wait_overlapped (
137137 handle_ ,
@@ -141,6 +141,7 @@ async def wait_overlapped(handle_: int | CData, lpOverlapped: CData | int) -> ob
141141 raise RuntimeError ("must be called from async context" ) from None
142142
143143
144+ @enable_ki_protection
144145async def write_overlapped (
145146 handle : int | CData ,
146147 data : Buffer ,
@@ -151,7 +152,6 @@ async def write_overlapped(
151152 <https://github.com/python-trio/trio/issues/26>`__ and `#52
152153 <https://github.com/python-trio/trio/issues/52>`__.
153154 """
154- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
155155 try :
156156 return await GLOBAL_RUN_CONTEXT .runner .io_manager .write_overlapped (
157157 handle ,
@@ -162,6 +162,7 @@ async def write_overlapped(
162162 raise RuntimeError ("must be called from async context" ) from None
163163
164164
165+ @enable_ki_protection
165166async def readinto_overlapped (
166167 handle : int | CData ,
167168 buffer : Buffer ,
@@ -172,7 +173,6 @@ async def readinto_overlapped(
172173 <https://github.com/python-trio/trio/issues/26>`__ and `#52
173174 <https://github.com/python-trio/trio/issues/52>`__.
174175 """
175- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
176176 try :
177177 return await GLOBAL_RUN_CONTEXT .runner .io_manager .readinto_overlapped (
178178 handle ,
@@ -183,26 +183,26 @@ async def readinto_overlapped(
183183 raise RuntimeError ("must be called from async context" ) from None
184184
185185
186+ @enable_ki_protection
186187def current_iocp () -> int :
187188 """TODO: these are implemented, but are currently more of a sketch than
188189 anything real. See `#26
189190 <https://github.com/python-trio/trio/issues/26>`__ and `#52
190191 <https://github.com/python-trio/trio/issues/52>`__.
191192 """
192- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
193193 try :
194194 return GLOBAL_RUN_CONTEXT .runner .io_manager .current_iocp ()
195195 except AttributeError :
196196 raise RuntimeError ("must be called from async context" ) from None
197197
198198
199+ @enable_ki_protection
199200def monitor_completion_key () -> ContextManager [tuple [int , UnboundedQueue [object ]]]:
200201 """TODO: these are implemented, but are currently more of a sketch than
201202 anything real. See `#26
202203 <https://github.com/python-trio/trio/issues/26>`__ and `#52
203204 <https://github.com/python-trio/trio/issues/52>`__.
204205 """
205- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
206206 try :
207207 return GLOBAL_RUN_CONTEXT .runner .io_manager .monitor_completion_key ()
208208 except AttributeError :
0 commit comments