test_interpreter.cpp
Go to the documentation of this file.
1 #include <pybind11/embed.h>
2 
3 #ifdef _MSC_VER
4 // Silence MSVC C++17 deprecation warning from Catch regarding std::uncaught_exceptions (up to
5 // catch 2.0.1; this should be fixed in the next catch release after 2.0.1).
6 # pragma warning(disable : 4996)
7 #endif
8 
9 #include <catch.hpp>
10 #include <cstdlib>
11 #include <fstream>
12 #include <functional>
13 #include <thread>
14 #include <utility>
15 
16 namespace py = pybind11;
17 using namespace py::literals;
18 
19 class Widget {
20 public:
21  explicit Widget(std::string message) : message(std::move(message)) {}
22  virtual ~Widget() = default;
23 
24  std::string the_message() const { return message; }
25  virtual int the_answer() const = 0;
26  virtual std::string argv0() const = 0;
27 
28 private:
29  std::string message;
30 };
31 
32 class PyWidget final : public Widget {
33  using Widget::Widget;
34 
35  int the_answer() const override { PYBIND11_OVERRIDE_PURE(int, Widget, the_answer); }
36  std::string argv0() const override { PYBIND11_OVERRIDE_PURE(std::string, Widget, argv0); }
37 };
38 
40 
41 public:
42  virtual int func() { return 0; }
43 
44  test_override_cache_helper() = default;
45  virtual ~test_override_cache_helper() = default;
46  // Non-copyable
47  test_override_cache_helper &operator=(test_override_cache_helper const &Right) = delete;
49 };
50 
53 };
54 
55 PYBIND11_EMBEDDED_MODULE(widget_module, m) {
56  py::class_<Widget, PyWidget>(m, "Widget")
57  .def(py::init<std::string>())
58  .def_property_readonly("the_message", &Widget::the_message);
59 
60  m.def("add", [](int i, int j) { return i + j; });
61 }
62 
63 PYBIND11_EMBEDDED_MODULE(trampoline_module, m) {
64  py::class_<test_override_cache_helper,
66  std::shared_ptr<test_override_cache_helper>>(m, "test_override_cache_helper")
67  .def(py::init_alias<>())
68  .def("func", &test_override_cache_helper::func);
69 }
70 
71 PYBIND11_EMBEDDED_MODULE(throw_exception, ) { throw std::runtime_error("C++ Error"); }
72 
73 PYBIND11_EMBEDDED_MODULE(throw_error_already_set, ) {
74  auto d = py::dict();
75  d["missing"].cast<py::object>();
76 }
77 
78 TEST_CASE("Pass classes and data between modules defined in C++ and Python") {
79  auto module_ = py::module_::import("test_interpreter");
80  REQUIRE(py::hasattr(module_, "DerivedWidget"));
81 
82  auto locals = py::dict("hello"_a = "Hello, World!", "x"_a = 5, **module_.attr("__dict__"));
83  py::exec(R"(
84  widget = DerivedWidget("{} - {}".format(hello, x))
85  message = widget.the_message
86  )",
87  py::globals(),
88  locals);
89  REQUIRE(locals["message"].cast<std::string>() == "Hello, World! - 5");
90 
91  auto py_widget = module_.attr("DerivedWidget")("The question");
92  auto message = py_widget.attr("the_message");
93  REQUIRE(message.cast<std::string>() == "The question");
94 
95  const auto &cpp_widget = py_widget.cast<const Widget &>();
96  REQUIRE(cpp_widget.the_answer() == 42);
97 }
98 
99 TEST_CASE("Override cache") {
100  auto module_ = py::module_::import("test_trampoline");
101  REQUIRE(py::hasattr(module_, "func"));
102  REQUIRE(py::hasattr(module_, "func2"));
103 
104  auto locals = py::dict(**module_.attr("__dict__"));
105 
106  int i = 0;
107  for (; i < 1500; ++i) {
108  std::shared_ptr<test_override_cache_helper> p_obj;
109  std::shared_ptr<test_override_cache_helper> p_obj2;
110 
111  py::object loc_inst = locals["func"]();
112  p_obj = py::cast<std::shared_ptr<test_override_cache_helper>>(loc_inst);
113 
114  int ret = p_obj->func();
115 
116  REQUIRE(ret == 42);
117 
118  loc_inst = locals["func2"]();
119 
120  p_obj2 = py::cast<std::shared_ptr<test_override_cache_helper>>(loc_inst);
121 
122  p_obj2->func();
123  }
124 }
125 
126 TEST_CASE("Import error handling") {
127  REQUIRE_NOTHROW(py::module_::import("widget_module"));
128  REQUIRE_THROWS_WITH(py::module_::import("throw_exception"), "ImportError: C++ Error");
129  REQUIRE_THROWS_WITH(py::module_::import("throw_error_already_set"),
130  Catch::Contains("ImportError: initialization failed"));
131 
132  auto locals = py::dict("is_keyerror"_a = false, "message"_a = "not set");
133  py::exec(R"(
134  try:
135  import throw_error_already_set
136  except ImportError as e:
137  is_keyerror = type(e.__cause__) == KeyError
138  message = str(e.__cause__)
139  )",
140  py::globals(),
141  locals);
142  REQUIRE(locals["is_keyerror"].cast<bool>() == true);
143  REQUIRE(locals["message"].cast<std::string>() == "'missing'");
144 }
145 
146 TEST_CASE("There can be only one interpreter") {
151 
152  REQUIRE_THROWS_WITH(py::initialize_interpreter(), "The interpreter is already running");
153  REQUIRE_THROWS_WITH(py::scoped_interpreter(), "The interpreter is already running");
154 
156  REQUIRE_NOTHROW(py::scoped_interpreter());
157  {
158  auto pyi1 = py::scoped_interpreter();
159  auto pyi2 = std::move(pyi1);
160  }
162 }
163 
165  auto builtins = py::handle(PyEval_GetBuiltins());
166  return builtins.contains(PYBIND11_INTERNALS_ID);
167 };
168 
170  auto **&ipp = py::detail::get_internals_pp();
171  return (ipp != nullptr) && (*ipp != nullptr);
172 }
173 
174 TEST_CASE("Restart the interpreter") {
175  // Verify pre-restart state.
176  REQUIRE(py::module_::import("widget_module").attr("add")(1, 2).cast<int>() == 3);
179  REQUIRE(py::module_::import("external_module").attr("A")(123).attr("value").cast<int>()
180  == 123);
181 
182  // local and foreign module internals should point to the same internals:
183  REQUIRE(reinterpret_cast<uintptr_t>(*py::detail::get_internals_pp())
184  == py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>());
185 
186  // Restart the interpreter.
188  REQUIRE(Py_IsInitialized() == 0);
189 
191  REQUIRE(Py_IsInitialized() == 1);
192 
193  // Internals are deleted after a restart.
194  REQUIRE_FALSE(has_pybind11_internals_builtin());
195  REQUIRE_FALSE(has_pybind11_internals_static());
199  REQUIRE(reinterpret_cast<uintptr_t>(*py::detail::get_internals_pp())
200  == py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>());
201 
202  // Make sure that an interpreter with no get_internals() created until finalize still gets the
203  // internals destroyed
206  bool ran = false;
207  py::module_::import("__main__").attr("internals_destroy_test")
208  = py::capsule(&ran, [](void *ran) {
210  *static_cast<bool *>(ran) = true;
211  });
212  REQUIRE_FALSE(has_pybind11_internals_builtin());
213  REQUIRE_FALSE(has_pybind11_internals_static());
214  REQUIRE_FALSE(ran);
216  REQUIRE(ran);
218  REQUIRE_FALSE(has_pybind11_internals_builtin());
219  REQUIRE_FALSE(has_pybind11_internals_static());
220 
221  // C++ modules can be reloaded.
222  auto cpp_module = py::module_::import("widget_module");
223  REQUIRE(cpp_module.attr("add")(1, 2).cast<int>() == 3);
224 
225  // C++ type information is reloaded and can be used in python modules.
226  auto py_module = py::module_::import("test_interpreter");
227  auto py_widget = py_module.attr("DerivedWidget")("Hello after restart");
228  REQUIRE(py_widget.attr("the_message").cast<std::string>() == "Hello after restart");
229 }
230 
231 TEST_CASE("Subinterpreter") {
232  // Add tags to the modules in the main interpreter and test the basics.
233  py::module_::import("__main__").attr("main_tag") = "main interpreter";
234  {
235  auto m = py::module_::import("widget_module");
236  m.attr("extension_module_tag") = "added to module in main interpreter";
237 
238  REQUIRE(m.attr("add")(1, 2).cast<int>() == 3);
239  }
242 
244  auto *main_tstate = PyThreadState_Get();
245  auto *sub_tstate = Py_NewInterpreter();
246 
247  // Subinterpreters get their own copy of builtins. detail::get_internals() still
248  // works by returning from the static variable, i.e. all interpreters share a single
249  // global pybind11::internals;
250  REQUIRE_FALSE(has_pybind11_internals_builtin());
252 
253  // Modules tags should be gone.
254  REQUIRE_FALSE(py::hasattr(py::module_::import("__main__"), "tag"));
255  {
256  auto m = py::module_::import("widget_module");
257  REQUIRE_FALSE(py::hasattr(m, "extension_module_tag"));
258 
259  // Function bindings should still work.
260  REQUIRE(m.attr("add")(1, 2).cast<int>() == 3);
261  }
262 
263  // Restore main interpreter.
264  Py_EndInterpreter(sub_tstate);
265  PyThreadState_Swap(main_tstate);
266 
267  REQUIRE(py::hasattr(py::module_::import("__main__"), "main_tag"));
268  REQUIRE(py::hasattr(py::module_::import("widget_module"), "extension_module_tag"));
269 }
270 
271 TEST_CASE("Execution frame") {
272  // When the interpreter is embedded, there is no execution frame, but `py::exec`
273  // should still function by using reasonable globals: `__main__.__dict__`.
274  py::exec("var = dict(number=42)");
275  REQUIRE(py::globals()["var"]["number"].cast<int>() == 42);
276 }
277 
278 TEST_CASE("Threads") {
279  // Restart interpreter to ensure threads are not initialized
282  REQUIRE_FALSE(has_pybind11_internals_static());
283 
284  constexpr auto num_threads = 10;
285  auto locals = py::dict("count"_a = 0);
286 
287  {
288  py::gil_scoped_release gil_release{};
290 
291  auto threads = std::vector<std::thread>();
292  for (auto i = 0; i < num_threads; ++i) {
293  threads.emplace_back([&]() {
294  py::gil_scoped_acquire gil{};
295  locals["count"] = locals["count"].cast<int>() + 1;
296  });
297  }
298 
299  for (auto &thread : threads) {
300  thread.join();
301  }
302  }
303 
304  REQUIRE(locals["count"].cast<int>() == num_threads);
305 }
306 
307 // Scope exit utility https://stackoverflow.com/a/36644501/7255855
308 struct scope_exit {
309  std::function<void()> f_;
310  explicit scope_exit(std::function<void()> f) noexcept : f_(std::move(f)) {}
312  if (f_) {
313  f_();
314  }
315  }
316 };
317 
318 TEST_CASE("Reload module from file") {
319  // Disable generation of cached bytecode (.pyc files) for this test, otherwise
320  // Python might pick up an old version from the cache instead of the new versions
321  // of the .py files generated below
322  auto sys = py::module_::import("sys");
323  bool dont_write_bytecode = sys.attr("dont_write_bytecode").cast<bool>();
324  sys.attr("dont_write_bytecode") = true;
325  // Reset the value at scope exit
326  scope_exit reset_dont_write_bytecode(
327  [&]() { sys.attr("dont_write_bytecode") = dont_write_bytecode; });
328 
329  std::string module_name = "test_module_reload";
330  std::string module_file = module_name + ".py";
331 
332  // Create the module .py file
333  std::ofstream test_module(module_file);
334  test_module << "def test():\n";
335  test_module << " return 1\n";
336  test_module.close();
337  // Delete the file at scope exit
338  scope_exit delete_module_file([&]() { std::remove(module_file.c_str()); });
339 
340  // Import the module from file
341  auto module_ = py::module_::import(module_name.c_str());
342  int result = module_.attr("test")().cast<int>();
343  REQUIRE(result == 1);
344 
345  // Update the module .py file with a small change
346  test_module.open(module_file);
347  test_module << "def test():\n";
348  test_module << " return 2\n";
349  test_module.close();
350 
351  // Reload the module
352  module_.reload();
353  result = module_.attr("test")().cast<int>();
354  REQUIRE(result == 2);
355 }
356 
357 TEST_CASE("sys.argv gets initialized properly") {
359  {
360  py::scoped_interpreter default_scope;
361  auto module = py::module::import("test_interpreter");
362  auto py_widget = module.attr("DerivedWidget")("The question");
363  const auto &cpp_widget = py_widget.cast<const Widget &>();
364  REQUIRE(cpp_widget.argv0().empty());
365  }
366 
367  {
368  char *argv[] = {strdup("a.out")};
369  py::scoped_interpreter argv_scope(true, 1, argv);
370  std::free(argv[0]);
371  auto module = py::module::import("test_interpreter");
372  auto py_widget = module.attr("DerivedWidget")("The question");
373  const auto &cpp_widget = py_widget.cast<const Widget &>();
374  REQUIRE(cpp_widget.argv0() == "a.out");
375  }
377 }
378 
379 TEST_CASE("make_iterator can be called before then after finalizing an interpreter") {
380  // Reproduction of issue #2101 (https://github.com/pybind/pybind11/issues/2101)
382 
383  std::vector<int> container;
384  {
385  pybind11::scoped_interpreter g;
386  auto iter = pybind11::make_iterator(container.begin(), container.end());
387  }
388 
389  REQUIRE_NOTHROW([&]() {
390  pybind11::scoped_interpreter g;
391  auto iter = pybind11::make_iterator(container.begin(), container.end());
392  }());
393 
395 }
int the_answer() const override
Matrix3f m
std::string argv0() const override
std::string message
void initialize_interpreter(bool init_signal_handlers=true, int argc=0, const char *const *argv=nullptr, bool add_program_dir_to_path=true)
Definition: embed.h:110
bool hasattr(handle obj, handle name)
Definition: pytypes.h:728
T cast() const &
bool has_pybind11_internals_static()
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:405
void finalize_interpreter()
Definition: embed.h:213
Wrapper for Python extension modules.
Definition: pybind11.h:1136
TEST_CASE("Pass classes and data between modules defined in C++ and Python")
Definition: BFloat16.h:88
PYBIND11_EMBEDDED_MODULE(widget_module, m)
iterator iter(handle obj)
Definition: pytypes.h:2273
bool has_pybind11_internals_builtin()
void g(const string &key, int i)
Definition: testBTree.cpp:41
void exec(const str &expr, object global=globals(), object local=object())
Definition: eval.h:88
Values result
scope_exit(std::function< void()> f) noexcept
std::string the_message() const
Widget(std::string message)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
#define PYBIND11_OVERRIDE(ret_type, cname, fn,...)
Definition: pybind11.h:2824
DenseIndex ret
#define PYBIND11_INTERNALS_ID
Definition: internals.h:280
iterator make_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra)
Makes a python iterator from a first and past-the-end C++ InputIterator.
Definition: pybind11.h:2373
#define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn,...)
Definition: pybind11.h:2831
std::function< void()> f_
dict globals()
Definition: pybind11.h:1270
internals **& get_internals_pp()
Definition: internals.h:292
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46