Skip to content

Commit fbc6bb4

Browse files
committed
fix: bitness detection
1 parent a86f260 commit fbc6bb4

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/auditwheel/policy/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import platform as _platform_module
66
import re
7+
import struct
78
import sys
89
from collections import defaultdict
910
from os.path import abspath, dirname, join
@@ -21,9 +22,6 @@
2122

2223
logger = logging.getLogger(__name__)
2324

24-
# https://docs.python.org/3/library/platform.html#platform.architecture
25-
bits = 8 * (8 if sys.maxsize > 2**32 else 4)
26-
2725
_POLICY_JSON_MAP = {
2826
Libc.GLIBC: _HERE / "manylinux-policy.json",
2927
Libc.MUSL: _HERE / "musllinux-policy.json",
@@ -224,10 +222,15 @@ def get_req_external(libs: set[str], whitelist: set[str]) -> set[str]:
224222
return ret
225223

226224

227-
def get_arch_name() -> str:
225+
def get_arch_name(*, bits: int | None = None) -> str:
228226
machine = _platform_module.machine()
229227
if sys.platform == "darwin" and machine == "arm64":
230228
return "aarch64"
229+
230+
if bits is None:
231+
# c.f. https://github.com/pypa/packaging/pull/711
232+
bits = 8 * struct.calcsize("P")
233+
231234
if machine in {"x86_64", "i686"}:
232235
return {64: "x86_64", 32: "i686"}[bits]
233236
if machine in {"aarch64", "armv8l"}:

tests/unit/test_policy.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

3+
import platform
34
import re
5+
import struct
6+
import sys
47
from contextlib import nullcontext as does_not_raise
5-
from unittest.mock import patch
68

79
import pytest
810

@@ -32,8 +34,6 @@ def raises(exception, match=None, escape=True):
3234
return pytest.raises(exception, match=match)
3335

3436

35-
@patch("auditwheel.policy._platform_module.machine")
36-
@patch("auditwheel.policy.bits", 32)
3737
@pytest.mark.parametrize(
3838
"reported_arch,expected_arch",
3939
[
@@ -45,14 +45,12 @@ def raises(exception, match=None, escape=True):
4545
("x86_64", "i686"),
4646
],
4747
)
48-
def test_32bits_arch_name(machine_mock, reported_arch, expected_arch):
49-
machine_mock.return_value = reported_arch
50-
machine = get_arch_name()
48+
def test_32bits_arch_name(reported_arch, expected_arch, monkeypatch):
49+
monkeypatch.setattr(platform, "machine", lambda: reported_arch)
50+
machine = get_arch_name(bits=32)
5151
assert machine == expected_arch
5252

5353

54-
@patch("auditwheel.policy._platform_module.machine")
55-
@patch("auditwheel.policy.bits", 64)
5654
@pytest.mark.parametrize(
5755
"reported_arch,expected_arch",
5856
[
@@ -63,12 +61,35 @@ def test_32bits_arch_name(machine_mock, reported_arch, expected_arch):
6361
("x86_64", "x86_64"),
6462
],
6563
)
66-
def test_64bits_arch_name(machine_mock, reported_arch, expected_arch):
67-
machine_mock.return_value = reported_arch
68-
machine = get_arch_name()
64+
def test_64bits_arch_name(reported_arch, expected_arch, monkeypatch):
65+
monkeypatch.setattr(platform, "machine", lambda: reported_arch)
66+
machine = get_arch_name(bits=64)
6967
assert machine == expected_arch
7068

7169

70+
@pytest.mark.parametrize(
71+
"maxsize, sizeof_voidp, expected",
72+
[
73+
# 64-bit
74+
(9223372036854775807, 8, "x86_64"),
75+
# 32-bit
76+
(2147483647, 4, "i686"),
77+
# 64-bit w/ 32-bit sys.maxsize: GraalPy, IronPython, Jython
78+
(2147483647, 8, "x86_64"),
79+
],
80+
)
81+
def test_arch_name_bits(maxsize, sizeof_voidp, expected, monkeypatch):
82+
def _calcsize(fmt):
83+
assert fmt == "P"
84+
return sizeof_voidp
85+
86+
monkeypatch.setattr(platform, "machine", lambda: "x86_64")
87+
monkeypatch.setattr(sys, "maxsize", maxsize)
88+
monkeypatch.setattr(struct, "calcsize", _calcsize)
89+
machine = get_arch_name()
90+
assert machine == expected
91+
92+
7293
@pytest.mark.parametrize(
7394
"name,expected",
7495
[

0 commit comments

Comments
 (0)