codekingpro/portable-devtools
114k
1from __future__ import annotations2 3import os4import sys5from importlib.util import cache_from_source as _compiled_file_name6 7import pytest8from jaraco import path9 10from setuptools.command.build_ext import build_ext, get_abi3_suffix11from setuptools.dist import Distribution12from setuptools.errors import CompileError13from setuptools.extension import Extension14 15from . import environment16from .textwrap import DALS17 18import distutils.command.build_ext as orig19from distutils.sysconfig import get_config_var20 21IS_PYPY = '__pypy__' in sys.builtin_module_names22 23 24class TestBuildExt:25 def test_get_ext_filename(self):26 """27 Setuptools needs to give back the same28 result as distutils, even if the fullname29 is not in ext_map.30 """31 dist = Distribution()32 cmd = build_ext(dist)33 cmd.ext_map['foo/bar'] = ''34 res = cmd.get_ext_filename('foo')35 wanted = orig.build_ext.get_ext_filename(cmd, 'foo')36 assert res == wanted37 38 def test_abi3_filename(self):39 """40 Filename needs to be loadable by several versions41 of Python 3 if 'is_abi3' is truthy on Extension()42 """43 print(get_abi3_suffix())44 45 extension = Extension('spam.eggs', ['eggs.c'], py_limited_api=True)46 dist = Distribution(dict(ext_modules=[extension]))47 cmd = build_ext(dist)48 cmd.finalize_options()49 assert 'spam.eggs' in cmd.ext_map50 res = cmd.get_ext_filename('spam.eggs')51 52 if not get_abi3_suffix():53 assert res.endswith(get_config_var('EXT_SUFFIX'))54 elif sys.platform == 'win32':55 assert res.endswith('eggs.pyd')56 else:57 assert 'abi3' in res58 59 def test_ext_suffix_override(self):60 """61 SETUPTOOLS_EXT_SUFFIX variable always overrides62 default extension options.63 """64 dist = Distribution()65 cmd = build_ext(dist)66 cmd.ext_map['for_abi3'] = ext = Extension(67 'for_abi3',68 ['s.c'],69 # Override shouldn't affect abi3 modules70 py_limited_api=True,71 )72 # Mock value needed to pass tests73 ext._links_to_dynamic = False74 75 if not IS_PYPY:76 expect = cmd.get_ext_filename('for_abi3')77 else:78 # PyPy builds do not use ABI3 tag, so they will79 # also get the overridden suffix.80 expect = 'for_abi3.test-suffix'81 82 try:83 os.environ['SETUPTOOLS_EXT_SUFFIX'] = '.test-suffix'84 res = cmd.get_ext_filename('normal')85 assert 'normal.test-suffix' == res86 res = cmd.get_ext_filename('for_abi3')87 assert expect == res88 finally:89 del os.environ['SETUPTOOLS_EXT_SUFFIX']90 91 def dist_with_example(self):92 files = {93 "src": {"mypkg": {"subpkg": {"ext2.c": ""}}},94 "c-extensions": {"ext1": {"main.c": ""}},95 }96 97 ext1 = Extension("mypkg.ext1", ["c-extensions/ext1/main.c"])98 ext2 = Extension("mypkg.subpkg.ext2", ["src/mypkg/subpkg/ext2.c"])99 ext3 = Extension("ext3", ["c-extension/ext3.c"])100 101 path.build(files)102 return Distribution({103 "script_name": "%test%",104 "ext_modules": [ext1, ext2, ext3],105 "package_dir": {"": "src"},106 })107 108 def test_get_outputs(self, tmpdir_cwd, monkeypatch):109 monkeypatch.setenv('SETUPTOOLS_EXT_SUFFIX', '.mp3') # make test OS-independent110 monkeypatch.setattr('setuptools.command.build_ext.use_stubs', False)111 dist = self.dist_with_example()112 113 # Regular build: get_outputs not empty, but get_output_mappings is empty114 build_ext = dist.get_command_obj("build_ext")115 build_ext.editable_mode = False116 build_ext.ensure_finalized()117 build_lib = build_ext.build_lib.replace(os.sep, "/")118 outputs = [x.replace(os.sep, "/") for x in build_ext.get_outputs()]119 assert outputs == [120 f"{build_lib}/ext3.mp3",121 f"{build_lib}/mypkg/ext1.mp3",122 f"{build_lib}/mypkg/subpkg/ext2.mp3",123 ]124 assert build_ext.get_output_mapping() == {}125 126 # Editable build: get_output_mappings should contain everything in get_outputs127 dist.reinitialize_command("build_ext")128 build_ext.editable_mode = True129 build_ext.ensure_finalized()130 mapping = {131 k.replace(os.sep, "/"): v.replace(os.sep, "/")132 for k, v in build_ext.get_output_mapping().items()133 }134 assert mapping == {135 f"{build_lib}/ext3.mp3": "src/ext3.mp3",136 f"{build_lib}/mypkg/ext1.mp3": "src/mypkg/ext1.mp3",137 f"{build_lib}/mypkg/subpkg/ext2.mp3": "src/mypkg/subpkg/ext2.mp3",138 }139 140 def test_get_output_mapping_with_stub(self, tmpdir_cwd, monkeypatch):141 monkeypatch.setenv('SETUPTOOLS_EXT_SUFFIX', '.mp3') # make test OS-independent142 monkeypatch.setattr('setuptools.command.build_ext.use_stubs', True)143 dist = self.dist_with_example()144 145 # Editable build should create compiled stubs (.pyc files only, no .py)146 build_ext = dist.get_command_obj("build_ext")147 build_ext.editable_mode = True148 build_ext.ensure_finalized()149 for ext in build_ext.extensions:150 monkeypatch.setattr(ext, "_needs_stub", True)151 152 build_lib = build_ext.build_lib.replace(os.sep, "/")153 mapping = {154 k.replace(os.sep, "/"): v.replace(os.sep, "/")155 for k, v in build_ext.get_output_mapping().items()156 }157 158 def C(file):159 """Make it possible to do comparisons and tests in a OS-independent way"""160 return _compiled_file_name(file).replace(os.sep, "/")161 162 assert mapping == {163 C(f"{build_lib}/ext3.py"): C("src/ext3.py"),164 f"{build_lib}/ext3.mp3": "src/ext3.mp3",165 C(f"{build_lib}/mypkg/ext1.py"): C("src/mypkg/ext1.py"),166 f"{build_lib}/mypkg/ext1.mp3": "src/mypkg/ext1.mp3",167 C(f"{build_lib}/mypkg/subpkg/ext2.py"): C("src/mypkg/subpkg/ext2.py"),168 f"{build_lib}/mypkg/subpkg/ext2.mp3": "src/mypkg/subpkg/ext2.mp3",169 }170 171 # Ensure only the compiled stubs are present not the raw .py stub172 assert f"{build_lib}/mypkg/ext1.py" not in mapping173 assert f"{build_lib}/mypkg/subpkg/ext2.py" not in mapping174 175 # Visualize what the cached stub files look like176 example_stub = C(f"{build_lib}/mypkg/ext1.py")177 assert example_stub in mapping178 assert example_stub.startswith(f"{build_lib}/mypkg/__pycache__/ext1")179 assert example_stub.endswith(".pyc")180 181 182class TestBuildExtInplace:183 def get_build_ext_cmd(self, optional: bool, **opts) -> build_ext:184 files: dict[str, str | dict[str, dict[str, str]]] = {185 "eggs.c": "#include missingheader.h\n",186 ".build": {"lib": {}, "tmp": {}},187 }188 path.build(files)189 extension = Extension('spam.eggs', ['eggs.c'], optional=optional)190 dist = Distribution(dict(ext_modules=[extension]))191 dist.script_name = 'setup.py'192 cmd = build_ext(dist)193 vars(cmd).update(build_lib=".build/lib", build_temp=".build/tmp", **opts)194 cmd.ensure_finalized()195 return cmd196 197 def get_log_messages(self, caplog, capsys):198 """199 Historically, distutils "logged" by printing to sys.std*.200 Later versions adopted the logging framework. Grab201 messages regardless of how they were captured.202 """203 std = capsys.readouterr()204 return std.out.splitlines() + std.err.splitlines() + caplog.messages205 206 def test_optional(self, tmpdir_cwd, caplog, capsys):207 """208 If optional extensions fail to build, setuptools should show the error209 in the logs but not fail to build210 """211 cmd = self.get_build_ext_cmd(optional=True, inplace=True)212 cmd.run()213 assert any(214 'build_ext: building extension "spam.eggs" failed'215 for msg in self.get_log_messages(caplog, capsys)216 )217 # No compile error exception should be raised218 219 def test_non_optional(self, tmpdir_cwd):220 # Non-optional extensions should raise an exception221 cmd = self.get_build_ext_cmd(optional=False, inplace=True)222 with pytest.raises(CompileError):223 cmd.run()224 225 226def test_build_ext_config_handling(tmpdir_cwd):227 files = {228 'setup.py': DALS(229 """230 from setuptools import Extension, setup231 setup(232 name='foo',233 version='0.0.0',234 ext_modules=[Extension('foo', ['foo.c'])],235 )236 """237 ),238 'foo.c': DALS(239 """240 #include "Python.h"241 242 #if PY_MAJOR_VERSION >= 3243 244 static struct PyModuleDef moduledef = {245 PyModuleDef_HEAD_INIT,246 "foo",247 NULL,248 0,249 NULL,250 NULL,251 NULL,252 NULL,253 NULL254 };255 256 #define INITERROR return NULL257 258 PyMODINIT_FUNC PyInit_foo(void)259 260 #else261 262 #define INITERROR return263 264 void initfoo(void)265 266 #endif267 {268 #if PY_MAJOR_VERSION >= 3269 PyObject *module = PyModule_Create(&moduledef);270 #else271 PyObject *module = Py_InitModule("extension", NULL);272 #endif273 if (module == NULL)274 INITERROR;275 #if PY_MAJOR_VERSION >= 3276 return module;277 #endif278 }279 """280 ),281 'setup.cfg': DALS(282 """283 [build]284 build_base = foo_build285 """286 ),287 }288 path.build(files)289 code, (stdout, stderr) = environment.run_setup_py(290 cmd=['build'],291 data_stream=(0, 2),292 )293 assert code == 0, f'\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}'294 