Skip to content

Commit 0cb3d1c

Browse files
committed
fix: address further PR feedback on imports, pyc caching, json loading, and encodings
- Use importlib.util.source_from_cache for robust .pyc resolution - Move importlib.util and logging imports to module level - Refine json.loads to parse only the last line of stdout - Specify UTF-8 encoding when opening files for writing
1 parent 5c0dfcb commit 0cb3d1c

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

scripts/import_profiler/profiler.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import statistics
66
import tracemalloc
77
import importlib
8+
import importlib.util
89
import csv
910
import os
11+
import logging
1012

1113
def run_worker(target_module):
1214
"""Performs ONE import and returns metrics."""
@@ -32,13 +34,15 @@ def run_worker(target_module):
3234
if mod and getattr(mod, '__file__', None):
3335
file_path = mod.__file__
3436
if file_path.endswith('.pyc'):
35-
file_path = file_path[:-1]
37+
try:
38+
file_path = importlib.util.source_from_cache(file_path)
39+
except ValueError:
40+
pass
3641
if file_path.endswith('.py'):
3742
try:
3843
with open(file_path, 'r', encoding='utf-8') as f:
3944
loaded_lines += sum(1 for _ in f)
4045
except Exception as e:
41-
import logging
4246
logging.warning(f"Failed to read lines from {file_path}: {e}")
4347

4448
# Output to stdout for the Master to capture
@@ -72,7 +76,7 @@ def run_master(iterations, target_module, cpu="0", csv_path=None):
7276
result = subprocess.run(
7377
cmd, capture_output=True, text=True, check=True
7478
)
75-
data = json.loads(result.stdout)
79+
data = json.loads(result.stdout.strip().splitlines()[-1])
7680
times.append(data["time_ms"])
7781
memories.append(data["peak_ram_mb"])
7882
loaded_modules_val = data.get("loaded_modules", 0)
@@ -85,7 +89,7 @@ def run_master(iterations, target_module, cpu="0", csv_path=None):
8589
cmd = [sys.executable, __file__, "--worker", f"--module={target_module}"]
8690
try:
8791
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
88-
data = json.loads(result.stdout)
92+
data = json.loads(result.stdout.strip().splitlines()[-1])
8993
times.append(data["time_ms"])
9094
memories.append(data["peak_ram_mb"])
9195
loaded_modules_val = data.get("loaded_modules", 0)
@@ -101,7 +105,7 @@ def run_master(iterations, target_module, cpu="0", csv_path=None):
101105

102106
# Write CSV if requested
103107
if csv_path:
104-
with open(csv_path, "w", newline="") as f:
108+
with open(csv_path, "w", newline="", encoding="utf-8") as f:
105109
writer = csv.writer(f)
106110
writer.writerow(["Iteration", "Time (ms)", "Peak RAM (MB)"])
107111
for idx, (t, m) in enumerate(zip(times, memories)):
@@ -157,7 +161,7 @@ def run_trace(target_module):
157161
capture_output=True, text=True
158162
)
159163

160-
with open(trace_file, "w") as f:
164+
with open(trace_file, "w", encoding="utf-8") as f:
161165
f.write(result.stderr)
162166

163167
print(f"Trace log successfully written to {trace_file}")

0 commit comments

Comments
 (0)