Skip to content

Commit

Permalink
Implement ext-modules from pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Sep 2, 2024
1 parent b1b9f0f commit 3b1051a
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions setuptools/config/_apply_pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
from inspect import cleandoc
from itertools import chain
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Callable, Dict, Mapping, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, Mapping, TypeVar, Union

from .._path import StrPath
from ..errors import RemovedConfigError
from ..extension import Extension
from ..warnings import SetuptoolsWarning

if TYPE_CHECKING:
Expand All @@ -35,6 +36,7 @@
_ProjectReadmeValue: TypeAlias = Union[str, Dict[str, str]]
_CorrespFn: TypeAlias = Callable[["Distribution", Any, StrPath], None]
_Correspondence: TypeAlias = Union[str, _CorrespFn]
_T = TypeVar("_T")

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -117,13 +119,14 @@ def json_compatible_key(key: str) -> str:


def _set_config(dist: Distribution, field: str, value: Any):
val = _PREPROCESS.get(field, _noop)(dist, value)
setter = getattr(dist.metadata, f"set_{field}", None)
if setter:
setter(value)
setter(val)
elif hasattr(dist.metadata, field) or field in SETUPTOOLS_PATCHES:
setattr(dist.metadata, field, value)
setattr(dist.metadata, field, val)
else:
setattr(dist, field, value)
setattr(dist, field, val)


_CONTENT_TYPES = {
Expand Down Expand Up @@ -218,6 +221,17 @@ def _optional_dependencies(dist: Distribution, val: dict, _root_dir):
dist.extras_require = {**existing, **val}


def _ext_modules(dist: Distribution, val: list[dict]) -> list[Extension]:
existing = dist.ext_modules or []
args = ({k.replace("-", "_"): v for k, v in x.items()} for x in val)
new = [Extension(**kw) for kw in args]
return [*existing, *new]


def _noop(_dist: Distribution, val: _T) -> _T:
return val


def _unify_entry_points(project_table: dict):
project = project_table
entry_points = project.pop("entry-points", project.pop("entry_points", {}))
Expand Down Expand Up @@ -376,6 +390,10 @@ def _acessor(obj):
"license_files",
}

_PREPROCESS = {
"ext_modules": _ext_modules,
}

_PREVIOUSLY_DEFINED = {
"name": _attrgetter("metadata.name"),
"version": _attrgetter("metadata.version"),
Expand Down

0 comments on commit 3b1051a

Please sign in to comment.