diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index 1485a7d3..e8bef593 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -13,6 +13,9 @@ This library adheres to `Semantic Versioning 2.0 `_. an object with a ``.fileno()`` method or an integer handle, and deprecated their now obsolete versions (``wait_socket_readable()`` and ``wait_socket_writable()``) (PR by @davidbrochart) +- Changed ``EventAdapter`` (an ``Event`` with no bound async backend) to allow ``set()`` + to work even before an async backend is bound to it + (`#819 `_) - Added support for ``wait_readable()`` and ``wait_writable()`` on ``ProactorEventLoop`` (used on asyncio + Windows by default) - Fixed the return type annotations of ``readinto()`` and ``readinto1()`` methods in the diff --git a/src/anyio/_core/_synchronization.py b/src/anyio/_core/_synchronization.py index 023ab733..7878ba66 100644 --- a/src/anyio/_core/_synchronization.py +++ b/src/anyio/_core/_synchronization.py @@ -109,6 +109,7 @@ def statistics(self) -> EventStatistics: class EventAdapter(Event): _internal_event: Event | None = None + _is_set: bool = False def __new__(cls) -> EventAdapter: return object.__new__(cls) @@ -117,14 +118,22 @@ def __new__(cls) -> EventAdapter: def _event(self) -> Event: if self._internal_event is None: self._internal_event = get_async_backend().create_event() + if self._is_set: + self._internal_event.set() return self._internal_event def set(self) -> None: - self._event.set() + if self._internal_event is None: + self._is_set = True + else: + self._event.set() def is_set(self) -> bool: - return self._internal_event is not None and self._internal_event.is_set() + if self._internal_event is None: + return self._is_set + + return self._internal_event.is_set() async def wait(self) -> None: await self._event.wait() diff --git a/tests/test_synchronization.py b/tests/test_synchronization.py index cc58188b..83758c62 100644 --- a/tests/test_synchronization.py +++ b/tests/test_synchronization.py @@ -316,16 +316,17 @@ async def waiter() -> None: def test_instantiate_outside_event_loop( self, anyio_backend_name: str, anyio_backend_options: dict[str, Any] ) -> None: - async def use_event() -> None: - event.set() - await event.wait() - event = Event() assert not event.is_set() assert event.statistics().tasks_waiting == 0 + event.set() + assert event.is_set() + run( - use_event, backend=anyio_backend_name, backend_options=anyio_backend_options + event.wait, + backend=anyio_backend_name, + backend_options=anyio_backend_options, )