Skip to content

Commit

Permalink
Fix iterdir making blocking call in Python 3.13
Browse files Browse the repository at this point in the history
fix #873
  • Loading branch information
cbornet committed Feb 20, 2025
1 parent 8bad9c0 commit 174e8ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Fix ``iterdir`` making blocking call in Python 3.13
(`#873 <https://github.com/agronholm/anyio/issues/873>`_; PR by @cbornet)
- Added ``stdin`` argument to ``anyio.run_process()`` akin to what
``anyio.open_process()``, ``asyncio.create_subprocess_…()``, ``trio.run_process()``,
and ``subprocess.run()`` already accept (PR by @jmehnle)
Expand Down
24 changes: 23 additions & 1 deletion src/anyio/_core/_fileio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import operator
import os
import pathlib
import sys
Expand Down Expand Up @@ -544,8 +545,29 @@ 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)

_remove_leading_dot = operator.itemgetter(slice(2, None))

def _from_parsed_string(self, path_str: str) -> pathlib.Path:
path = (
self._path.with_segments(path_str)
if hasattr(self._path, "with_segments")
else type(self._path)(path_str)
)
path._str = path_str or "." # type: ignore[attr-defined]
return path

def _iterdir_sync(self) -> Iterator[pathlib.Path]:
"""Like Python 3.13 pathlib.Path.iterdir but calling scandir lazily."""
root_dir = str(self)
with os.scandir(root_dir) as scandir_it:
for entry in scandir_it:
path = entry.path
if root_dir == ".":
path = self._remove_leading_dot(path)
yield self._from_parsed_string(path)

def iterdir(self) -> AsyncIterator[Path]:
gen = self._path.iterdir()
gen = self._iterdir_sync()
return _PathIterator(gen)

def joinpath(self, *args: str | PathLike[str]) -> Path:
Expand Down

0 comments on commit 174e8ed

Please sign in to comment.