Skip to content

Commit c1346df

Browse files
mayeutbrettcannon
andauthored
fix: Detect when a platform is 32-bit more accurately (#711)
When running a 64-bit version of GraalPy (either x86_64 or aarch64), packaging returns the wrong platform tag (either i686 or armv8l). Fix this by using the same default implementation as found in stdlib platform.architecture https://github.com/python/cpython/blob/4d4393139fae39db26dead33529b6ae0bafbfc58/Lib/platform.py#L713-L716 Co-authored-by: Brett Cannon <brett@python.org>
1 parent 7e68d82 commit c1346df

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/packaging/tags.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
import platform
7+
import struct
78
import subprocess
89
import sys
910
import sysconfig
@@ -37,7 +38,7 @@
3738
}
3839

3940

40-
_32_BIT_INTERPRETER = sys.maxsize <= 2**32
41+
_32_BIT_INTERPRETER = struct.calcsize("P") == 4
4142

4243

4344
class Tag:

tests/test_tags.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import ctypes
1111
except ImportError:
1212
ctypes = None
13+
import importlib
1314
import os
1415
import pathlib
1516
import platform
17+
import struct
1618
import sys
1719
import sysconfig
1820
import types
@@ -1341,3 +1343,29 @@ def test_cpython_first_none_any_tag(self, monkeypatch):
13411343

13421344
interpreter = f"cp{tags.interpreter_version()}"
13431345
assert tag == tags.Tag(interpreter, "none", "any")
1346+
1347+
1348+
class TestBitness:
1349+
def teardown_method(self):
1350+
importlib.reload(tags)
1351+
1352+
@pytest.mark.parametrize(
1353+
"maxsize, sizeof_voidp, expected",
1354+
[
1355+
# 64-bit
1356+
(9223372036854775807, 8, False),
1357+
# 32-bit
1358+
(2147483647, 4, True),
1359+
# 64-bit w/ 32-bit sys.maxsize: GraalPy, IronPython, Jython
1360+
(2147483647, 8, False),
1361+
],
1362+
)
1363+
def test_32bit_interpreter(self, maxsize, sizeof_voidp, expected, monkeypatch):
1364+
def _calcsize(fmt):
1365+
assert fmt == "P"
1366+
return sizeof_voidp
1367+
1368+
monkeypatch.setattr(sys, "maxsize", maxsize)
1369+
monkeypatch.setattr(struct, "calcsize", _calcsize)
1370+
importlib.reload(tags)
1371+
assert tags._32_BIT_INTERPRETER == expected

0 commit comments

Comments
 (0)