Don't leak exceptions when a subprocess fails when run by Platforms/WASI (#156030)

brettcannon <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/04242c027feeff726acb15b6463422897b489bcf
commit: 04242c027feeff726acb15b6463422897b489bcf
branch: main
author: Brett Cannon <[email protected]>
committer: brettcannon <[email protected]>
date: 2026-08-20T12:24:45-07:00
summary:

Don't leak exceptions when a subprocess fails when run by Platforms/WASI (#156030)

files:
M Platforms/WASI/_build.py
M Platforms/WASI/_package.py

diff --git a/Platforms/WASI/_build.py b/Platforms/WASI/_build.py
index 66e2ae64e181d70..fff7e3f9f8cdada 100644
--- a/Platforms/WASI/_build.py
+++ b/Platforms/WASI/_build.py
@@ -129,7 +129,19 @@ def call(command, *, context=None, quiet=False, **kwargs):
         stderr = subprocess.STDOUT
         _shared.log("📝", f"Logging output to {stdout.name} (--quiet)...")
 
-    subprocess.check_call(command, **kwargs, stdout=stdout, stderr=stderr)
+    try:
+        subprocess.check_call(command, **kwargs, stdout=stdout, stderr=stderr)
+    except subprocess.CalledProcessError as error:
+        if quiet:
+            _shared.log("❌", f"Exit code {error.returncode}")
+            separator()
+            with open(stdout.name, encoding="utf-8") as file:
+                lines = file.readlines()
+                # Inefficient, but the log shouldn't be dramatically large.
+                print("".join(lines[-10:]), end="")
+                if not lines[-1].endswith("\n"):
+                    print()
+        sys.exit(error.returncode)
 
 
 @subdir("build_python_path", clean_ok=True)
@@ -163,8 +175,7 @@ def make_build_python(context, _working_dir):
     cmd = [
         binary,
         "-c",
-        "import sys; "
-        "print(f'{sys.version_info.major}.{sys.version_info.minor}')",
+        "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')",
     ]
     version = subprocess.check_output(cmd, encoding="utf-8").strip()
 
diff --git a/Platforms/WASI/_package.py b/Platforms/WASI/_package.py
index 37119799c233a23..d1d43e5da2843c1 100644
--- a/Platforms/WASI/_package.py
+++ b/Platforms/WASI/_package.py
@@ -4,6 +4,7 @@
     "pathlib",
     "shutil",
     "subprocess",
+    "sys",
     "_shared",
 ]
 
@@ -12,6 +13,7 @@
 import pathlib
 import shutil
 import subprocess
+import sys
 
 import _shared
 
@@ -376,45 +378,53 @@ def archive(context):
             int(source_date_epoch), datetime.UTC
         ).strftime(mtime_format)
     else:
-        mtime = subprocess.run(
+        try:
+            mtime = subprocess.run(
+                [
+                    "git",
+                    "log",
+                    "-1",
+                    "--format=tformat:%cd",
+                    f"--date=format:{mtime_format}",
+                    os.fsdecode(context.checkout),
+                ],
+                env={"TZ": "UTC0"},
+                capture_output=True,
+                text=True,
+                check=True,
+            ).stdout.strip()
+        except subprocess.CalledProcessError as error:
+            print(error.output)
+            sys.exit(error.returncode)
+
+    try:
+        subprocess.run(
             [
-                "git",
-                "log",
-                "-1",
-                "--format=tformat:%cd",
-                f"--date=format:{mtime_format}",
-                os.fsdecode(context.checkout),
+                "tar",
+                "-c",
+                "-f",
+                os.fsdecode(file_path),
+                "--sort=name",
+                "--mtime",
+                mtime,
+                "--clamp-mtime",
+                "--owner=0",
+                "--group=0",
+                "--numeric-owner",
+                "--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime",
+                "--mode=go+u,go-w",
+                # Explicitly using `-T` because if you don't compress with threads you can't
+                # uncompress with them and the size difference is negligible when using
+                # single-threaded compression.
+                "--use-compress-program",
+                "xz -T 0",
+                to_compress.name,
             ],
-            env={"TZ": "UTC0"},
+            cwd=to_compress.parent,
             capture_output=True,
             text=True,
             check=True,
-        ).stdout.strip()
-
-    subprocess.run(
-        [
-            "tar",
-            "-c",
-            "-f",
-            os.fsdecode(file_path),
-            "--sort=name",
-            "--mtime",
-            mtime,
-            "--clamp-mtime",
-            "--owner=0",
-            "--group=0",
-            "--numeric-owner",
-            "--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime",
-            "--mode=go+u,go-w",
-            # Explicitly using `-T` because if you don't compress with threads you can't
-            # uncompress with them and the size difference is negligible when using
-            # single-threaded compression.
-            "--use-compress-program",
-            "xz -T 0",
-            to_compress.name,
-        ],
-        cwd=to_compress.parent,
-        capture_output=True,
-        text=True,
-        check=True,
-    )
+        )
+    except subprocess.CalledProcessError as error:
+        print(error.output)
+        sys.exit(error.returncode)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.