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/5083.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`` more than once would
raise an exception.
6 changes: 3 additions & 3 deletions setuptools/_distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ def finalize_options(self) -> None: # noqa: C901
if self.undef:
self.undef = self.undef.split(',')

if self.swig_opts is None:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the bug isn't obvious: this if/else fork originally assumed that self.swig_opts could be either None or str, but it immediately binds it to a list, so it fails on the second pass with AttributeError: list has no attribute 'split'.

self.swig_opts = []
else:
if isinstance(self.swig_opts, str):
self.swig_opts = self.swig_opts.split(' ')
elif self.swig_opts is None:
self.swig_opts = []

# Finally add the user include and library directories if requested
if self.user:
Expand Down
11 changes: 11 additions & 0 deletions setuptools/tests/test_build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ def C(file):
assert example_stub.startswith(f"{build_lib}/mypkg/__pycache__/ext1")
assert example_stub.endswith(".pyc")

def test_finalize_options_multiple_call(self):
"""
Regression test. Check that calling build_ext.finalize_options
more than once doesn't raise an exception.
"""
extension = Extension('spam.eggs', ['eggs.c'])
dist = Distribution(dict(ext_modules=[extension]))
cmd = build_ext(dist)
cmd.finalize_options()
cmd.finalize_options()


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