Skip to content

Commit

Permalink
Merge pull request #2723 from zooba/ext_suffix
Browse files Browse the repository at this point in the history
Fixes #2722 Adds an environment variable SETUPTOOLS_EXT_SUFFIX
  • Loading branch information
jaraco committed Jul 19, 2021
2 parents 10b6bc3 + dfc843d commit f2de5dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/2722.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for ``SETUPTOOLS_EXT_SUFFIX`` environment variable to override the suffix normally detected from the ``sysconfig`` module.
12 changes: 9 additions & 3 deletions setuptools/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,20 @@ def copy_extensions_to_source(self):
self.write_stub(package_dir or os.curdir, ext, True)

def get_ext_filename(self, fullname):
filename = _build_ext.get_ext_filename(self, fullname)
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')

if fullname in self.ext_map:
ext = self.ext_map[fullname]
use_abi3 = getattr(ext, 'py_limited_api') and get_abi3_suffix()
if use_abi3:
so_ext = get_config_var('EXT_SUFFIX')
filename = filename[:-len(so_ext)]
filename = filename + get_abi3_suffix()
so_ext = get_abi3_suffix()
filename = filename + so_ext
if isinstance(ext, Library):
fn, ext = os.path.splitext(filename)
return self.shlib_compiler.library_filename(fn, libtype)
Expand Down
36 changes: 36 additions & 0 deletions setuptools/tests/test_build_ext.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import distutils.command.build_ext as orig
from distutils.sysconfig import get_config_var
Expand All @@ -12,6 +13,9 @@
from .textwrap import DALS


IS_PYPY = '__pypy__' in sys.builtin_module_names


class TestBuildExt:
def test_get_ext_filename(self):
"""
Expand Down Expand Up @@ -47,6 +51,38 @@ def test_abi3_filename(self):
else:
assert 'abi3' in res

def test_ext_suffix_override(self):
"""
SETUPTOOLS_EXT_SUFFIX variable always overrides
default extension options.
"""
dist = Distribution()
cmd = build_ext(dist)
cmd.ext_map['for_abi3'] = ext = Extension(
'for_abi3',
['s.c'],
# Override shouldn't affect abi3 modules
py_limited_api=True,
)
# Mock value needed to pass tests
ext._links_to_dynamic = False

if not IS_PYPY:
expect = cmd.get_ext_filename('for_abi3')
else:
# PyPy builds do not use ABI3 tag, so they will
# also get the overridden suffix.
expect = 'for_abi3.test-suffix'

try:
os.environ['SETUPTOOLS_EXT_SUFFIX'] = '.test-suffix'
res = cmd.get_ext_filename('normal')
assert 'normal.test-suffix' == res
res = cmd.get_ext_filename('for_abi3')
assert expect == res
finally:
del os.environ['SETUPTOOLS_EXT_SUFFIX']


def test_build_ext_config_handling(tmpdir_cwd):
files = {
Expand Down

0 comments on commit f2de5dc

Please sign in to comment.