File size: 1,759 Bytes
be11144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
    tests/test_gil_scoped.cpp -- acquire and release gil

    Copyright (c) 2017 Borja Zarco (Google LLC) <[email protected]>

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

#include "pybind11_tests.h"
#include <pybind11/functional.h>


class VirtClass  {
public:
    virtual ~VirtClass() = default;
    VirtClass() = default;
    VirtClass(const VirtClass&) = delete;
    virtual void virtual_func() {}
    virtual void pure_virtual_func() = 0;
};

class PyVirtClass : public VirtClass {
    void virtual_func() override {
        PYBIND11_OVERLOAD(void, VirtClass, virtual_func,);
    }
    void pure_virtual_func() override {
        PYBIND11_OVERLOAD_PURE(void, VirtClass, pure_virtual_func,);
    }
};

TEST_SUBMODULE(gil_scoped, m) {
  py::class_<VirtClass, PyVirtClass>(m, "VirtClass")
      .def(py::init<>())
      .def("virtual_func", &VirtClass::virtual_func)
      .def("pure_virtual_func", &VirtClass::pure_virtual_func);

    m.def("test_callback_py_obj",
          [](py::object func) { func(); });
    m.def("test_callback_std_func",
          [](const std::function<void()> &func) { func(); });
    m.def("test_callback_virtual_func",
          [](VirtClass &virt) { virt.virtual_func(); });
    m.def("test_callback_pure_virtual_func",
          [](VirtClass &virt) { virt.pure_virtual_func(); });
    m.def("test_cross_module_gil",
          []() {
              auto cm = py::module::import("cross_module_gil_utils");
              auto gil_acquire = reinterpret_cast<void (*)()>(
                  PyLong_AsVoidPtr(cm.attr("gil_acquire_funcaddr").ptr()));
              py::gil_scoped_release gil_release;
              gil_acquire();
          });
}