• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2import os
3import sys
4import subprocess
5from textwrap import dedent
6
7import pytest
8
9DIR = os.path.abspath(os.path.dirname(__file__))
10MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
11
12
13@pytest.mark.parametrize("parallel", [False, True])
14@pytest.mark.parametrize("std", [11, 0])
15def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
16    monkeypatch.chdir(tmpdir)
17    monkeypatch.syspath_prepend(MAIN_DIR)
18
19    (tmpdir / "setup.py").write_text(
20        dedent(
21            u"""\
22            import sys
23            sys.path.append({MAIN_DIR!r})
24
25            from setuptools import setup, Extension
26            from pybind11.setup_helpers import build_ext, Pybind11Extension
27
28            std = {std}
29
30            ext_modules = [
31                Pybind11Extension(
32                    "simple_setup",
33                    sorted(["main.cpp"]),
34                    cxx_std=std,
35                ),
36            ]
37
38            cmdclass = dict()
39            if std == 0:
40                cmdclass["build_ext"] = build_ext
41
42
43            parallel = {parallel}
44            if parallel:
45                from pybind11.setup_helpers import ParallelCompile
46                ParallelCompile().install()
47
48            setup(
49                name="simple_setup_package",
50                cmdclass=cmdclass,
51                ext_modules=ext_modules,
52            )
53            """
54        ).format(MAIN_DIR=MAIN_DIR, std=std, parallel=parallel),
55        encoding="ascii",
56    )
57
58    (tmpdir / "main.cpp").write_text(
59        dedent(
60            u"""\
61            #include <pybind11/pybind11.h>
62
63            int f(int x) {
64                return x * 3;
65            }
66            PYBIND11_MODULE(simple_setup, m) {
67                m.def("f", &f);
68            }
69            """
70        ),
71        encoding="ascii",
72    )
73
74    subprocess.check_call(
75        [sys.executable, "setup.py", "build_ext", "--inplace"],
76        stdout=sys.stdout,
77        stderr=sys.stderr,
78    )
79
80    # Debug helper printout, normally hidden
81    for item in tmpdir.listdir():
82        print(item.basename)
83
84    assert (
85        len([f for f in tmpdir.listdir() if f.basename.startswith("simple_setup")]) == 1
86    )
87    assert len(list(tmpdir.listdir())) == 4  # two files + output + build_dir
88
89    (tmpdir / "test.py").write_text(
90        dedent(
91            u"""\
92            import simple_setup
93            assert simple_setup.f(3) == 9
94            """
95        ),
96        encoding="ascii",
97    )
98
99    subprocess.check_call(
100        [sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr
101    )
102