test_setuphelper.py
Go to the documentation of this file.
1 import os
2 import subprocess
3 import sys
4 from textwrap import dedent
5 
6 import pytest
7 
8 DIR = os.path.abspath(os.path.dirname(__file__))
9 MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
10 WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin")
11 
12 
13 @pytest.mark.parametrize("parallel", [False, True])
14 @pytest.mark.parametrize("std", [11, 0])
15 def 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  f"""\
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  ),
55  encoding="ascii",
56  )
57 
58  (tmpdir / "main.cpp").write_text(
59  dedent(
60  """\
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  out = subprocess.check_output(
75  [sys.executable, "setup.py", "build_ext", "--inplace"],
76  )
77  if not WIN:
78  assert b"-g0" in out
79  out = subprocess.check_output(
80  [sys.executable, "setup.py", "build_ext", "--inplace", "--force"],
81  env=dict(os.environ, CFLAGS="-g"),
82  )
83  if not WIN:
84  assert b"-g0" not in out
85 
86  # Debug helper printout, normally hidden
87  print(out)
88  for item in tmpdir.listdir():
89  print(item.basename)
90 
91  assert (
92  len([f for f in tmpdir.listdir() if f.basename.startswith("simple_setup")]) == 1
93  )
94  assert len(list(tmpdir.listdir())) == 4 # two files + output + build_dir
95 
96  (tmpdir / "test.py").write_text(
97  dedent(
98  """\
99  import simple_setup
100  assert simple_setup.f(3) == 9
101  """
102  ),
103  encoding="ascii",
104  )
105 
106  subprocess.check_call(
107  [sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr
108  )
109 
110 
111 def test_intree_extensions(monkeypatch, tmpdir):
112  monkeypatch.syspath_prepend(MAIN_DIR)
113 
114  from pybind11.setup_helpers import intree_extensions
115 
116  monkeypatch.chdir(tmpdir)
117  root = tmpdir
118  root.ensure_dir()
119  subdir = root / "dir"
120  subdir.ensure_dir()
121  src = subdir / "ext.cpp"
122  src.ensure()
123  relpath = src.relto(tmpdir)
124  (ext,) = intree_extensions([relpath])
125  assert ext.name == "ext"
126  subdir.ensure("__init__.py")
127  (ext,) = intree_extensions([relpath])
128  assert ext.name == "dir.ext"
129 
130 
131 def test_intree_extensions_package_dir(monkeypatch, tmpdir):
132  monkeypatch.syspath_prepend(MAIN_DIR)
133 
134  from pybind11.setup_helpers import intree_extensions
135 
136  monkeypatch.chdir(tmpdir)
137  root = tmpdir / "src"
138  root.ensure_dir()
139  subdir = root / "dir"
140  subdir.ensure_dir()
141  src = subdir / "ext.cpp"
142  src.ensure()
143  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
144  assert ext.name == "dir.ext"
145  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
146  assert ext.name == "foo.dir.ext"
147  subdir.ensure("__init__.py")
148  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
149  assert ext.name == "dir.ext"
150  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
151  assert ext.name == "foo.dir.ext"
pybind11.setup_helpers
Definition: setup_helpers.py:1
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
pybind11.setup_helpers.intree_extensions
List[Pybind11Extension] intree_extensions(Iterable[str] paths, Optional[Dict[str, str]] package_dir=None)
Definition: setup_helpers.py:288
list
Definition: pytypes.h:2124
dict
Definition: pytypes.h:2065
test_setuphelper.test_intree_extensions_package_dir
def test_intree_extensions_package_dir(monkeypatch, tmpdir)
Definition: test_setuphelper.py:131
test_setuphelper.test_intree_extensions
def test_intree_extensions(monkeypatch, tmpdir)
Definition: test_setuphelper.py:111
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2399
test_setuphelper.test_simple_setup_py
def test_simple_setup_py(monkeypatch, tmpdir, parallel, std)
Definition: test_setuphelper.py:15


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:31