Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions newsfragments/5084.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a bug where calling ``build_ext.finalize_options`` after ``define`` or
``undef`` attributes were already set would raise an exception.
4 changes: 2 additions & 2 deletions setuptools/_distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,14 @@ def finalize_options(self) -> None: # noqa: C901
# specified by the 'define' option will be set to '1'. Multiple
# symbols can be separated with commas.

if self.define:
if isinstance(self.define, str):
defines = self.define.split(',')
self.define = [(symbol, '1') for symbol in defines]

# The option for macros to undefine is also a string from the
# option parsing, but has to be a list. Multiple symbols can also
# be separated with commas here.
if self.undef:
if isinstance(self.undef, str):
self.undef = self.undef.split(',')

if self.swig_opts is None:
Expand Down
16 changes: 16 additions & 0 deletions setuptools/tests/test_build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ def C(file):
assert example_stub.startswith(f"{build_lib}/mypkg/__pycache__/ext1")
assert example_stub.endswith(".pyc")

def test_finalize_options_subclassing(self):
"""
Regression test. Check that calling build_ext.finalize_options after
define or undef attributes were already set to their final type doesn't raise.
"""
extension = Extension('spam.eggs', ['eggs.c'])
dist = Distribution(dict(ext_modules=[extension]))
cmd = build_ext(dist)
cmd.define = [("MY_MACRO", "1")]
cmd.undef = ["EVIL_MACRO"]

cmd.finalize_options()

assert cmd.define == [("MY_MACRO", "1")]
assert cmd.undef == ["EVIL_MACRO"]


class TestBuildExtInplace:
def get_build_ext_cmd(self, optional: bool, **opts) -> build_ext:
Expand Down