Skip to content

Commit 42aa134

Browse files
authored
chore: minor cleanups and perf tweaks from code review (#2910)
* chore: minor cleanups and perf tweaks from code review - util/file.py, util/python_build_standalone.py: use hashlib.file_digest for streaming SHA-256 verification instead of loading whole archives into memory with read_bytes() - logger.py: convert colors/symbols properties to functools.cached_property so the Colors/Symbols objects are constructed only once per Logger instance - platforms/windows.py: remove redundant .strip() on where_pip (already stripped at assignment) - util/python_build_standalone.py: remove unreachable python_base_dir.exists() guard (callers always pass a fresh temp subdirectory) - util/file.py: add comment explaining the getattr shim for tar_.extraction_filter and when it can be removed Assisted-by: ClaudeCode:claude-fable-5 * revert: restore assertion to check python_base_dir existence
1 parent 01265e5 commit 42aa134

4 files changed

Lines changed: 10 additions & 8 deletions

File tree

‎cibuildwheel/logger.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,11 @@ def _github_step_summary(self, duration: float, options: Options) -> str:
375375
out.write("\n")
376376
return out.getvalue()
377377

378-
@property
378+
@functools.cached_property
379379
def colors(self) -> Colors:
380380
return Colors(enabled=self.colors_enabled)
381381

382-
@property
382+
@functools.cached_property
383383
def symbols(self) -> Symbols:
384384
return Symbols(unicode=self.unicode_enabled)
385385

‎cibuildwheel/platforms/windows.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def setup_python(
348348
assert (venv_path / "Scripts" / "pip.exe").exists()
349349
where_pip = call("where", "pip", env=env, capture_stdout=True).splitlines()[0].strip()
350350
print(where_pip)
351-
if where_pip.strip() != str(venv_path / "Scripts" / "pip.exe"):
351+
if where_pip != str(venv_path / "Scripts" / "pip.exe"):
352352
msg = "pip available on PATH doesn't match our installed instance. If you have modified PATH, ensure that you don't overwrite cibuildwheel's entry or insert pip above it."
353353
raise errors.FatalError(msg)
354354
call("pip", "--version", env=env)

‎cibuildwheel/util/file.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def download(url: str, dest: Path, *, sha256: str | None = None) -> None:
9999
time.sleep(3)
100100

101101
if sha256:
102-
computed = hashlib.sha256(dest.read_bytes()).hexdigest()
102+
with dest.open("rb") as f:
103+
computed = hashlib.file_digest(f, "sha256").hexdigest()
103104
if computed != sha256:
104105
dest.unlink(missing_ok=True)
105106
msg = f"SHA256 mismatch for {url}: expected {sha256!r}, got {computed!r}"
@@ -130,6 +131,8 @@ def extract_tar(tar_src: Path, dest: Path) -> None:
130131
See: https://docs.python.org/3/library/tarfile.html#tarfile.tar_filter for filter details
131132
"""
132133
with tarfile.open(tar_src) as tar_:
134+
# getattr shim needed while Python 3.11.0-3.11.3 are supported;
135+
# once the minimum is 3.11.4+/3.12, replace with: tar_.extractall(dest, filter="tar")
133136
tar_.extraction_filter = getattr(tarfile, "tar_filter", (lambda member, _: member))
134137
tar_.extractall(dest)
135138

‎cibuildwheel/util/python_build_standalone.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def _download_or_get_from_cache(
154154
asset_cache_path = cache_dir / asset_filename
155155
if asset_cache_path.is_file():
156156
if sha256:
157-
computed = hashlib.sha256(asset_cache_path.read_bytes()).hexdigest()
157+
with asset_cache_path.open("rb") as f:
158+
computed = hashlib.file_digest(f, "sha256").hexdigest()
158159
if computed != sha256:
159160
print(
160161
f"Cached python_build_standalone SHA256 mismatch for {asset_cache_path}; redownloading."
@@ -229,9 +230,7 @@ def create_python_build_standalone_environment(
229230
)
230231

231232
python_base_dir = temp_dir / "pbs"
232-
if python_base_dir.exists():
233-
msg = f"python-build-standalone directory already exists: {python_base_dir}"
234-
raise PythonBuildStandaloneError(msg)
233+
assert not python_base_dir.exists()
235234
extract_tar(archive_path, python_base_dir)
236235

237236
return _find_python_executable(python_base_dir)

0 commit comments

Comments
 (0)