Skip to content

Commit 2019c16

Browse files
authored
Parse ext-module.define-macros from pyproject.toml as list of tuples (#5169)
1 parent b809c86 commit 2019c16

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

newsfragments/5169.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pre-process ``ext-module.define-macros`` from ``pyproject.toml`` into a list of tuples.

setuptools/config/_apply_pyprojecttoml.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,18 @@ def _optional_dependencies(dist: Distribution, val: dict, _root_dir: StrPath | N
268268
def _ext_modules(dist: Distribution, val: list[dict]) -> list[Extension]:
269269
existing = dist.ext_modules or []
270270
args = ({k.replace("-", "_"): v for k, v in x.items()} for x in val)
271-
new = [Extension(**kw) for kw in args]
271+
new = (Extension(**_adjust_ext_attrs(kw)) for kw in args)
272272
return [*existing, *new]
273273

274274

275+
def _adjust_ext_attrs(attrs: dict) -> dict:
276+
# https://github.com/pypa/setuptools/issues/4810
277+
# In TOML there is no differentiation between tuples and lists,
278+
# and distutils requires tuples...
279+
attrs["define_macros"] = list(map(tuple, attrs.get("define_macros") or []))
280+
return attrs
281+
282+
275283
def _noop(_dist: Distribution, val: _T) -> _T:
276284
return val
277285

setuptools/tests/config/test_apply_pyprojecttoml.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,14 @@ def test_invalid_module_name(self, tmp_path, monkeypatch, module):
535535

536536

537537
class TestExtModules:
538+
def make_dist(self, toml_config):
539+
pyproject = Path("pyproject.toml")
540+
pyproject.write_text(cleandoc(toml_config), encoding="utf-8")
541+
with pytest.warns(pyprojecttoml._ExperimentalConfiguration):
542+
return pyprojecttoml.apply_configuration(Distribution({}), pyproject)
543+
538544
def test_pyproject_sets_attribute(self, tmp_path, monkeypatch):
539545
monkeypatch.chdir(tmp_path)
540-
pyproject = Path("pyproject.toml")
541546
toml_config = """
542547
[project]
543548
name = "test"
@@ -547,13 +552,28 @@ def test_pyproject_sets_attribute(self, tmp_path, monkeypatch):
547552
{name = "my.ext", sources = ["hello.c", "world.c"]}
548553
]
549554
"""
550-
pyproject.write_text(cleandoc(toml_config), encoding="utf-8")
551-
with pytest.warns(pyprojecttoml._ExperimentalConfiguration):
552-
dist = pyprojecttoml.apply_configuration(Distribution({}), pyproject)
555+
dist = self.make_dist(toml_config)
553556
assert len(dist.ext_modules) == 1
554557
assert dist.ext_modules[0].name == "my.ext"
555558
assert set(dist.ext_modules[0].sources) == {"hello.c", "world.c"}
556559

560+
def test_pyproject_define_macros_as_tuples(self, tmp_path, monkeypatch):
561+
# https://github.com/pypa/setuptools/issues/4810
562+
monkeypatch.chdir(tmp_path)
563+
toml_config = """
564+
[project]
565+
name = "test"
566+
version = "42.0"
567+
[[tool.setuptools.ext-modules]]
568+
name = "my.ext"
569+
sources = ["hello.c", "world.c"]
570+
define-macros = [["FIRST_SINGLE"], ["SECOND_TWO", "1"]]
571+
"""
572+
dist = self.make_dist(toml_config)
573+
assert isinstance(dist.ext_modules[0].define_macros[0], tuple)
574+
assert dist.ext_modules[0].define_macros[0] == ("FIRST_SINGLE",)
575+
assert dist.ext_modules[0].define_macros[1] == ("SECOND_TWO", "1")
576+
557577

558578
class TestDeprecatedFields:
559579
def test_namespace_packages(self, tmp_path):

0 commit comments

Comments
 (0)