diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index 61c0f6ed..794cc8ad 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -13,6 +13,9 @@ This library adheres to `Semantic Versioning 2.0 `_. nesting on asyncio due to exception chaining when raising ``ExceptionGroups`` in ``TaskGroup.__aexit__`` (`#863 `_; PR by @tapetersen) +- Fixed ``anyio.Path.iterdir()`` making a blocking call in Python 3.13 + (`#873 `_; PR by @cbornet and + @agronholm) **4.8.0** diff --git a/src/anyio/_core/_fileio.py b/src/anyio/_core/_fileio.py index 350a873a..a0d61984 100644 --- a/src/anyio/_core/_fileio.py +++ b/src/anyio/_core/_fileio.py @@ -544,9 +544,14 @@ async def is_socket(self) -> bool: async def is_symlink(self) -> bool: return await to_thread.run_sync(self._path.is_symlink, abandon_on_cancel=True) - def iterdir(self) -> AsyncIterator[Path]: - gen = self._path.iterdir() - return _PathIterator(gen) + async def iterdir(self) -> AsyncIterator[Path]: + gen = ( + self._path.iterdir() + if sys.version_info < (3, 13) + else await to_thread.run_sync(self._path.iterdir, abandon_on_cancel=True) + ) + async for path in _PathIterator(gen): + yield path def joinpath(self, *args: str | PathLike[str]) -> Path: return Path(self._path.joinpath(*args))