Fix unclosed scandir iterator in path_to_filesystem

Use os.scandir() as a context manager to ensure the iterator is
properly closed. This fixes ResourceWarning: unclosed scandir iterator
that occurs when the iterator is garbage collected without being closed.

Fixes #1972

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Tobias Brox
2026-01-23 16:44:55 +01:00
parent 5b5fc8dd39
commit 0388051046
2 changed files with 95 additions and 3 deletions

View File

@@ -286,9 +286,10 @@ def path_to_filesystem(root: str, sane_path: str) -> str:
safe_path = os.path.join(safe_path, part)
# Check for conflicting files (e.g. case-insensitive file systems
# or short names on Windows file systems)
if (os.path.lexists(safe_path) and
part not in (e.name for e in os.scandir(safe_path_parent))):
raise CollidingPathError(part)
if os.path.lexists(safe_path):
with os.scandir(safe_path_parent) as entries:
if part not in (e.name for e in entries):
raise CollidingPathError(part)
return safe_path