Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:51

0001 #pragma once
0002 
0003 #include <pybind11/eval.h>
0004 #include <pybind11/pybind11.h>
0005 
0006 namespace py = pybind11;
0007 using namespace pybind11::literals;
0008 
0009 class test_initializer {
0010     using Initializer = void (*)(py::module_ &);
0011 
0012 public:
0013     explicit test_initializer(Initializer init);
0014     test_initializer(const char *submodule_name, Initializer init);
0015 };
0016 
0017 #define TEST_SUBMODULE(name, variable)                                                            \
0018     void test_submodule_##name(py::module_ &);                                                    \
0019     test_initializer name(#name, test_submodule_##name);                                          \
0020     void test_submodule_##name(py::module_ &(variable))
0021 
0022 /// Dummy type which is not exported anywhere -- something to trigger a conversion error
0023 struct UnregisteredType {};
0024 
0025 /// A user-defined type which is exported and can be used by any test
0026 class UserType {
0027 public:
0028     UserType() = default;
0029     explicit UserType(int i) : i(i) {}
0030 
0031     int value() const { return i; }
0032     void set(int set) { i = set; }
0033 
0034 private:
0035     int i = -1;
0036 };
0037 
0038 /// Like UserType, but increments `value` on copy for quick reference vs. copy tests
0039 class IncType : public UserType {
0040 public:
0041     using UserType::UserType;
0042     IncType() = default;
0043     IncType(const IncType &other) : IncType(other.value() + 1) {}
0044     IncType(IncType &&) = delete;
0045     IncType &operator=(const IncType &) = delete;
0046     IncType &operator=(IncType &&) = delete;
0047 };
0048 
0049 /// A simple union for basic testing
0050 union IntFloat {
0051     int i;
0052     float f;
0053 };
0054 
0055 /// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast
0056 /// context. Used to test recursive casters (e.g. std::tuple, stl containers).
0057 struct RValueCaster {};
0058 PYBIND11_NAMESPACE_BEGIN(pybind11)
0059 PYBIND11_NAMESPACE_BEGIN(detail)
0060 template <>
0061 class type_caster<RValueCaster> {
0062 public:
0063     PYBIND11_TYPE_CASTER(RValueCaster, const_name("RValueCaster"));
0064     static handle cast(RValueCaster &&, return_value_policy, handle) {
0065         return py::str("rvalue").release();
0066     }
0067     static handle cast(const RValueCaster &, return_value_policy, handle) {
0068         return py::str("lvalue").release();
0069     }
0070 };
0071 PYBIND11_NAMESPACE_END(detail)
0072 PYBIND11_NAMESPACE_END(pybind11)
0073 
0074 template <typename F>
0075 void ignoreOldStyleInitWarnings(F &&body) {
0076     py::exec(R"(
0077     message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"
0078 
0079     import warnings
0080     with warnings.catch_warnings():
0081         warnings.filterwarnings("ignore", message=message, category=FutureWarning)
0082         body()
0083     )",
0084              py::dict(py::arg("body") = py::cpp_function(body)));
0085 }