Skip to content

Commit e7b43b2

Browse files
authored
Merge pull request #11859 from bluetech/numbered-dir-scandir
pathlib: speed up `make_numbered_dir` given a large tmp root
2 parents d0f427a + eb9013d commit e7b43b2

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/_pytest/pathlib.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,23 @@ def rm_rf(path: Path) -> None:
171171
shutil.rmtree(str(path), onerror=onerror)
172172

173173

174-
def find_prefixed(root: Path, prefix: str) -> Iterator[Path]:
174+
def find_prefixed(root: Path, prefix: str) -> Iterator["os.DirEntry[str]"]:
175175
"""Find all elements in root that begin with the prefix, case insensitive."""
176176
l_prefix = prefix.lower()
177-
for x in root.iterdir():
177+
for x in os.scandir(root):
178178
if x.name.lower().startswith(l_prefix):
179179
yield x
180180

181181

182-
def extract_suffixes(iter: Iterable[PurePath], prefix: str) -> Iterator[str]:
182+
def extract_suffixes(iter: Iterable["os.DirEntry[str]"], prefix: str) -> Iterator[str]:
183183
"""Return the parts of the paths following the prefix.
184184
185185
:param iter: Iterator over path names.
186186
:param prefix: Expected prefix of the path names.
187187
"""
188188
p_len = len(prefix)
189-
for p in iter:
190-
yield p.name[p_len:]
189+
for entry in iter:
190+
yield entry.name[p_len:]
191191

192192

193193
def find_suffixes(root: Path, prefix: str) -> Iterator[str]:
@@ -346,12 +346,12 @@ def cleanup_candidates(root: Path, prefix: str, keep: int) -> Iterator[Path]:
346346
"""List candidates for numbered directories to be removed - follows py.path."""
347347
max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)
348348
max_delete = max_existing - keep
349-
paths = find_prefixed(root, prefix)
350-
paths, paths2 = itertools.tee(paths)
351-
numbers = map(parse_num, extract_suffixes(paths2, prefix))
352-
for path, number in zip(paths, numbers):
349+
entries = find_prefixed(root, prefix)
350+
entries, entries2 = itertools.tee(entries)
351+
numbers = map(parse_num, extract_suffixes(entries2, prefix))
352+
for entry, number in zip(entries, numbers):
353353
if number <= max_delete:
354-
yield path
354+
yield Path(entry)
355355

356356

357357
def cleanup_dead_symlinks(root: Path):

0 commit comments

Comments
 (0)