Skip to content

Commit 0bb8036

Browse files
committed
Extract function to inject macos version.
1 parent a4a1f23 commit 0bb8036

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

distutils/spawn.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
executable name.
77
"""
88

9+
from __future__ import annotations
10+
911
import os
12+
import platform
1013
import subprocess
1114
import sys
1215

16+
from typing import Mapping
17+
1318
from ._log import log
1419
from .debug import DEBUG
1520
from .errors import DistutilsExecError
@@ -22,6 +27,21 @@ def _debug(cmd):
2227
return cmd if DEBUG else cmd[0]
2328

2429

30+
def _inject_macos_ver(env: Mapping[str:str] | None) -> Mapping[str:str] | None:
31+
if platform.system() != 'Darwin':
32+
return env
33+
34+
from distutils.util import MACOSX_VERSION_VAR, get_macosx_target_ver
35+
36+
target_ver = get_macosx_target_ver()
37+
update = {MACOSX_VERSION_VAR: target_ver} if target_ver else {}
38+
return {**_resolve(env), **update}
39+
40+
41+
def _resolve(env: Mapping[str:str] | None) -> Mapping[str:str]:
42+
return os.environ if env is None else env
43+
44+
2545
def spawn(cmd, search_path=True, verbose=False, dry_run=False, env=None):
2646
"""Run another program, specified as a command list 'cmd', in a new process.
2747
@@ -47,17 +67,8 @@ def spawn(cmd, search_path=True, verbose=False, dry_run=False, env=None):
4767
if executable is not None:
4868
cmd[0] = executable
4969

50-
env = env if env is not None else dict(os.environ)
51-
52-
if sys.platform == 'darwin':
53-
from distutils.util import MACOSX_VERSION_VAR, get_macosx_target_ver
54-
55-
macosx_target_ver = get_macosx_target_ver()
56-
if macosx_target_ver:
57-
env[MACOSX_VERSION_VAR] = macosx_target_ver
58-
5970
try:
60-
subprocess.check_call(cmd, env=env)
71+
subprocess.check_call(cmd, env=_inject_macos_ver(env))
6172
except OSError as exc:
6273
raise DistutilsExecError(
6374
f"command {_debug(cmd)!r} failed: {exc.args[-1]}"

0 commit comments

Comments
 (0)