From c1c6c0265d96af6d8f6429f4cf051342e228aa5d Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 24 Aug 2024 14:41:37 -0400 Subject: [PATCH] Make get_ext_filename typesafe --- newsfragments/xxxx.misc.rst | 1 + setuptools/command/build_ext.py | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 newsfragments/xxxx.misc.rst diff --git a/newsfragments/xxxx.misc.rst b/newsfragments/xxxx.misc.rst new file mode 100644 index 00000000000..79d36a9d82f --- /dev/null +++ b/newsfragments/xxxx.misc.rst @@ -0,0 +1 @@ +If somehow the ``EXT_SUFFIX`` configuration variable and ``SETUPTOOLS_EXT_SUFFIX`` environment variables are both missing, ``setuptools.command.build_ext.pyget_ext_filename`` will now raise an `OSError` instead of a `TypeError` -- by :user:`Avasam` diff --git a/setuptools/command/build_ext.py b/setuptools/command/build_ext.py index d44d7b8ae1f..1aea7c90378 100644 --- a/setuptools/command/build_ext.py +++ b/setuptools/command/build_ext.py @@ -155,21 +155,25 @@ def _get_output_mapping(self) -> Iterator[tuple[str, str]]: output_cache = _compiled_file_name(regular_stub, optimization=opt) yield (output_cache, inplace_cache) - def get_ext_filename(self, fullname): + def get_ext_filename(self, fullname: str) -> str: so_ext = os.getenv('SETUPTOOLS_EXT_SUFFIX') if so_ext: filename = os.path.join(*fullname.split('.')) + so_ext else: filename = _build_ext.get_ext_filename(self, fullname) - so_ext = get_config_var('EXT_SUFFIX') + ext_suffix = get_config_var('EXT_SUFFIX') + if not isinstance(ext_suffix, str): + raise OSError( + "Configuration variable EXT_SUFFIX not found for this platform " + + "and environment variable SETUPTOOLS_EXT_SUFFIX is missing" + ) + so_ext = ext_suffix if fullname in self.ext_map: ext = self.ext_map[fullname] - use_abi3 = ext.py_limited_api and get_abi3_suffix() - if use_abi3: - filename = filename[: -len(so_ext)] - so_ext = get_abi3_suffix() - filename = filename + so_ext + abi3_suffix = get_abi3_suffix() + if ext.py_limited_api and abi3_suffix: # Use abi3 + filename = filename[: -len(so_ext)] + abi3_suffix if isinstance(ext, Library): fn, ext = os.path.splitext(filename) return self.shlib_compiler.library_filename(fn, libtype)