Skip to content

Commit

Permalink
fix: Detect when a platform is 32-bit more accurately (#711)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mayeut and brettcannon committed Sep 1, 2023
1 parent 7e68d82 commit c1346df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/packaging/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
import platform
import struct
import subprocess
import sys
import sysconfig
Expand Down Expand Up @@ -37,7 +38,7 @@
}


_32_BIT_INTERPRETER = sys.maxsize <= 2**32
_32_BIT_INTERPRETER = struct.calcsize("P") == 4


class Tag:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import ctypes
except ImportError:
ctypes = None
import importlib
import os
import pathlib
import platform
import struct
import sys
import sysconfig
import types
Expand Down Expand Up @@ -1341,3 +1343,29 @@ def test_cpython_first_none_any_tag(self, monkeypatch):

interpreter = f"cp{tags.interpreter_version()}"
assert tag == tags.Tag(interpreter, "none", "any")


class TestBitness:
def teardown_method(self):
importlib.reload(tags)

@pytest.mark.parametrize(
"maxsize, sizeof_voidp, expected",
[
# 64-bit
(9223372036854775807, 8, False),
# 32-bit
(2147483647, 4, True),
# 64-bit w/ 32-bit sys.maxsize: GraalPy, IronPython, Jython
(2147483647, 8, False),
],
)
def test_32bit_interpreter(self, maxsize, sizeof_voidp, expected, monkeypatch):
def _calcsize(fmt):
assert fmt == "P"
return sizeof_voidp

monkeypatch.setattr(sys, "maxsize", maxsize)
monkeypatch.setattr(struct, "calcsize", _calcsize)
importlib.reload(tags)
assert tags._32_BIT_INTERPRETER == expected

0 comments on commit c1346df

Please sign in to comment.