From 65cb37868947edd0956a72c3d10f5446f5b6e10d Mon Sep 17 00:00:00 2001 From: Kyle Benesch <4b796c65+github@gmail.com> Date: Mon, 14 Oct 2024 23:24:42 -0700 Subject: [PATCH] Refactor patch_wheel function Remove `raise ValueError` on missing patch file. This was always extraneous since the open call would've raised `FileNotFoundError` on its own. Normally I wouldn't add an undocumented raise to the changelog, but I'm being safe. Convert to pathlib and use str types for Popen. --- Changelog.md | 5 +++++ delocate/delocating.py | 31 +++++++++++++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Changelog.md b/Changelog.md index dd72765..771da88 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,11 @@ rules on making a good Changelog. ## [Unreleased] +### Changed + +- `patch_wheel` function raises `FileNotFoundError` instead of `ValueError` on + missing patch files. + ### Removed - Dropped support for Python 3.7 and Python 3.8. diff --git a/delocate/delocating.py b/delocate/delocating.py index ca1e652..8e3137b 100644 --- a/delocate/delocating.py +++ b/delocate/delocating.py @@ -1092,7 +1092,9 @@ def delocate_wheel( def patch_wheel( - in_wheel: str, patch_fname: str, out_wheel: str | None = None + in_wheel: str | os.PathLike[str], + patch_fname: str | os.PathLike[str], + out_wheel: str | os.PathLike[str] | None = None, ) -> None: """Apply ``-p1`` style patch in `patch_fname` to contents of `in_wheel`. @@ -1101,32 +1103,25 @@ def patch_wheel( Parameters ---------- - in_wheel : str + in_wheel : str or PathLike Filename of wheel to process - patch_fname : str + patch_fname : str or PathLike Filename of patch file. Will be applied with ``patch -p1 < patch_fname`` - out_wheel : None or str + out_wheel : None or str or PathLike Filename of patched wheel to write. If None, overwrite `in_wheel` """ - in_wheel = abspath(in_wheel) - patch_fname = abspath(patch_fname) - if out_wheel is None: - out_wheel = in_wheel - else: - out_wheel = abspath(out_wheel) - if not exists(patch_fname): - raise ValueError(f"patch file {patch_fname} does not exist") + in_wheel = Path(in_wheel).resolve(strict=True) + patch_fname = Path(patch_fname).resolve(strict=True) + out_wheel = Path(out_wheel) if out_wheel is not None else in_wheel with InWheel(in_wheel, out_wheel): - with open(patch_fname, "rb") as fobj: + with open(patch_fname, "rb") as f: patch_proc = Popen( - ["patch", "-p1"], stdin=fobj, stdout=PIPE, stderr=PIPE + ["patch", "-p1"], stdin=f, stdout=PIPE, stderr=PIPE, text=True ) - stdout, stderr = patch_proc.communicate() + stdout, _stderr = patch_proc.communicate() if patch_proc.returncode != 0: - raise RuntimeError( - "Patch failed with stdout:\n" + stdout.decode("latin1") - ) + raise RuntimeError(f"Patch failed with stdout:\n{stdout}") _ARCH_LOOKUP = {"intel": ["i386", "x86_64"], "universal2": ["x86_64", "arm64"]}