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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04