diff --git a/distutils/command/build_ext.py b/distutils/command/build_ext.py index ec45b440..1b4763eb 100644 --- a/distutils/command/build_ext.py +++ b/distutils/command/build_ext.py @@ -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: - 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: diff --git a/distutils/tests/test_build_ext.py b/distutils/tests/test_build_ext.py index dab0507f..c85083e0 100644 --- a/distutils/tests/test_build_ext.py +++ b/distutils/tests/test_build_ext.py @@ -320,6 +320,12 @@ def test_finalize_options(self): cmd.finalize_options() assert cmd.swig_opts == ['1', '2'] + # make sure finalize_options() can be called more than once + # without raising an exception + cmd = self.build_ext(dist) + cmd.finalize_options() + cmd.finalize_options() + def test_check_extensions_list(self): dist = Distribution() cmd = self.build_ext(dist) diff --git a/newsfragments/385.bugfix.rst b/newsfragments/385.bugfix.rst new file mode 100644 index 00000000..30e52b16 --- /dev/null +++ b/newsfragments/385.bugfix.rst @@ -0,0 +1,2 @@ +Fix a bug where calling ``distutils.build_ext.finalize_options`` more than once +would raise an exception. \ No newline at end of file