• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 #include <pybind11/pybind11.h>
3 #include <pybind11/eval.h>
4 
5 #if defined(_MSC_VER) && _MSC_VER < 1910
6 // We get some really long type names here which causes MSVC 2015 to emit warnings
7 #  pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
8 #endif
9 
10 namespace py = pybind11;
11 using namespace pybind11::literals;
12 
13 class test_initializer {
14     using Initializer = void (*)(py::module_ &);
15 
16 public:
17     test_initializer(Initializer init);
18     test_initializer(const char *submodule_name, Initializer init);
19 };
20 
21 #define TEST_SUBMODULE(name, variable)                   \
22     void test_submodule_##name(py::module_ &);            \
23     test_initializer name(#name, test_submodule_##name); \
24     void test_submodule_##name(py::module_ &variable)
25 
26 
27 /// Dummy type which is not exported anywhere -- something to trigger a conversion error
28 struct UnregisteredType { };
29 
30 /// A user-defined type which is exported and can be used by any test
31 class UserType {
32 public:
33     UserType() = default;
UserType(int i)34     UserType(int i) : i(i) { }
35 
value()36     int value() const { return i; }
set(int set)37     void set(int set) { i = set; }
38 
39 private:
40     int i = -1;
41 };
42 
43 /// Like UserType, but increments `value` on copy for quick reference vs. copy tests
44 class IncType : public UserType {
45 public:
46     using UserType::UserType;
47     IncType() = default;
IncType(const IncType & other)48     IncType(const IncType &other) : IncType(other.value() + 1) { }
49     IncType(IncType &&) = delete;
50     IncType &operator=(const IncType &) = delete;
51     IncType &operator=(IncType &&) = delete;
52 };
53 
54 /// A simple union for basic testing
55 union IntFloat {
56     int i;
57     float f;
58 };
59 
60 /// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context.
61 /// Used to test recursive casters (e.g. std::tuple, stl containers).
62 struct RValueCaster {};
63 PYBIND11_NAMESPACE_BEGIN(pybind11)
PYBIND11_NAMESPACE_BEGIN(detail)64 PYBIND11_NAMESPACE_BEGIN(detail)
65 template<> class type_caster<RValueCaster> {
66 public:
67     PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster"));
68     static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); }
69     static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); }
70 };
71 PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(pybind11)72 PYBIND11_NAMESPACE_END(pybind11)
73 
74 template <typename F>
75 void ignoreOldStyleInitWarnings(F &&body) {
76     py::exec(R"(
77     message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"
78 
79     import warnings
80     with warnings.catch_warnings():
81         warnings.filterwarnings("ignore", message=message, category=FutureWarning)
82         body()
83     )", py::dict(py::arg("body") = py::cpp_function(body)));
84 }
85