File indexing completed on 2025-01-18 10:17:51
0001 import os
0002 import subprocess
0003 import sys
0004 from textwrap import dedent
0005
0006 import pytest
0007
0008 DIR = os.path.abspath(os.path.dirname(__file__))
0009 MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
0010 WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin")
0011
0012
0013 @pytest.mark.parametrize("parallel", [False, True])
0014 @pytest.mark.parametrize("std", [11, 0])
0015 def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
0016 monkeypatch.chdir(tmpdir)
0017 monkeypatch.syspath_prepend(MAIN_DIR)
0018
0019 (tmpdir / "setup.py").write_text(
0020 dedent(
0021 f"""\
0022 import sys
0023 sys.path.append({MAIN_DIR!r})
0024
0025 from setuptools import setup, Extension
0026 from pybind11.setup_helpers import build_ext, Pybind11Extension
0027
0028 std = {std}
0029
0030 ext_modules = [
0031 Pybind11Extension(
0032 "simple_setup",
0033 sorted(["main.cpp"]),
0034 cxx_std=std,
0035 ),
0036 ]
0037
0038 cmdclass = dict()
0039 if std == 0:
0040 cmdclass["build_ext"] = build_ext
0041
0042
0043 parallel = {parallel}
0044 if parallel:
0045 from pybind11.setup_helpers import ParallelCompile
0046 ParallelCompile().install()
0047
0048 setup(
0049 name="simple_setup_package",
0050 cmdclass=cmdclass,
0051 ext_modules=ext_modules,
0052 )
0053 """
0054 ),
0055 encoding="ascii",
0056 )
0057
0058 (tmpdir / "main.cpp").write_text(
0059 dedent(
0060 """\
0061 #include <pybind11/pybind11.h>
0062
0063 int f(int x) {
0064 return x * 3;
0065 }
0066 PYBIND11_MODULE(simple_setup, m) {
0067 m.def("f", &f);
0068 }
0069 """
0070 ),
0071 encoding="ascii",
0072 )
0073
0074 out = subprocess.check_output(
0075 [sys.executable, "setup.py", "build_ext", "--inplace"],
0076 )
0077 if not WIN:
0078 assert b"-g0" in out
0079 out = subprocess.check_output(
0080 [sys.executable, "setup.py", "build_ext", "--inplace", "--force"],
0081 env=dict(os.environ, CFLAGS="-g"),
0082 )
0083 if not WIN:
0084 assert b"-g0" not in out
0085
0086
0087 print(out)
0088 for item in tmpdir.listdir():
0089 print(item.basename)
0090
0091 assert (
0092 len([f for f in tmpdir.listdir() if f.basename.startswith("simple_setup")]) == 1
0093 )
0094 assert len(list(tmpdir.listdir())) == 4
0095
0096 (tmpdir / "test.py").write_text(
0097 dedent(
0098 """\
0099 import simple_setup
0100 assert simple_setup.f(3) == 9
0101 """
0102 ),
0103 encoding="ascii",
0104 )
0105
0106 subprocess.check_call(
0107 [sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr
0108 )
0109
0110
0111 def test_intree_extensions(monkeypatch, tmpdir):
0112 monkeypatch.syspath_prepend(MAIN_DIR)
0113
0114 from pybind11.setup_helpers import intree_extensions
0115
0116 monkeypatch.chdir(tmpdir)
0117 root = tmpdir
0118 root.ensure_dir()
0119 subdir = root / "dir"
0120 subdir.ensure_dir()
0121 src = subdir / "ext.cpp"
0122 src.ensure()
0123 relpath = src.relto(tmpdir)
0124 (ext,) = intree_extensions([relpath])
0125 assert ext.name == "ext"
0126 subdir.ensure("__init__.py")
0127 (ext,) = intree_extensions([relpath])
0128 assert ext.name == "dir.ext"
0129
0130
0131 def test_intree_extensions_package_dir(monkeypatch, tmpdir):
0132 monkeypatch.syspath_prepend(MAIN_DIR)
0133
0134 from pybind11.setup_helpers import intree_extensions
0135
0136 monkeypatch.chdir(tmpdir)
0137 root = tmpdir / "src"
0138 root.ensure_dir()
0139 subdir = root / "dir"
0140 subdir.ensure_dir()
0141 src = subdir / "ext.cpp"
0142 src.ensure()
0143 (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
0144 assert ext.name == "dir.ext"
0145 (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
0146 assert ext.name == "foo.dir.ext"
0147 subdir.ensure("__init__.py")
0148 (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
0149 assert ext.name == "dir.ext"
0150 (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
0151 assert ext.name == "foo.dir.ext"