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
  • Loading branch information
cbornet committed Feb 20, 2025
1 parent 8bad9c0 commit 9077c3f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 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,25 @@ 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)
path._str = path_str or '.'
return path

def _iterdir_sync(self) -> Iterator[pathlib.Path]:
"""Like Python 3.13 pathlib.Path.iterdir but lazily calling scandir."""
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 9077c3f

Please sign in to comment.