Skip to content

Commit

Permalink
refactor to use EXT_SUFFIX
Browse files Browse the repository at this point in the history
  • Loading branch information
mattip committed Nov 10, 2022
1 parent 65cbb7f commit d7d6f00
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 47 deletions.
32 changes: 20 additions & 12 deletions packaging/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,27 @@ def cpython_tags(
yield Tag(interpreter, "abi3", platform_)


def _generic_abi(py_version: PythonVersion) -> List[str]:
"""Return the ABI tag based on SOABI (if available) or emulate SOABI
(CPython 2)."""

soabi = sysconfig.get_config_var("SOABI")
impl = interpreter_name()
if impl == "cp":
if not soabi:
return _cpython_abis(py_version)
def _generic_abi() -> List[str]:
"""Return the ABI tag based on EXT_SUFFIX"""
# ext_suffix is something like this. We want to keep the parts which are
# related to the ABI and remove the parts which are related to the
# platform:
# - linux: '.cpython-310-x86_64-linux-gnu.so' => cp310
# - mac: '.cpython-310-darwin.so' => cp310
# - win: '.cp310-win_amd64.pyd' => cp310
# - pypy: '.pypy38-pp73-x86_64-linux-gnu.so' => pypy38-pp73
# - graalpy: '.graalpy-38-native-x86_64-darwin.dylib'
# => graalpy-38-native

ext_suffix = _get_config_var("EXT_SUFFIX", warn=True)
assert ext_suffix[0] == "."
_, soabi, ext = ext_suffix.split(".")
if soabi.startswith("cpython"):
abi = "cp" + soabi.split("-")[1]
elif soabi and impl == "pp":
# we want something like pypy36-pp73
elif soabi.startswith("pypy"):
abi = "-".join(soabi.split("-")[:2])
elif soabi.startswith("graalpy"):
abi = "-".join(soabi.split("-")[:3])
elif soabi:
abi = soabi
else:
Expand Down Expand Up @@ -265,7 +273,7 @@ def generic_tags(
interp_version = interpreter_version(warn=warn)
interpreter = "".join([interp_name, interp_version])
if abis is None:
abis = _generic_abi(sys.version_info[:2])
abis = _generic_abi()
else:
abis = list(abis)
platforms = list(platforms or platform_tags())
Expand Down
46 changes: 11 additions & 35 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,62 +825,38 @@ def test_no_abi3_python27(self):
class TestGenericTags:
def test__generic_abi_macos(self, monkeypatch):
monkeypatch.setattr(
sysconfig, "get_config_var", lambda key: "cpython-37m-darwin"
sysconfig, "get_config_var", lambda key: ".cpython-37m-darwin.so"
)
monkeypatch.setattr(tags, "interpreter_name", lambda: "cp")
assert tags._generic_abi(sys.version_info[:2]) == ["cp37m"]
assert tags._generic_abi() == ["cp37m"]

def test__generic_abi_linux_cpython(self, monkeypatch):
config = {
"Py_DEBUG": False,
"WITH_PYMALLOC": True,
"SOABI": "cpython-37m-x86_64-linux-gnu",
"EXT_SUFFIX": ".cpython-37m-x86_64-linux-gnu.so",
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.setattr(tags, "interpreter_name", lambda: "cp")
# They are identical
assert tags._cpython_abis((3, 7)) == ["cp37m"]
assert tags._generic_abi(sys.version_info[:2]) == ["cp37m"]
assert tags._generic_abi() == ["cp37m"]

def test__generic_abi_jp(self, monkeypatch):
config = {"SOABI": "return exactly this"}
config = {"EXT_SUFFIX": ".return exactly this.so"}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.setattr(tags, "interpreter_name", lambda: "other")
assert tags._generic_abi(sys.version_info[:2]) == ["return exactly this"]
assert tags._generic_abi() == ["return exactly this"]

def test__generic_abi_linux_pypy_old(self, monkeypatch):
def test__generic_abi_linux_pypy(self, monkeypatch):
# issue gh-606
config = {
"Py_DEBUG": False,
"SOABI": "pypy39-pp73",
"EXT_SUFFIX": ".pypy39-pp73-x86_64-linux-gnu.so",
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.setattr(tags, "interpreter_name", lambda: "pp")
assert tags._generic_abi(sys.version_info[:2]) == ["pypy39_pp73"]

def test__generic_abi_linux_pypy_new(self, monkeypatch):
# issue gh-606
config = {
"Py_DEBUG": False,
"SOABI": "pypy39-pp73-x86_64-linux-gnu",
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.setattr(tags, "interpreter_name", lambda: "pp")
assert tags._generic_abi(sys.version_info[:2]) == ["pypy39_pp73"]

def test__generic_abi_no_soabi_cp(self, monkeypatch):
# CPython falls back to _cpython_abis
monkeypatch.setattr(sysconfig, "get_config_var", lambda key: None)
monkeypatch.setattr(tags, "interpreter_name", lambda: "cp")
ver = sys.version_info[:2]
assert tags._generic_abi(ver) == tags._cpython_abis(ver)

def test__generic_abi_no_soabi_pp(self, monkeypatch):
# Non-cpython should always have a SOABI, so this should never
# happen in practice
monkeypatch.setattr(sysconfig, "get_config_var", lambda key: None)
monkeypatch.setattr(tags, "interpreter_name", lambda: "pp")
ver = sys.version_info[:2]
assert not tags._generic_abi(ver)
assert tags._generic_abi() == ["pypy39_pp73"]

def test_generic_platforms(self):
platform = sysconfig.get_platform().replace("-", "_")
Expand Down Expand Up @@ -916,7 +892,7 @@ def test_interpreter_default(self, monkeypatch):
assert result == [tags.Tag("sillywalkNN", "none", "any")]

def test_abis_default(self, monkeypatch):
monkeypatch.setattr(tags, "_generic_abi", lambda x: ["abi"])
monkeypatch.setattr(tags, "_generic_abi", lambda: ["abi"])
result = list(tags.generic_tags(interpreter="sillywalk", platforms=["any"]))
assert result == [
tags.Tag("sillywalk", "abi", "any"),
Expand Down

0 comments on commit d7d6f00

Please sign in to comment.