noio 0.2__tar.gz → 0.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {noio-0.2 → noio-0.3}/CHANGELOG.md +16 -0
- {noio-0.2 → noio-0.3}/PKG-INFO +1 -1
- {noio-0.2 → noio-0.3}/noio/_contextlib.py +17 -14
- {noio-0.2 → noio-0.3}/noio/_contextlib_test.py +33 -0
- {noio-0.2 → noio-0.3}/noio/_ring.py +11 -9
- {noio-0.2 → noio-0.3}/noio/_ring_test.py +19 -0
- {noio-0.2 → noio-0.3}/noio/conftest.py +22 -0
- {noio-0.2 → noio-0.3}/noio/event.py +9 -7
- {noio-0.2 → noio-0.3}/noio/event_test.py +10 -0
- {noio-0.2 → noio-0.3}/noio/queue.py +17 -14
- {noio-0.2 → noio-0.3}/noio/queue_test.py +13 -0
- {noio-0.2 → noio-0.3}/noio/routine.py +43 -17
- {noio-0.2 → noio-0.3}/noio/routine_test.py +56 -0
- {noio-0.2 → noio-0.3}/noio/selectable.py +8 -1
- {noio-0.2 → noio-0.3}/pyproject.toml +1 -1
- {noio-0.2 → noio-0.3}/.github/workflows/build.yml +0 -0
- {noio-0.2 → noio-0.3}/.github/workflows/publish.yml +0 -0
- {noio-0.2 → noio-0.3}/.gitignore +0 -0
- {noio-0.2 → noio-0.3}/AGENTS.md +0 -0
- {noio-0.2 → noio-0.3}/CONTRIBUTING.md +0 -0
- {noio-0.2 → noio-0.3}/LICENSE +0 -0
- {noio-0.2 → noio-0.3}/README.md +0 -0
- {noio-0.2 → noio-0.3}/noio/__init__.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_cancel.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_cancel_scope.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_cancel_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_event.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_event_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_lock.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_lock_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_runtime.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_runtime_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_selector.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_selector_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_shield.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_shield_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_task_group.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_task_group_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_timer.py +0 -0
- {noio-0.2 → noio-0.3}/noio/_timer_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/cancel.md +0 -0
- {noio-0.2 → noio-0.3}/noio/cancel.py +0 -0
- {noio-0.2 → noio-0.3}/noio/cancel_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/contextlib.md +0 -0
- {noio-0.2 → noio-0.3}/noio/event.md +0 -0
- {noio-0.2 → noio-0.3}/noio/gevent_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/py.typed +0 -0
- {noio-0.2 → noio-0.3}/noio/queue.md +0 -0
- {noio-0.2 → noio-0.3}/noio/routine.md +0 -0
- {noio-0.2 → noio-0.3}/noio/select.md +0 -0
- {noio-0.2 → noio-0.3}/noio/select.py +0 -0
- {noio-0.2 → noio-0.3}/noio/select_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/selectable.md +0 -0
- {noio-0.2 → noio-0.3}/noio/selectable_test.py +0 -0
- {noio-0.2 → noio-0.3}/noio/sleep.md +0 -0
- {noio-0.2 → noio-0.3}/noio/sleep.py +0 -0
- {noio-0.2 → noio-0.3}/noio/sleep_test.py +0 -0
- {noio-0.2 → noio-0.3}/pyrightconfig.json +0 -0
|
@@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
|
|
6
6
|
The format is loosely based on [Keep a Changelog](https://keepachangelog.com).
|
|
7
7
|
|
|
8
|
+
[0.3] - 2026-11-03
|
|
9
|
+
-----------------
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- Accessing routine, selectable, and context manager methods is much cheaper,
|
|
14
|
+
roughly halving the cost of queue operations and selection.
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- Queue, Event, and selection operations no longer stall every thread
|
|
19
|
+
for milliseconds at a time on free-threaded CPython 3.15.
|
|
20
|
+
Each operation used to create decorated functions,
|
|
21
|
+
and renaming a function there pauses all threads in the process.
|
|
22
|
+
|
|
8
23
|
[0.2] - 2026-11-02
|
|
9
24
|
-----------------
|
|
10
25
|
|
|
@@ -68,5 +83,6 @@ Requires Python 3.15 or later.
|
|
|
68
83
|
from a single coroutine implementation.
|
|
69
84
|
- A guard against ordinary routine calls inside routine implementations.
|
|
70
85
|
|
|
86
|
+
[0.3]: https://github.com/ryanhiebert/noio/releases/tag/0.3
|
|
71
87
|
[0.2]: https://github.com/ryanhiebert/noio/releases/tag/0.2
|
|
72
88
|
[0.1]: https://github.com/ryanhiebert/noio/releases/tag/0.1
|
{noio-0.2 → noio-0.3}/PKG-INFO
RENAMED
|
@@ -20,10 +20,11 @@ from typing import cast
|
|
|
20
20
|
from typing import overload
|
|
21
21
|
|
|
22
22
|
from .routine import Routine
|
|
23
|
+
from .routine import _cleared_call
|
|
23
24
|
from .routine import _CompatibleFunctionDescriptor
|
|
24
25
|
from .routine import _Mode
|
|
25
26
|
from .routine import _mode
|
|
26
|
-
from .routine import
|
|
27
|
+
from .routine import _shallow_copy
|
|
27
28
|
from .routine import routine
|
|
28
29
|
|
|
29
30
|
|
|
@@ -94,9 +95,9 @@ class _GeneratorContextManager[T](ContextManager[T]):
|
|
|
94
95
|
if self.__manager is not None:
|
|
95
96
|
raise RuntimeError("This context manager has already been entered")
|
|
96
97
|
if _mode.get() is _Mode.DRIVEN and self.__ordinary_factory is not None:
|
|
97
|
-
manager =
|
|
98
|
+
manager = _cleared_call(self.__ordinary_factory)
|
|
98
99
|
self.__manager = manager
|
|
99
|
-
return
|
|
100
|
+
return _cleared_call(manager.__enter__)
|
|
100
101
|
manager = self.__factory()
|
|
101
102
|
self.__manager = manager
|
|
102
103
|
return await manager.__aenter__()
|
|
@@ -114,7 +115,7 @@ class _GeneratorContextManager[T](ContextManager[T]):
|
|
|
114
115
|
# Pair exit with the implementation chosen at entry, even if the
|
|
115
116
|
# calling mode has changed in the meantime.
|
|
116
117
|
if isinstance(manager, AbstractContextManager):
|
|
117
|
-
return
|
|
118
|
+
return _cleared_call(manager.__exit__, exc_type, exc_value, traceback)
|
|
118
119
|
return await manager.__aexit__(exc_type, exc_value, traceback)
|
|
119
120
|
|
|
120
121
|
|
|
@@ -180,18 +181,20 @@ class _ContextManagerFactory[**P, T]:
|
|
|
180
181
|
|
|
181
182
|
def __get__(self, instance: Any, owner: type[Any] | None = None) -> Any:
|
|
182
183
|
if self.__class_method is not None:
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
)
|
|
186
|
-
if self.__ordinary is not None:
|
|
187
|
-
cls = owner if owner is not None else type(instance)
|
|
188
|
-
result.__ordinary = MethodType(self.__ordinary, cls)
|
|
189
|
-
return result
|
|
190
|
-
if instance is None or self.__static_method:
|
|
184
|
+
target = owner if owner is not None else type(instance)
|
|
185
|
+
elif instance is None or self.__static_method:
|
|
191
186
|
return self
|
|
192
|
-
|
|
187
|
+
else:
|
|
188
|
+
target = instance
|
|
189
|
+
# Bind the factory created at decoration time. Creating one per access
|
|
190
|
+
# would modify new function objects, which stops the world on
|
|
191
|
+
# free-threaded Python.
|
|
192
|
+
result = _shallow_copy(self)
|
|
193
|
+
result.__class_method = None
|
|
194
|
+
result.__wrapped__ = MethodType(self.__wrapped__, target)
|
|
195
|
+
result.__factory = MethodType(self.__factory, target)
|
|
193
196
|
if self.__ordinary is not None:
|
|
194
|
-
result.__ordinary = MethodType(self.__ordinary,
|
|
197
|
+
result.__ordinary = MethodType(self.__ordinary, target)
|
|
195
198
|
return result
|
|
196
199
|
|
|
197
200
|
|
|
@@ -732,3 +732,36 @@ configured(1, unknown=True) # reject
|
|
|
732
732
|
result.stdout
|
|
733
733
|
)
|
|
734
734
|
assert all(item["severity"] == "error" for item in diagnostics), result.stdout
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
def test_method_binding_does_not_decorate_functions(
|
|
738
|
+
function_metadata_writes: list[str], async_runtime: AsyncRuntime
|
|
739
|
+
) -> None:
|
|
740
|
+
class Resource:
|
|
741
|
+
@contextmanager
|
|
742
|
+
async def open(self) -> AsyncIterator[int]:
|
|
743
|
+
yield 1
|
|
744
|
+
|
|
745
|
+
@classmethod
|
|
746
|
+
@contextmanager
|
|
747
|
+
async def named(cls) -> AsyncIterator[str]:
|
|
748
|
+
yield cls.__name__
|
|
749
|
+
|
|
750
|
+
resource = Resource()
|
|
751
|
+
|
|
752
|
+
async def main() -> list[str]:
|
|
753
|
+
# Runtimes may decorate main itself: only record work inside it.
|
|
754
|
+
function_metadata_writes.clear()
|
|
755
|
+
async with resource.open() as value:
|
|
756
|
+
assert value == 1
|
|
757
|
+
async with Resource.named() as name:
|
|
758
|
+
assert name == "Resource"
|
|
759
|
+
return list(function_metadata_writes)
|
|
760
|
+
|
|
761
|
+
assert async_runtime.run(main) == []
|
|
762
|
+
function_metadata_writes.clear()
|
|
763
|
+
with resource.open() as value:
|
|
764
|
+
assert value == 1
|
|
765
|
+
with Resource.named() as name:
|
|
766
|
+
assert name == "Resource"
|
|
767
|
+
assert function_metadata_writes == []
|
|
@@ -73,19 +73,21 @@ class Ring[T]:
|
|
|
73
73
|
for guard in guards:
|
|
74
74
|
del self._waiters[guard]
|
|
75
75
|
|
|
76
|
+
# Withdrawal must finish even if cancellation interrupted matching
|
|
77
|
+
# or the subsequent wait. Registration itself remains cancellable.
|
|
78
|
+
# Decorate once: shielding wraps functions, and modifying function
|
|
79
|
+
# metadata per call stops the world on free-threaded Python.
|
|
80
|
+
@shield
|
|
81
|
+
@routine
|
|
82
|
+
async def _withdraw(self, guard: SelectorGuard[T]) -> None:
|
|
83
|
+
async with self._lock:
|
|
84
|
+
self._waiters.pop(guard, None)
|
|
85
|
+
|
|
76
86
|
@selectable
|
|
77
87
|
@contextmanager
|
|
78
88
|
async def exchange(self, guard: SelectorGuard[T], value: T) -> AsyncIterator[None]:
|
|
79
89
|
"""Contribute a value and receive the preceding group member's value."""
|
|
80
90
|
|
|
81
|
-
@shield
|
|
82
|
-
@routine
|
|
83
|
-
async def cleanup() -> None:
|
|
84
|
-
# Withdrawal must finish even if cancellation interrupted matching
|
|
85
|
-
# or the subsequent wait. Registration itself remains cancellable.
|
|
86
|
-
async with self._lock:
|
|
87
|
-
self._waiters.pop(guard, None)
|
|
88
|
-
|
|
89
91
|
# Cover partial registration too: matching can be interrupted while
|
|
90
92
|
# acquiring selection locks after the contribution has been inserted.
|
|
91
93
|
# An unsuccessful context entry never gets an exit from its caller,
|
|
@@ -96,4 +98,4 @@ class Ring[T]:
|
|
|
96
98
|
await self._match.a()
|
|
97
99
|
yield
|
|
98
100
|
finally:
|
|
99
|
-
await
|
|
101
|
+
await self._withdraw.a(guard)
|
|
@@ -248,3 +248,22 @@ def test_cancellation_withdraws_waiting_contribution(backend: str) -> None:
|
|
|
248
248
|
assert not ring._waiters
|
|
249
249
|
|
|
250
250
|
anyio.run(main, backend=backend)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def test_exchange_does_not_decorate_functions(
|
|
254
|
+
function_metadata_writes: list[str],
|
|
255
|
+
) -> None:
|
|
256
|
+
ring = Ring[str](2)
|
|
257
|
+
states = [_Selection[str](), _Selection[str]()]
|
|
258
|
+
registrations = [
|
|
259
|
+
ring.exchange.select(value).__select__(
|
|
260
|
+
SelectorGuard(_Selector(state, 0), state._lock)
|
|
261
|
+
)
|
|
262
|
+
for value, state in zip("AB", states, strict=True)
|
|
263
|
+
]
|
|
264
|
+
for registration in registrations:
|
|
265
|
+
registration.__enter__()
|
|
266
|
+
assert [state.wait() for state in states] == [(0, "B"), (0, "A")]
|
|
267
|
+
for registration in reversed(registrations):
|
|
268
|
+
registration.__exit__(None, None, None)
|
|
269
|
+
assert function_metadata_writes == []
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import functools
|
|
1
2
|
import subprocess
|
|
2
3
|
import sys
|
|
3
4
|
import sysconfig
|
|
@@ -8,6 +9,7 @@ from concurrent.futures import ThreadPoolExecutor
|
|
|
8
9
|
from dataclasses import dataclass
|
|
9
10
|
from functools import partial
|
|
10
11
|
from time import sleep
|
|
12
|
+
from types import FunctionType
|
|
11
13
|
from typing import Any
|
|
12
14
|
from typing import Protocol
|
|
13
15
|
|
|
@@ -158,3 +160,23 @@ def sync_runtime(request: pytest.FixtureRequest) -> RunSync:
|
|
|
158
160
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
159
161
|
|
|
160
162
|
return run
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
@pytest.fixture
|
|
166
|
+
def function_metadata_writes(monkeypatch: pytest.MonkeyPatch) -> list[str]:
|
|
167
|
+
"""Record functions renamed through functools while the test runs.
|
|
168
|
+
|
|
169
|
+
Free-threaded CPython 3.15 stops the world when a function's name,
|
|
170
|
+
qualified name, module, or docstring is assigned, so operations must
|
|
171
|
+
not create decorated functions per call.
|
|
172
|
+
"""
|
|
173
|
+
writes: list[str] = []
|
|
174
|
+
update_wrapper = functools.update_wrapper
|
|
175
|
+
|
|
176
|
+
def recording(wrapper: Any, wrapped: Any, *args: Any, **kwargs: Any) -> Any:
|
|
177
|
+
if isinstance(wrapper, FunctionType):
|
|
178
|
+
writes.append(getattr(wrapped, "__qualname__", repr(wrapped)))
|
|
179
|
+
return update_wrapper(wrapper, wrapped, *args, **kwargs)
|
|
180
|
+
|
|
181
|
+
monkeypatch.setattr(functools, "update_wrapper", recording)
|
|
182
|
+
return writes
|
|
@@ -46,13 +46,15 @@ class Event:
|
|
|
46
46
|
else:
|
|
47
47
|
self._waiters[guard] = None
|
|
48
48
|
|
|
49
|
-
@shield
|
|
50
|
-
@routine
|
|
51
|
-
async def cleanup() -> None:
|
|
52
|
-
async with self._lock:
|
|
53
|
-
self._waiters.pop(guard, None)
|
|
54
|
-
|
|
55
49
|
try:
|
|
56
50
|
yield
|
|
57
51
|
finally:
|
|
58
|
-
await
|
|
52
|
+
await self._withdraw.a(guard)
|
|
53
|
+
|
|
54
|
+
# Decorate once: shielding wraps functions, and modifying function
|
|
55
|
+
# metadata per call stops the world on free-threaded Python.
|
|
56
|
+
@shield
|
|
57
|
+
@routine
|
|
58
|
+
async def _withdraw(self, guard: SelectorGuard[None]) -> None:
|
|
59
|
+
async with self._lock:
|
|
60
|
+
self._waiters.pop(guard, None)
|
|
@@ -514,3 +514,13 @@ def test_two_events_compete_for_one_selection(backend: str) -> None:
|
|
|
514
514
|
await second.wait.a()
|
|
515
515
|
|
|
516
516
|
anyio.run(main, backend=backend)
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
def test_wait_does_not_decorate_functions(
|
|
520
|
+
function_metadata_writes: list[str],
|
|
521
|
+
) -> None:
|
|
522
|
+
event = Event()
|
|
523
|
+
event.set()
|
|
524
|
+
event.wait()
|
|
525
|
+
assert select(event.wait.select()) == (0, None)
|
|
526
|
+
assert function_metadata_writes == []
|
|
@@ -65,17 +65,26 @@ class Queue[T]:
|
|
|
65
65
|
if not (self._getters and self._queue):
|
|
66
66
|
return
|
|
67
67
|
|
|
68
|
+
# Withdrawal must finish even if cancellation interrupted registration.
|
|
69
|
+
# Decorate once: shielding wraps functions, and modifying function
|
|
70
|
+
# metadata per call stops the world on free-threaded Python.
|
|
71
|
+
@shield
|
|
72
|
+
@routine
|
|
73
|
+
async def _withdraw_getter(self, guard: SelectorGuard[T]) -> None:
|
|
74
|
+
async with self._lock:
|
|
75
|
+
self._getters.pop(guard, None)
|
|
76
|
+
|
|
77
|
+
@shield
|
|
78
|
+
@routine
|
|
79
|
+
async def _withdraw_putter(self, guard: SelectorGuard[None]) -> None:
|
|
80
|
+
async with self._lock:
|
|
81
|
+
self._putters.pop(guard, None)
|
|
82
|
+
|
|
68
83
|
@selectable
|
|
69
84
|
@contextmanager
|
|
70
85
|
async def get(self, guard: SelectorGuard[T]) -> AsyncIterator[None]:
|
|
71
86
|
"""Wait for and remove the oldest value, or raise ShutDown after draining."""
|
|
72
87
|
|
|
73
|
-
@shield
|
|
74
|
-
@routine
|
|
75
|
-
async def cleanup() -> None:
|
|
76
|
-
async with self._lock:
|
|
77
|
-
self._getters.pop(guard, None)
|
|
78
|
-
|
|
79
88
|
try:
|
|
80
89
|
async with self._lock:
|
|
81
90
|
async with selectors(guard) as (selector,):
|
|
@@ -89,19 +98,13 @@ class Queue[T]:
|
|
|
89
98
|
await self._notify.a()
|
|
90
99
|
yield
|
|
91
100
|
finally:
|
|
92
|
-
await
|
|
101
|
+
await self._withdraw_getter.a(guard)
|
|
93
102
|
|
|
94
103
|
@selectable
|
|
95
104
|
@contextmanager
|
|
96
105
|
async def put(self, guard: SelectorGuard[None], value: T) -> AsyncIterator[None]:
|
|
97
106
|
"""Wait for space and store value, or raise ShutDown after shutdown."""
|
|
98
107
|
|
|
99
|
-
@shield
|
|
100
|
-
@routine
|
|
101
|
-
async def cleanup() -> None:
|
|
102
|
-
async with self._lock:
|
|
103
|
-
self._putters.pop(guard, None)
|
|
104
|
-
|
|
105
108
|
try:
|
|
106
109
|
async with self._lock:
|
|
107
110
|
async with selectors(guard) as (selector,):
|
|
@@ -116,7 +119,7 @@ class Queue[T]:
|
|
|
116
119
|
await self._notify.a()
|
|
117
120
|
yield
|
|
118
121
|
finally:
|
|
119
|
-
await
|
|
122
|
+
await self._withdraw_putter.a(guard)
|
|
120
123
|
|
|
121
124
|
@shield
|
|
122
125
|
@routine
|
|
@@ -407,3 +407,16 @@ def test_two_queues_compete_for_one_selection(backend: str, putting: bool) -> No
|
|
|
407
407
|
assert all(not queue._getters and not queue._putters for queue in queues)
|
|
408
408
|
|
|
409
409
|
anyio.run(main, backend=backend)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def test_operations_do_not_decorate_functions(
|
|
413
|
+
function_metadata_writes: list[str],
|
|
414
|
+
) -> None:
|
|
415
|
+
queue = Queue[int]()
|
|
416
|
+
queue.put(1)
|
|
417
|
+
assert queue.get() == 1
|
|
418
|
+
queue.put(2)
|
|
419
|
+
assert select(queue.get.select(), queue.put.select(3)) == (0, 2)
|
|
420
|
+
assert select(queue.get.select(), queue.put.select(3)) == (1, None)
|
|
421
|
+
assert queue.get() == 3
|
|
422
|
+
assert function_metadata_writes == []
|
|
@@ -107,7 +107,7 @@ class Routine[**P, R, **F = P]:
|
|
|
107
107
|
if self.__ordinary is not None:
|
|
108
108
|
return self.__ordinary(*args, **kwargs)
|
|
109
109
|
# Without an override, F defaults to the coroutine's parameters P.
|
|
110
|
-
awaited = cast(Callable[F, Coroutine[Any, Any, R]], self.a)
|
|
110
|
+
awaited = cast("Callable[F, Coroutine[Any, Any, R]]", self.a)
|
|
111
111
|
iterator = awaited(*args, **kwargs).__await__()
|
|
112
112
|
token = _mode.set(_Mode.DRIVEN)
|
|
113
113
|
try:
|
|
@@ -156,19 +156,42 @@ class Routine[**P, R, **F = P]:
|
|
|
156
156
|
self, instance: Any, owner: type[Any] | None = None
|
|
157
157
|
) -> Routine[..., R, ...] | Callable[..., R]:
|
|
158
158
|
if self.__class_method is not None:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
cls = owner if owner is not None else type(instance)
|
|
162
|
-
result.__ordinary = MethodType(self.__ordinary, cls)
|
|
163
|
-
return result
|
|
164
|
-
if instance is None or self.__static_method:
|
|
159
|
+
target = owner if owner is not None else type(instance)
|
|
160
|
+
elif instance is None or self.__static_method:
|
|
165
161
|
return self
|
|
166
|
-
|
|
162
|
+
else:
|
|
163
|
+
target = instance
|
|
164
|
+
# Binding happens on every method access. Reuse metadata copied at
|
|
165
|
+
# decoration time instead of validating and wrapping again.
|
|
166
|
+
result = _shallow_copy(self, Routine)
|
|
167
|
+
method = MethodType(self.__fn, target)
|
|
168
|
+
result.__fn = method
|
|
169
|
+
result.__wrapped__ = method
|
|
170
|
+
result.__class_method = None
|
|
167
171
|
if self.__ordinary is not None:
|
|
168
|
-
result.__ordinary = MethodType(self.__ordinary,
|
|
172
|
+
result.__ordinary = MethodType(self.__ordinary, target)
|
|
169
173
|
return result
|
|
170
174
|
|
|
171
175
|
|
|
176
|
+
def _shallow_copy[T](obj: T, cls: type[T] | None = None) -> T:
|
|
177
|
+
"""Copy obj's attributes into a new, uninitialized instance of cls.
|
|
178
|
+
|
|
179
|
+
Use on per-access paths: copy.copy is several times slower.
|
|
180
|
+
"""
|
|
181
|
+
result = object.__new__(type(obj) if cls is None else cls)
|
|
182
|
+
result.__dict__.update(obj.__dict__)
|
|
183
|
+
return result
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _cleared_call[**P, R](fn: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
|
|
187
|
+
"""Call fn with the coroutine execution mode cleared."""
|
|
188
|
+
token = _mode.set(None)
|
|
189
|
+
try:
|
|
190
|
+
return fn(*args, **kwargs)
|
|
191
|
+
finally:
|
|
192
|
+
_mode.reset(token)
|
|
193
|
+
|
|
194
|
+
|
|
172
195
|
class Function[**P, R]:
|
|
173
196
|
"""An ordinary callable that clears the coroutine execution mode before calling."""
|
|
174
197
|
|
|
@@ -191,11 +214,7 @@ class Function[**P, R]:
|
|
|
191
214
|
update_wrapper(self, self.__fn)
|
|
192
215
|
|
|
193
216
|
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
|
|
194
|
-
|
|
195
|
-
try:
|
|
196
|
-
return self.__fn(*args, **kwargs)
|
|
197
|
-
finally:
|
|
198
|
-
_mode.reset(token)
|
|
217
|
+
return _cleared_call(self.__fn, *args, **kwargs)
|
|
199
218
|
|
|
200
219
|
@overload
|
|
201
220
|
def __get__[T, **B](
|
|
@@ -220,10 +239,17 @@ class Function[**P, R]:
|
|
|
220
239
|
self, instance: Any, owner: type[Any] | None = None
|
|
221
240
|
) -> Function[..., R]:
|
|
222
241
|
if self.__class_method is not None:
|
|
223
|
-
|
|
224
|
-
|
|
242
|
+
target = owner if owner is not None else type(instance)
|
|
243
|
+
elif instance is None or self.__static_method:
|
|
225
244
|
return self
|
|
226
|
-
|
|
245
|
+
else:
|
|
246
|
+
target = instance
|
|
247
|
+
result = _shallow_copy(self, Function)
|
|
248
|
+
method = MethodType(self.__fn, target)
|
|
249
|
+
result.__fn = method
|
|
250
|
+
result.__wrapped__ = method
|
|
251
|
+
result.__class_method = None
|
|
252
|
+
return result
|
|
227
253
|
|
|
228
254
|
|
|
229
255
|
@overload
|
|
@@ -1353,3 +1353,59 @@ def test_routine_with_different_self_types(async_runtime: AsyncRuntime) -> None:
|
|
|
1353
1353
|
assert await asynchronous.run.a() == 42
|
|
1354
1354
|
|
|
1355
1355
|
async_runtime.run(main)
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
def test_bound_methods_keep_wrapper_metadata() -> None:
|
|
1359
|
+
def marked[F: Callable[..., Any]](fn: F) -> F:
|
|
1360
|
+
setattr(fn, "marker", "kept") # noqa: B010
|
|
1361
|
+
return fn
|
|
1362
|
+
|
|
1363
|
+
class Counter:
|
|
1364
|
+
def __init__(self, start: int) -> None:
|
|
1365
|
+
self.start = start
|
|
1366
|
+
|
|
1367
|
+
@routine
|
|
1368
|
+
@marked
|
|
1369
|
+
async def add( # pyright: ignore[reportRedeclaration]
|
|
1370
|
+
self, value: int
|
|
1371
|
+
) -> int:
|
|
1372
|
+
"""Add to the start."""
|
|
1373
|
+
return self.start + value
|
|
1374
|
+
|
|
1375
|
+
@add.fn
|
|
1376
|
+
def add(self, value: int) -> int:
|
|
1377
|
+
return self.start + value + 100
|
|
1378
|
+
|
|
1379
|
+
@routine
|
|
1380
|
+
@classmethod
|
|
1381
|
+
async def name(cls) -> str:
|
|
1382
|
+
"""Name the class."""
|
|
1383
|
+
return cls.__name__
|
|
1384
|
+
|
|
1385
|
+
@cleared_mode
|
|
1386
|
+
def double(self, value: int) -> int:
|
|
1387
|
+
"""Double plus the start."""
|
|
1388
|
+
return self.start + 2 * value
|
|
1389
|
+
|
|
1390
|
+
first, second = Counter(1), Counter(2)
|
|
1391
|
+
bound = [first.add, second.add, Counter.name, first.name, first.double]
|
|
1392
|
+
for method in bound:
|
|
1393
|
+
unbound = Counter.__dict__[method.__name__]
|
|
1394
|
+
unbound = getattr(unbound, "__func__", unbound)
|
|
1395
|
+
for attr in ("__module__", "__name__", "__qualname__", "__doc__"):
|
|
1396
|
+
assert getattr(method, attr) == getattr(unbound, attr)
|
|
1397
|
+
assert "self" not in signature(method).parameters
|
|
1398
|
+
assert "cls" not in signature(method).parameters
|
|
1399
|
+
assert getattr(first.add, "marker") == "kept" # noqa: B009
|
|
1400
|
+
assert getattr(first.add.__wrapped__, "__self__") is first # noqa: B009
|
|
1401
|
+
assert getattr(Counter.name.__wrapped__, "__self__") is Counter # noqa: B009
|
|
1402
|
+
assert getattr(first.name.__wrapped__, "__self__") is Counter # noqa: B009
|
|
1403
|
+
assert (first.add(1), second.add(1)) == (102, 103)
|
|
1404
|
+
assert (first.double(3), second.double(3)) == (7, 8)
|
|
1405
|
+
assert Counter.name() == first.name() == "Counter"
|
|
1406
|
+
|
|
1407
|
+
@routine
|
|
1408
|
+
async def awaited() -> tuple[int, int, str]:
|
|
1409
|
+
return await first.add.a(1), await second.add.a(1), await first.name.a()
|
|
1410
|
+
|
|
1411
|
+
assert asyncio.run(awaited.a()) == (2, 3, "Counter")
|
|
@@ -31,6 +31,7 @@ from ._lock import acquire
|
|
|
31
31
|
from ._selector import Selector as Selector
|
|
32
32
|
from ._selector import SelectorGuard as SelectorGuard
|
|
33
33
|
from .routine import Routine
|
|
34
|
+
from .routine import _shallow_copy
|
|
34
35
|
from .routine import routine
|
|
35
36
|
from .select import select
|
|
36
37
|
|
|
@@ -93,6 +94,12 @@ async def selectors(
|
|
|
93
94
|
Setters record and notify immediately; callers coordinate multi-party
|
|
94
95
|
completion without intervening awaits or other fallible work.
|
|
95
96
|
"""
|
|
97
|
+
if len(guards) == 1:
|
|
98
|
+
# Most selectables complete one participant: skip lock ordering.
|
|
99
|
+
(guard,) = guards
|
|
100
|
+
async with guard.lock:
|
|
101
|
+
yield (guard.selector if guard.selector.pending() else None,)
|
|
102
|
+
return
|
|
96
103
|
if len({id(guard.lock) for guard in guards}) != len(guards):
|
|
97
104
|
raise SelectionCollisionError("Guards from the same selection collide")
|
|
98
105
|
async with acquire(guard.lock for guard in guards):
|
|
@@ -206,7 +213,7 @@ class _CallOverrides:
|
|
|
206
213
|
return result
|
|
207
214
|
|
|
208
215
|
def _bind(self, instance: Any, owner: type[Any] | None) -> Self:
|
|
209
|
-
result =
|
|
216
|
+
result = _shallow_copy(self)
|
|
210
217
|
if instance is None:
|
|
211
218
|
result._unbound = True
|
|
212
219
|
else:
|
|
File without changes
|
|
File without changes
|
{noio-0.2 → noio-0.3}/.gitignore
RENAMED
|
File without changes
|
{noio-0.2 → noio-0.3}/AGENTS.md
RENAMED
|
File without changes
|
|
File without changes
|
{noio-0.2 → noio-0.3}/LICENSE
RENAMED
|
File without changes
|
{noio-0.2 → noio-0.3}/README.md
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|