Skip to content

Commit 479b39d

Browse files
committed
refactor: simplify sys.modules traversal and add debug logging for missing modules
Addresses PR review by replacing null-checks with a cleaner try/except block that assumes normal module behavior and explicitly logs when a C-extension or built-in is skipped due to a missing __file__ attribute.
1 parent 68465e5 commit 479b39d

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

scripts/import_profiler/profiler.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ def run_worker(target_module):
6363

6464
loaded_lines = 0
6565
for m in new_modules:
66-
mod = sys.modules.get(m)
67-
if mod and getattr(mod, '__file__', None):
68-
file_path = mod.__file__
66+
try:
67+
file_path = sys.modules[m].__file__
68+
if not file_path:
69+
continue
70+
6971
if file_path.endswith('.pyc'):
7072
try:
7173
file_path = importlib.util.source_from_cache(file_path)
@@ -81,6 +83,10 @@ def run_worker(target_module):
8183
loaded_lines += sum(1 for _ in f)
8284
except OSError as e:
8385
logging.warning(f"Failed to read lines from {file_path}: {e}")
86+
except KeyError:
87+
logging.debug(f"Module {m} disappeared from sys.modules during execution.")
88+
except AttributeError:
89+
logging.debug(f"Module {m} has no __file__ attribute (likely a C-extension or built-in). Skipping.")
8490

8591
# Output to stdout for the Master to capture
8692
metrics = {

0 commit comments

Comments
 (0)