test_setuphelper.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import os
4 import subprocess
5 import sys
6 from textwrap import dedent
7 
8 import pytest
9 
10 DIR = os.path.abspath(os.path.dirname(__file__))
11 MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
12 WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin")
13 
14 
15 @pytest.mark.parametrize("parallel", [False, True])
16 @pytest.mark.parametrize("std", [11, 0])
17 def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
18  monkeypatch.chdir(tmpdir)
19  monkeypatch.syspath_prepend(MAIN_DIR)
20 
21  (tmpdir / "setup.py").write_text(
22  dedent(
23  f"""\
24  import sys
25  sys.path.append({MAIN_DIR!r})
26 
27  from setuptools import setup, Extension
28  from pybind11.setup_helpers import build_ext, Pybind11Extension
29 
30  std = {std}
31 
32  ext_modules = [
33  Pybind11Extension(
34  "simple_setup",
35  sorted(["main.cpp"]),
36  cxx_std=std,
37  ),
38  ]
39 
40  cmdclass = dict()
41  if std == 0:
42  cmdclass["build_ext"] = build_ext
43 
44 
45  parallel = {parallel}
46  if parallel:
47  from pybind11.setup_helpers import ParallelCompile
48  ParallelCompile().install()
49 
50  setup(
51  name="simple_setup_package",
52  cmdclass=cmdclass,
53  ext_modules=ext_modules,
54  )
55  """
56  ),
57  encoding="ascii",
58  )
59 
60  (tmpdir / "main.cpp").write_text(
61  dedent(
62  """\
63  #include <pybind11/pybind11.h>
64 
65  int f(int x) {
66  return x * 3;
67  }
68  PYBIND11_MODULE(simple_setup, m) {
69  m.def("f", &f);
70  }
71  """
72  ),
73  encoding="ascii",
74  )
75 
76  out = subprocess.check_output(
77  [sys.executable, "setup.py", "build_ext", "--inplace"],
78  )
79  if not WIN:
80  assert b"-g0" in out
81  out = subprocess.check_output(
82  [sys.executable, "setup.py", "build_ext", "--inplace", "--force"],
83  env=dict(os.environ, CFLAGS="-g"),
84  )
85  if not WIN:
86  assert b"-g0" not in out
87 
88  # Debug helper printout, normally hidden
89  print(out)
90  for item in tmpdir.listdir():
91  print(item.basename)
92 
93  assert (
94  len([f for f in tmpdir.listdir() if f.basename.startswith("simple_setup")]) == 1
95  )
96  assert len(list(tmpdir.listdir())) == 4 # two files + output + build_dir
97 
98  (tmpdir / "test.py").write_text(
99  dedent(
100  """\
101  import simple_setup
102  assert simple_setup.f(3) == 9
103  """
104  ),
105  encoding="ascii",
106  )
107 
108  subprocess.check_call(
109  [sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr
110  )
111 
112 
113 def test_intree_extensions(monkeypatch, tmpdir):
114  monkeypatch.syspath_prepend(MAIN_DIR)
115 
116  from pybind11.setup_helpers import intree_extensions
117 
118  monkeypatch.chdir(tmpdir)
119  root = tmpdir
120  root.ensure_dir()
121  subdir = root / "dir"
122  subdir.ensure_dir()
123  src = subdir / "ext.cpp"
124  src.ensure()
125  relpath = src.relto(tmpdir)
126  (ext,) = intree_extensions([relpath])
127  assert ext.name == "ext"
128  subdir.ensure("__init__.py")
129  (ext,) = intree_extensions([relpath])
130  assert ext.name == "dir.ext"
131 
132 
133 def test_intree_extensions_package_dir(monkeypatch, tmpdir):
134  monkeypatch.syspath_prepend(MAIN_DIR)
135 
136  from pybind11.setup_helpers import intree_extensions
137 
138  monkeypatch.chdir(tmpdir)
139  root = tmpdir / "src"
140  root.ensure_dir()
141  subdir = root / "dir"
142  subdir.ensure_dir()
143  src = subdir / "ext.cpp"
144  src.ensure()
145  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
146  assert ext.name == "dir.ext"
147  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
148  assert ext.name == "foo.dir.ext"
149  subdir.ensure("__init__.py")
150  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"})
151  assert ext.name == "dir.ext"
152  (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"})
153  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
list
Definition: pytypes.h:2166
dict
Definition: pytypes.h:2107
pybind11.setup_helpers.intree_extensions
list[Pybind11Extension] intree_extensions(Iterable[str] paths, dict[str, str]|None package_dir=None)
Definition: setup_helpers.py:290
test_setuphelper.test_intree_extensions_package_dir
def test_intree_extensions_package_dir(monkeypatch, tmpdir)
Definition: test_setuphelper.py:133
test_setuphelper.test_intree_extensions
def test_intree_extensions(monkeypatch, tmpdir)
Definition: test_setuphelper.py:113
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
test_setuphelper.test_simple_setup_py
def test_simple_setup_py(monkeypatch, tmpdir, parallel, std)
Definition: test_setuphelper.py:17


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:08