6
6
import sys
7
7
from typing import TYPE_CHECKING , ContextManager
8
8
9
- from ._ki import LOCALS_KEY_KI_PROTECTION_ENABLED
9
+ from ._ki import enable_ki_protection
10
10
from ._run import GLOBAL_RUN_CONTEXT
11
11
12
12
if TYPE_CHECKING :
32
32
]
33
33
34
34
35
+ @enable_ki_protection
35
36
async def wait_readable (sock : _HasFileNo | int ) -> None :
36
37
"""Block until the kernel reports that the given object is readable.
37
38
@@ -54,13 +55,13 @@ async def wait_readable(sock: _HasFileNo | int) -> None:
54
55
if another task calls :func:`notify_closing` while this
55
56
function is still working.
56
57
"""
57
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
58
58
try :
59
59
return await GLOBAL_RUN_CONTEXT .runner .io_manager .wait_readable (sock )
60
60
except AttributeError :
61
61
raise RuntimeError ("must be called from async context" ) from None
62
62
63
63
64
+ @enable_ki_protection
64
65
async def wait_writable (sock : _HasFileNo | int ) -> None :
65
66
"""Block until the kernel reports that the given object is writable.
66
67
@@ -73,13 +74,13 @@ async def wait_writable(sock: _HasFileNo | int) -> None:
73
74
if another task calls :func:`notify_closing` while this
74
75
function is still working.
75
76
"""
76
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
77
77
try :
78
78
return await GLOBAL_RUN_CONTEXT .runner .io_manager .wait_writable (sock )
79
79
except AttributeError :
80
80
raise RuntimeError ("must be called from async context" ) from None
81
81
82
82
83
+ @enable_ki_protection
83
84
def notify_closing (handle : Handle | int | _HasFileNo ) -> None :
84
85
"""Notify waiters of the given object that it will be closed.
85
86
@@ -105,33 +106,32 @@ def notify_closing(handle: Handle | int | _HasFileNo) -> None:
105
106
step, so other tasks won't be able to tell what order they happened
106
107
in anyway.
107
108
"""
108
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
109
109
try :
110
110
return GLOBAL_RUN_CONTEXT .runner .io_manager .notify_closing (handle )
111
111
except AttributeError :
112
112
raise RuntimeError ("must be called from async context" ) from None
113
113
114
114
115
+ @enable_ki_protection
115
116
def register_with_iocp (handle : int | CData ) -> None :
116
117
"""TODO: these are implemented, but are currently more of a sketch than
117
118
anything real. See `#26
118
119
<https://github.com/python-trio/trio/issues/26>`__ and `#52
119
120
<https://github.com/python-trio/trio/issues/52>`__.
120
121
"""
121
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
122
122
try :
123
123
return GLOBAL_RUN_CONTEXT .runner .io_manager .register_with_iocp (handle )
124
124
except AttributeError :
125
125
raise RuntimeError ("must be called from async context" ) from None
126
126
127
127
128
+ @enable_ki_protection
128
129
async def wait_overlapped (handle_ : int | CData , lpOverlapped : CData | int ) -> object :
129
130
"""TODO: these are implemented, but are currently more of a sketch than
130
131
anything real. See `#26
131
132
<https://github.com/python-trio/trio/issues/26>`__ and `#52
132
133
<https://github.com/python-trio/trio/issues/52>`__.
133
134
"""
134
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
135
135
try :
136
136
return await GLOBAL_RUN_CONTEXT .runner .io_manager .wait_overlapped (
137
137
handle_ ,
@@ -141,6 +141,7 @@ async def wait_overlapped(handle_: int | CData, lpOverlapped: CData | int) -> ob
141
141
raise RuntimeError ("must be called from async context" ) from None
142
142
143
143
144
+ @enable_ki_protection
144
145
async def write_overlapped (
145
146
handle : int | CData ,
146
147
data : Buffer ,
@@ -151,7 +152,6 @@ async def write_overlapped(
151
152
<https://github.com/python-trio/trio/issues/26>`__ and `#52
152
153
<https://github.com/python-trio/trio/issues/52>`__.
153
154
"""
154
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
155
155
try :
156
156
return await GLOBAL_RUN_CONTEXT .runner .io_manager .write_overlapped (
157
157
handle ,
@@ -162,6 +162,7 @@ async def write_overlapped(
162
162
raise RuntimeError ("must be called from async context" ) from None
163
163
164
164
165
+ @enable_ki_protection
165
166
async def readinto_overlapped (
166
167
handle : int | CData ,
167
168
buffer : Buffer ,
@@ -172,7 +173,6 @@ async def readinto_overlapped(
172
173
<https://github.com/python-trio/trio/issues/26>`__ and `#52
173
174
<https://github.com/python-trio/trio/issues/52>`__.
174
175
"""
175
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
176
176
try :
177
177
return await GLOBAL_RUN_CONTEXT .runner .io_manager .readinto_overlapped (
178
178
handle ,
@@ -183,26 +183,26 @@ async def readinto_overlapped(
183
183
raise RuntimeError ("must be called from async context" ) from None
184
184
185
185
186
+ @enable_ki_protection
186
187
def current_iocp () -> int :
187
188
"""TODO: these are implemented, but are currently more of a sketch than
188
189
anything real. See `#26
189
190
<https://github.com/python-trio/trio/issues/26>`__ and `#52
190
191
<https://github.com/python-trio/trio/issues/52>`__.
191
192
"""
192
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
193
193
try :
194
194
return GLOBAL_RUN_CONTEXT .runner .io_manager .current_iocp ()
195
195
except AttributeError :
196
196
raise RuntimeError ("must be called from async context" ) from None
197
197
198
198
199
+ @enable_ki_protection
199
200
def monitor_completion_key () -> ContextManager [tuple [int , UnboundedQueue [object ]]]:
200
201
"""TODO: these are implemented, but are currently more of a sketch than
201
202
anything real. See `#26
202
203
<https://github.com/python-trio/trio/issues/26>`__ and `#52
203
204
<https://github.com/python-trio/trio/issues/52>`__.
204
205
"""
205
- sys ._getframe ().f_locals [LOCALS_KEY_KI_PROTECTION_ENABLED ] = True
206
206
try :
207
207
return GLOBAL_RUN_CONTEXT .runner .io_manager .monitor_completion_key ()
208
208
except AttributeError :
0 commit comments