Skip to content

Commit 400d3bd

Browse files
mikedep333claude
authored andcommitted
refactor(sources): use EAFP pattern instead of is_file() check
Replace the is_file() stat syscall with a try/except around open() to handle symlinks to directories in scan_compiled_extensions. This avoids an extra syscall per file, eliminates the TOCTOU race between the check and the open, and is more idiomatic Python (EAFP over LBYL). Only IsADirectoryError is caught to avoid masking real I/O errors. Co-Authored-By: Claude Opus 4.6 <claude@anthropic.com> Signed-off-by: Mike DePaulo <mikedep333@redhat.com>
1 parent cd32d2e commit 400d3bd

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/fromager/sources.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,11 @@ def scan_compiled_extensions(
867867
elif suffix not in ignore_suffixes:
868868
# Path.walk() lists symlinks to directories as filenames
869869
# rather than dirnames, causing IsADirectoryError on open().
870-
if not filepath.is_file():
870+
try:
871+
with filepath.open("rb") as f:
872+
header = f.read(_MAGIC_HEADERS_READ)
873+
except IsADirectoryError:
871874
continue
872-
with filepath.open("rb") as f:
873-
header = f.read(_MAGIC_HEADERS_READ)
874875
if header.startswith(magic_headers):
875876
relpath = filepath.relative_to(root_dir)
876877
logger.debug(

0 commit comments

Comments
 (0)