From ebc420cee48c2a18908b138ffd22afb46aa7e8a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Mon, 17 Nov 2025 22:18:41 +0100 Subject: [PATCH] BUG: fixed a bug where calling `build_ext.finalize_options` after `define` or `undef` attributes were already set would raise an exception. --- distutils/command/build_ext.py | 4 ++-- distutils/tests/test_build_ext.py | 10 ++++++++++ newsfragments/386.bugfix.rst | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 newsfragments/386.bugfix.rst diff --git a/distutils/command/build_ext.py b/distutils/command/build_ext.py index ec45b440..6e61e567 100644 --- a/distutils/command/build_ext.py +++ b/distutils/command/build_ext.py @@ -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: diff --git a/distutils/tests/test_build_ext.py b/distutils/tests/test_build_ext.py index dab0507f..cabdd789 100644 --- a/distutils/tests/test_build_ext.py +++ b/distutils/tests/test_build_ext.py @@ -320,6 +320,16 @@ def test_finalize_options(self): cmd.finalize_options() assert cmd.swig_opts == ['1', '2'] + # Check that calling build_ext.finalize_options after + # define or undef attributes were already set to their final type doesn't raise + cmd = self.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"] + def test_check_extensions_list(self): dist = Distribution() cmd = self.build_ext(dist) diff --git a/newsfragments/386.bugfix.rst b/newsfragments/386.bugfix.rst new file mode 100644 index 00000000..65eba288 --- /dev/null +++ b/newsfragments/386.bugfix.rst @@ -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. \ No newline at end of file