pybind11_tests.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <pybind11/eval.h>
4 #include <pybind11/pybind11.h>
5 
6 #include <memory>
7 
8 namespace py = pybind11;
9 using namespace pybind11::literals;
10 
12  using Initializer = void (*)(py::module_ &);
13 
14 public:
16  test_initializer(const char *submodule_name, Initializer init);
17 };
18 
19 #define TEST_SUBMODULE(name, variable) \
20  void test_submodule_##name(py::module_ &); \
21  test_initializer name(#name, test_submodule_##name); \
22  void test_submodule_##name(py::module_ &(variable))
23 
25 struct UnregisteredType {};
26 
28 class UserType {
29 public:
30  UserType() = default;
31  explicit UserType(int i) : i(i) {}
32 
33  int value() const { return i; }
34  void set(int set) { i = set; }
35 
36 private:
37  int i = -1;
38 };
39 
41 class IncType : public UserType {
42 public:
43  using UserType::UserType;
44  IncType() = default;
45  IncType(const IncType &other) : IncType(other.value() + 1) {}
46  IncType(IncType &&) = delete;
47  IncType &operator=(const IncType &) = delete;
48  IncType &operator=(IncType &&) = delete;
49 };
50 
52 union IntFloat {
53  int i;
54  float f;
55 };
56 
57 class UnusualOpRef {
58 public:
59  using NonTrivialType = std::shared_ptr<int>; // Almost any non-trivial type will do.
60  // Overriding operator& should not break pybind11.
61  NonTrivialType operator&() { return non_trivial_member; }
62  NonTrivialType operator&() const { return non_trivial_member; }
63 
64 private:
66 };
67 
70 struct RValueCaster {};
73 template <>
75 public:
78  return py::str("rvalue").release();
79  }
81  return py::str("lvalue").release();
82  }
83 };
86 
87 template <typename F>
89  py::exec(R"(
90  message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"
91 
92  import warnings
93  with warnings.catch_warnings():
94  warnings.filterwarnings("ignore", message=message, category=FutureWarning)
95  body()
96  )",
97  py::dict(py::arg("body") = py::cpp_function(body)));
98 }
IntFloat::f
float f
Definition: pybind11_tests.h:54
UnusualOpRef::operator&
NonTrivialType operator&()
Definition: pybind11_tests.h:61
IntFloat::i
int i
Definition: pybind11_tests.h:53
const_name
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: wrap/pybind11/include/pybind11/detail/common.h:499
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
UserType::UserType
UserType()=default
UnregisteredType
Dummy type which is not exported anywhere – something to trigger a conversion error.
Definition: pybind11_tests.h:25
detail
Definition: testSerializationNonlinear.cpp:70
UserType::set
void set(int set)
Definition: pybind11_tests.h:34
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
type_caster< RValueCaster >::cast
static handle cast(RValueCaster &&, return_value_policy, handle)
Definition: pybind11_tests.h:77
IntFloat
A simple union for basic testing.
Definition: pybind11_tests.h:52
handle
Definition: pytypes.h:226
type_caster
Definition: cast.h:38
exec
void exec(const str &expr, object global=globals(), object local=object())
Definition: eval.h:88
ignoreOldStyleInitWarnings
void ignoreOldStyleInitWarnings(F &&body)
Definition: pybind11_tests.h:88
IncType::IncType
IncType()=default
RValueCaster
Definition: pybind11_tests.h:70
eval.h
test_initializer::Initializer
void(*)(py::module_ &) Initializer
Definition: pybind11_tests.h:12
UnusualOpRef::non_trivial_member
NonTrivialType non_trivial_member
Definition: pybind11_tests.h:65
IncType::IncType
IncType(const IncType &other)
Definition: pybind11_tests.h:45
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
type_caster< RValueCaster >::cast
static handle cast(const RValueCaster &, return_value_policy, handle)
Definition: pybind11_tests.h:80
gtsam::symbol_shorthand::F
Key F(std::uint64_t j)
Definition: inference/Symbol.h:153
UnusualOpRef
Definition: pybind11_tests.h:57
set
void set(Container &c, Position position, const Value &value)
Definition: stdlist_overload.cpp:37
PYBIND11_TYPE_CASTER
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:87
pybind11.h
test_initializer
Definition: pybind11_tests.h:11
UserType::value
int value() const
Definition: pybind11_tests.h:33
UnusualOpRef::NonTrivialType
std::shared_ptr< int > NonTrivialType
Definition: pybind11_tests.h:59
UserType::UserType
UserType(int i)
Definition: pybind11_tests.h:31
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
init
Definition: TutorialInplaceLU.cpp:2
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
UnusualOpRef::operator&
NonTrivialType operator&() const
Definition: pybind11_tests.h:62


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:53