Blame external/pybind11/tests/test_call_policies.cpp

Packit 534379
/*
Packit 534379
    tests/test_call_policies.cpp -- keep_alive and call_guard
Packit 534379
Packit 534379
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Packit 534379
Packit 534379
    All rights reserved. Use of this source code is governed by a
Packit 534379
    BSD-style license that can be found in the LICENSE file.
Packit 534379
*/
Packit 534379
Packit 534379
#include "pybind11_tests.h"
Packit 534379
Packit 534379
struct CustomGuard {
Packit 534379
    static bool enabled;
Packit 534379
Packit 534379
    CustomGuard() { enabled = true; }
Packit 534379
    ~CustomGuard() { enabled = false; }
Packit 534379
Packit 534379
    static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
Packit 534379
};
Packit 534379
bool CustomGuard::enabled = false;
Packit 534379
Packit 534379
struct DependentGuard {
Packit 534379
    static bool enabled;
Packit 534379
Packit 534379
    DependentGuard() { enabled = CustomGuard::enabled; }
Packit 534379
    ~DependentGuard() { enabled = false; }
Packit 534379
Packit 534379
    static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
Packit 534379
};
Packit 534379
bool DependentGuard::enabled = false;
Packit 534379
Packit 534379
TEST_SUBMODULE(call_policies, m) {
Packit 534379
    // Parent/Child are used in:
Packit 534379
    // test_keep_alive_argument, test_keep_alive_return_value, test_alive_gc_derived,
Packit 534379
    // test_alive_gc_multi_derived, test_return_none, test_keep_alive_constructor
Packit 534379
    class Child {
Packit 534379
    public:
Packit 534379
        Child() { py::print("Allocating child."); }
Packit 534379
        Child(const Child &) = default;
Packit 534379
        Child(Child &&) = default;
Packit 534379
        ~Child() { py::print("Releasing child."); }
Packit 534379
    };
Packit 534379
    py::class_<Child>(m, "Child")
Packit 534379
        .def(py::init<>());
Packit 534379
Packit 534379
    class Parent {
Packit 534379
    public:
Packit 534379
        Parent() { py::print("Allocating parent."); }
Packit 534379
        ~Parent() { py::print("Releasing parent."); }
Packit 534379
        void addChild(Child *) { }
Packit 534379
        Child *returnChild() { return new Child(); }
Packit 534379
        Child *returnNullChild() { return nullptr; }
Packit 534379
    };
Packit 534379
    py::class_<Parent>(m, "Parent")
Packit 534379
        .def(py::init<>())
Packit 534379
        .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>())
Packit 534379
        .def("addChild", &Parent::addChild)
Packit 534379
        .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
Packit 534379
        .def("returnChild", &Parent::returnChild)
Packit 534379
        .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
Packit 534379
        .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
Packit 534379
        .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
Packit 534379
Packit 534379
#if !defined(PYPY_VERSION)
Packit 534379
    // test_alive_gc
Packit 534379
    class ParentGC : public Parent {
Packit 534379
    public:
Packit 534379
        using Parent::Parent;
Packit 534379
    };
Packit 534379
    py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
Packit 534379
        .def(py::init<>());
Packit 534379
#endif
Packit 534379
Packit 534379
    // test_call_guard
Packit 534379
    m.def("unguarded_call", &CustomGuard::report_status);
Packit 534379
    m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>());
Packit 534379
Packit 534379
    m.def("multiple_guards_correct_order", []() {
Packit 534379
        return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status();
Packit 534379
    }, py::call_guard<CustomGuard, DependentGuard>());
Packit 534379
Packit 534379
    m.def("multiple_guards_wrong_order", []() {
Packit 534379
        return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status();
Packit 534379
    }, py::call_guard<DependentGuard, CustomGuard>());
Packit 534379
Packit 534379
#if defined(WITH_THREAD) && !defined(PYPY_VERSION)
Packit 534379
    // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well,
Packit 534379
    // but it's unclear how to test it without `PyGILState_GetThisThreadState`.
Packit 534379
    auto report_gil_status = []() {
Packit 534379
        auto is_gil_held = false;
Packit 534379
        if (auto tstate = py::detail::get_thread_state_unchecked())
Packit 534379
            is_gil_held = (tstate == PyGILState_GetThisThreadState());
Packit 534379
Packit 534379
        return is_gil_held ? "GIL held" : "GIL released";
Packit 534379
    };
Packit 534379
Packit 534379
    m.def("with_gil", report_gil_status);
Packit 534379
    m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());
Packit 534379
#endif
Packit 534379
}