test_gil_scoped.cpp
Go to the documentation of this file.
1 /*
2  tests/test_gil_scoped.cpp -- acquire and release gil
3 
4  Copyright (c) 2017 Borja Zarco (Google LLC) <bzarco@google.com>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #include <pybind11/functional.h>
11 
12 #include "pybind11_tests.h"
13 
14 class VirtClass {
15 public:
16  virtual ~VirtClass() = default;
17  VirtClass() = default;
18  VirtClass(const VirtClass &) = delete;
19  virtual void virtual_func() {}
20  virtual void pure_virtual_func() = 0;
21 };
22 
23 class PyVirtClass : public VirtClass {
24  void virtual_func() override { PYBIND11_OVERRIDE(void, VirtClass, virtual_func, ); }
25  void pure_virtual_func() override {
27  }
28 };
29 
30 TEST_SUBMODULE(gil_scoped, m) {
31  py::class_<VirtClass, PyVirtClass>(m, "VirtClass")
32  .def(py::init<>())
33  .def("virtual_func", &VirtClass::virtual_func)
34  .def("pure_virtual_func", &VirtClass::pure_virtual_func);
35 
36  m.def("test_callback_py_obj", [](py::object &func) { func(); });
37  m.def("test_callback_std_func", [](const std::function<void()> &func) { func(); });
38  m.def("test_callback_virtual_func", [](VirtClass &virt) { virt.virtual_func(); });
39  m.def("test_callback_pure_virtual_func", [](VirtClass &virt) { virt.pure_virtual_func(); });
40  m.def("test_cross_module_gil", []() {
41  auto cm = py::module_::import("cross_module_gil_utils");
42  auto gil_acquire = reinterpret_cast<void (*)()>(
43  PyLong_AsVoidPtr(cm.attr("gil_acquire_funcaddr").ptr()));
44  py::gil_scoped_release gil_release;
45  gil_acquire();
46  });
47 }
Matrix3f m
static const double cm
TEST_SUBMODULE(gil_scoped, m)
void virtual_func() override
void pure_virtual_func() override
#define PYBIND11_OVERRIDE(ret_type, cname, fn,...)
Definition: pybind11.h:2824
int func(const int &a)
Definition: testDSF.cpp:221
virtual ~VirtClass()=default
#define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn,...)
Definition: pybind11.h:2831
virtual void pure_virtual_func()=0
virtual void virtual_func()
VirtClass()=default


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