Skip to content

Commit

Permalink
Be a bit more conservative about checking abiflags.
Browse files Browse the repository at this point in the history
Also improve test coverage.
  • Loading branch information
colesbury committed Oct 6, 2023
1 parent 7c18384 commit 4578d19
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
35 changes: 25 additions & 10 deletions 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 re
import struct
import subprocess
import sys
Expand Down Expand Up @@ -124,16 +125,30 @@ def _normalize_string(string: str) -> str:
return string.replace(".", "_").replace("-", "_").replace(" ", "_")


def _abi3_applies(python_version: PythonVersion, abis: List[str]) -> bool:
def _is_threaded_cpython(abis: List[str]) -> bool:
"""
Determine if the ABI corresponds to a threaded (`--disable-gil`) build.
The threaded builds are indicated by a "t" in the abiflags.
"""
if len(abis) == 0:
return False
# expect e.g., cp313
m = re.match(r"cp\d+(.*)", abis[0])
if not m:
return False
abiflags = m.group(1)
return "t" in abiflags


def _abi3_applies(python_version: PythonVersion, threading: bool) -> bool:
"""
Determine if the Python version supports abi3.
PEP 384 was first implemented in Python 3.2. The `--disable-gil` builds
do not support abi3 and are indicated by a "t" (for "threading") in their
abi tags.
PEP 384 was first implemented in Python 3.2. The threaded (`--disable-gil`)
builds do not support abi3.
"""
return (len(python_version) > 1 and tuple(python_version) >= (3, 2) and
(len(abis) == 0 or "t" not in abis[0]))
return len(python_version) > 1 and tuple(python_version) >= (3, 2) and not threading


def _cpython_abis(py_version: PythonVersion, warn: bool = False) -> List[str]:
Expand All @@ -149,9 +164,8 @@ def _cpython_abis(py_version: PythonVersion, warn: bool = False) -> List[str]:
has_ext = "_d.pyd" in EXTENSION_SUFFIXES
if with_debug or (with_debug is None and (has_refcount or has_ext)):
debug = "d"
if py_version >= (3, 13):
if _get_config_var("Py_NOGIL", warn):
threading = "t"
if py_version >= (3, 13) and _get_config_var("Py_NOGIL", warn):
threading = "t"
if py_version < (3, 8):
with_pymalloc = _get_config_var("WITH_PYMALLOC", warn)
if with_pymalloc or with_pymalloc is None:
Expand Down Expand Up @@ -215,7 +229,8 @@ def cpython_tags(
for platform_ in platforms:
yield Tag(interpreter, abi, platform_)

use_abi3 = _abi3_applies(python_version, abis)
threading = _is_threaded_cpython(abis)
use_abi3 = _abi3_applies(python_version, threading)
if use_abi3:
yield from (Tag(interpreter, "abi3", platform_) for platform_ in platforms)
yield from (Tag(interpreter, "none", platform_) for platform_ in platforms)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,15 @@ def test__generic_abi_graal(self, monkeypatch):
assert tags._generic_abi() == ["graalpy_38_native"]

def test__generic_abi_disable_gil(self, monkeypatch):
config = {"EXT_SUFFIX": ".cpython-313t-x86_64-linux-gnu.so"}
config = {
"Py_DEBUG": False,
"EXT_SUFFIX": ".cpython-313t-x86_64-linux-gnu.so",
"WITH_PYMALLOC": 0,
"Py_NOGIL": 1,
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
assert tags._generic_abi() == ["cp313t"]
assert tags._generic_abi() == tags._cpython_abis((3, 13))

def test__generic_abi_none(self, monkeypatch):
config = {"EXT_SUFFIX": "..so"}
Expand Down

0 comments on commit 4578d19

Please sign in to comment.