Skip to content

Commit 0b1cb9b

Browse files
committed
fix: macOS platform tags with old macOS SDK
This retrieves the real macOS version to compute macOS platform tags when the python interpreter is built with an old macOS SDK. fixes #497
1 parent 71c8e12 commit 0b1cb9b

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

packaging/tags.py

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
import platform
7+
import subprocess
78
import sys
89
import sysconfig
910
from importlib.machinery import EXTENSION_SUFFIXES
@@ -356,6 +357,22 @@ def mac_platforms(
356357
version_str, _, cpu_arch = platform.mac_ver()
357358
if version is None:
358359
version = cast("MacVersion", tuple(map(int, version_str.split(".")[:2])))
360+
if version == (10, 16):
361+
# When built against an older macOS SDK, Python will report macOS 10.16
362+
# instead of the real version.
363+
version_str = subprocess.run(
364+
[
365+
sys.executable,
366+
"-sS",
367+
"-c",
368+
"import platform; print(platform.mac_ver()[0])",
369+
],
370+
check=True,
371+
env={"SYSTEM_VERSION_COMPAT": "0"},
372+
stdout=subprocess.PIPE,
373+
universal_newlines=True,
374+
).stdout
375+
version = cast("MacVersion", tuple(map(int, version_str.split(".")[:2])))
359376
else:
360377
version = version
361378
if arch is None:

tests/test_tags.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
import collections.abc
7+
import subprocess
78

89
try:
910
import ctypes
@@ -230,12 +231,48 @@ def test_version_detection(self, monkeypatch):
230231
version = platform.mac_ver()[0].split(".")
231232
major = version[0]
232233
minor = version[1] if major == "10" else "0"
233-
expected = f"macosx_{major}_{minor}"
234+
235+
platforms = list(tags.mac_platforms(arch="x86_64"))
236+
if (major, minor) == ("10", "16"):
237+
print(platforms, "macosx_11+")
238+
# For 10.16, the real version is at least 11.0.
239+
prefix, major, minor, _ = platforms[0].split("_", maxsplit=3)
240+
assert prefix == "macosx"
241+
assert int(major) >= 11
242+
assert minor == "0"
243+
else:
244+
expected = f"macosx_{major}_{minor}_"
245+
print(platforms, expected)
246+
assert platforms[0].startswith(expected)
247+
248+
def test_version_detection_10_15(self, monkeypatch):
249+
monkeypatch.setattr(
250+
platform, "mac_ver", lambda: ("10.15", ("", "", ""), "x86_64")
251+
)
252+
expected = "macosx_10_15_"
234253

235254
platforms = list(tags.mac_platforms(arch="x86_64"))
236255
print(platforms, expected)
237256
assert platforms[0].startswith(expected)
238257

258+
def test_version_detection_compatibility(self, monkeypatch):
259+
if platform.system() != "Darwin":
260+
monkeypatch.setattr(
261+
subprocess,
262+
"run",
263+
lambda *args, **kwargs: subprocess.CompletedProcess(
264+
[], 0, stdout="10.15"
265+
),
266+
)
267+
monkeypatch.setattr(
268+
platform, "mac_ver", lambda: ("10.16", ("", "", ""), "x86_64")
269+
)
270+
unexpected = "macosx_10_16_"
271+
272+
platforms = list(tags.mac_platforms(arch="x86_64"))
273+
print(platforms, unexpected)
274+
assert not platforms[0].startswith(unexpected)
275+
239276
@pytest.mark.parametrize("arch", ["x86_64", "i386"])
240277
def test_arch_detection(self, arch, monkeypatch):
241278
if platform.system() != "Darwin" or platform.mac_ver()[2] != arch:

0 commit comments

Comments
 (0)