Skip to content

Commit 22a8361

Browse files
committed
refactor: simplify logic
Assisted-by: Copilot:claude-sonnet-4.6 Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent a657445 commit 22a8361

1 file changed

Lines changed: 48 additions & 54 deletions

File tree

src/scikit_build_core/builder/builder.py

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import dataclasses
4+
import enum
45
import os
56
import re
67
import shlex
@@ -35,6 +36,12 @@
3536
DIR = Path(__file__).parent.resolve()
3637

3738

39+
class _SabiMode(enum.Enum):
40+
NONE = enum.auto()
41+
ABI3 = enum.auto()
42+
ABI3T = enum.auto()
43+
44+
3845
def __dir__() -> list[str]:
3946
return __all__
4047

@@ -211,61 +218,46 @@ def configure(
211218

212219
py_api = self.settings.wheel.py_api
213220
gil_disabled = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
214-
ft_abi = limited_api is True and gil_disabled
215-
if limited_api is None:
216-
if py_api.startswith("cp3"):
217-
if py_api.endswith("t"):
218-
# Free-threaded stable ABI (PEP 803 / abi3t)
219-
target_minor_version = int(py_api[3:-1])
220-
if (
221-
sys.implementation.name == "cpython"
222-
and gil_disabled
223-
and target_minor_version <= sys.version_info.minor
224-
):
225-
ft_abi = True
226-
limited_api = True
227-
else:
228-
limited_api = False
229-
logger.info(
230-
"py-api {} requires free-threaded CPython >= 3.{}, ignoring",
231-
py_api,
232-
target_minor_version,
233-
)
221+
222+
sabi = _SabiMode.NONE
223+
if limited_api is True:
224+
# Handle externally-set limited_api (e.g. from setuptools)
225+
if sys.implementation.name != "cpython":
226+
logger.info("PyPy doesn't support the Limited API, ignoring")
227+
elif gil_disabled:
228+
sabi = _SabiMode.ABI3T
229+
else:
230+
sabi = _SabiMode.ABI3
231+
elif limited_api is None and py_api.startswith("cp3"):
232+
target_minor_version = int(py_api[3:].rstrip("t"))
233+
if sys.implementation.name != "cpython":
234+
logger.info("py-api {} requires CPython, ignoring")
235+
elif py_api.endswith("t"):
236+
# Free-threaded stable ABI (PEP 803 / abi3t)
237+
if gil_disabled and target_minor_version <= sys.version_info.minor:
238+
sabi = _SabiMode.ABI3T
234239
else:
235-
# Classic stable ABI (abi3)
236-
target_minor_version = int(py_api[3:])
237-
if sys.implementation.name != "cpython":
238-
limited_api = False
239-
logger.info("PyPy doesn't support the Limited API, ignoring")
240-
elif gil_disabled:
241-
limited_api = False
242-
logger.info(
243-
"Free-threaded Python doesn't support the classic Limited API, ignoring"
244-
)
245-
else:
246-
limited_api = target_minor_version <= sys.version_info.minor
240+
logger.info(
241+
"py-api {} requires free-threaded CPython >= 3.{}, ignoring",
242+
py_api,
243+
target_minor_version,
244+
)
247245
else:
248-
limited_api = False
249-
250-
# Handle externally-set limited_api (e.g. from setuptools)
251-
if (limited_api or ft_abi) and sys.implementation.name != "cpython":
252-
limited_api = False
253-
ft_abi = False
254-
logger.info("PyPy doesn't support the Limited API, ignoring")
255-
256-
if limited_api and gil_disabled and not ft_abi:
257-
limited_api = False
258-
logger.info(
259-
"Free-threaded Python doesn't support the classic Limited API, ignoring"
260-
)
246+
# Classic stable ABI (abi3)
247+
target_minor_version = int(py_api[3:])
248+
if gil_disabled:
249+
logger.info(
250+
"Free-threaded Python doesn't support the classic Limited API, ignoring"
251+
)
252+
elif target_minor_version <= sys.version_info.minor:
253+
sabi = _SabiMode.ABI3
261254

262255
python_library = get_python_library(self.config.env, abi3=False)
263256
python_sabi_library = None
264-
if limited_api:
265-
if ft_abi:
266-
python_sabi_library = get_python_library(self.config.env, abi3t=True)
267-
else:
268-
python_sabi_library = get_python_library(self.config.env, abi3=True)
257+
if sabi == _SabiMode.ABI3T:
258+
python_sabi_library = get_python_library(self.config.env, abi3t=True)
259+
elif sabi == _SabiMode.ABI3:
260+
python_sabi_library = get_python_library(self.config.env, abi3=True)
269261
python_include_dir = get_python_include_dir()
270262
numpy_include_dir = get_numpy_include_dir()
271263

@@ -302,24 +294,26 @@ def configure(
302294
cache_config[f"{prefix}_NumPy_INCLUDE_DIR"] = numpy_include_dir
303295

304296
cache_config["SKBUILD_SOABI"] = get_soabi(
305-
self.config.env, abi3=(limited_api and not ft_abi), abi3t=ft_abi
297+
self.config.env,
298+
abi3=(sabi == _SabiMode.ABI3),
299+
abi3t=(sabi == _SabiMode.ABI3T),
306300
)
307301

308302
# Allow CMakeLists to detect this is supposed to be a limited ABI build
309303
cache_config["SKBUILD_SABI_COMPONENT"] = (
310-
"Development.SABIModule" if limited_api else ""
304+
"Development.SABIModule" if sabi != _SabiMode.NONE else ""
311305
)
312306

313307
# Allow users to detect the version requested in settings
314-
if limited_api and py_api.startswith("cp"):
308+
if sabi != _SabiMode.NONE and py_api.startswith("cp"):
315309
version_str = py_api[2:]
316310
if version_str.endswith("t"):
317311
version_str = version_str[:-1]
318312
cache_config["SKBUILD_SABI_VERSION"] = f"{version_str[0]}.{version_str[1:]}"
319313
else:
320314
cache_config["SKBUILD_SABI_VERSION"] = ""
321315

322-
if ft_abi:
316+
if sabi == _SabiMode.ABI3T:
323317
cache_config["Py_TARGET_ABI3T"] = "1"
324318

325319
if cache_entries:

0 commit comments

Comments
 (0)