μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_pickling.cpp
Go to the documentation of this file.
1// clang-format off
2/*
3 tests/test_pickling.cpp -- pickle support
4
5 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6 Copyright (c) 2021 The Pybind Development Team.
7
8 All rights reserved. Use of this source code is governed by a
9 BSD-style license that can be found in the LICENSE file.
10*/
11
12#include "pybind11_tests.h"
13
14// clang-format on
15
16#include <memory>
17#include <stdexcept>
18#include <utility>
19
21
22struct SimpleBase {
23 int num = 0;
24 virtual ~SimpleBase() = default;
25
26 // For compatibility with old clang versions:
27 SimpleBase() = default;
28 SimpleBase(const SimpleBase &) = default;
29};
30
32
34
35void wrap(py::module m) {
36 py::class_<SimpleBase, SimpleBaseTrampoline>(m, "SimpleBase")
37 .def(py::init<>())
38 .def_readwrite("num", &SimpleBase::num)
39 .def(py::pickle(
40 [](const py::object &self) {
41 py::dict d;
42 if (py::hasattr(self, "__dict__")) {
43 d = self.attr("__dict__");
44 }
45 return py::make_tuple(self.attr("num"), d);
46 },
47 [](const py::tuple &t) {
48 if (t.size() != 2) {
49 throw std::runtime_error("Invalid state!");
50 }
51 auto cpp_state = std::unique_ptr<SimpleBase>(new SimpleBaseTrampoline);
52 cpp_state->num = t[0].cast<int>();
53 auto py_state = t[1].cast<py::dict>();
54 return std::make_pair(std::move(cpp_state), py_state);
55 }));
56
57 m.def("make_SimpleCppDerivedAsBase",
58 []() { return std::unique_ptr<SimpleBase>(new SimpleCppDerived); });
59 m.def("check_dynamic_cast_SimpleCppDerived", [](const SimpleBase *base_ptr) {
60 return dynamic_cast<const SimpleCppDerived *>(base_ptr) != nullptr;
61 });
62}
63
64} // namespace exercise_trampoline
65
66// clang-format off
67
68TEST_SUBMODULE(pickling, m) {
69 // test_roundtrip
70 class Pickleable {
71 public:
72 explicit Pickleable(const std::string &value) : m_value(value) { }
73 const std::string &value() const { return m_value; }
74
75 void setExtra1(int extra1) { m_extra1 = extra1; }
76 void setExtra2(int extra2) { m_extra2 = extra2; }
77 int extra1() const { return m_extra1; }
78 int extra2() const { return m_extra2; }
79 private:
80 std::string m_value;
81 int m_extra1 = 0;
82 int m_extra2 = 0;
83 };
84
85 class PickleableNew : public Pickleable {
86 public:
87 using Pickleable::Pickleable;
88 };
89
90 py::class_<Pickleable> pyPickleable(m, "Pickleable");
91 pyPickleable
92 .def(py::init<std::string>())
93 .def("value", &Pickleable::value)
94 .def("extra1", &Pickleable::extra1)
95 .def("extra2", &Pickleable::extra2)
96 .def("setExtra1", &Pickleable::setExtra1)
97 .def("setExtra2", &Pickleable::setExtra2)
98 // For details on the methods below, refer to
99 // http://docs.python.org/3/library/pickle.html#pickling-class-instances
100 .def("__getstate__", [](const Pickleable &p) {
101 /* Return a tuple that fully encodes the state of the object */
102 return py::make_tuple(p.value(), p.extra1(), p.extra2());
103 });
104 ignoreOldStyleInitWarnings([&pyPickleable]() {
105 pyPickleable.def("__setstate__", [](Pickleable &p, const py::tuple &t) {
106 if (t.size() != 3) {
107 throw std::runtime_error("Invalid state!");
108}
109 /* Invoke the constructor (need to use in-place version) */
110 new (&p) Pickleable(t[0].cast<std::string>());
111
112 /* Assign any additional state */
113 p.setExtra1(t[1].cast<int>());
114 p.setExtra2(t[2].cast<int>());
115 });
116 });
117
118 py::class_<PickleableNew, Pickleable>(m, "PickleableNew")
119 .def(py::init<std::string>())
120 .def(py::pickle(
121 [](const PickleableNew &p) {
122 return py::make_tuple(p.value(), p.extra1(), p.extra2());
123 },
124 [](const py::tuple &t) {
125 if (t.size() != 3) {
126 throw std::runtime_error("Invalid state!");
127}
128 auto p = PickleableNew(t[0].cast<std::string>());
129
130 p.setExtra1(t[1].cast<int>());
131 p.setExtra2(t[2].cast<int>());
132 return p;
133 }));
134
135#if !defined(PYPY_VERSION)
136 // test_roundtrip_with_dict
137 class PickleableWithDict {
138 public:
139 explicit PickleableWithDict(const std::string &value) : value(value) { }
140
141 std::string value;
142 int extra;
143 };
144
145 class PickleableWithDictNew : public PickleableWithDict {
146 public:
147 using PickleableWithDict::PickleableWithDict;
148 };
149
150 py::class_<PickleableWithDict> pyPickleableWithDict(m, "PickleableWithDict", py::dynamic_attr());
151 pyPickleableWithDict.def(py::init<std::string>())
152 .def_readwrite("value", &PickleableWithDict::value)
153 .def_readwrite("extra", &PickleableWithDict::extra)
154 .def("__getstate__", [](const py::object &self) {
155 /* Also include __dict__ in state */
156 return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__"));
157 });
158 ignoreOldStyleInitWarnings([&pyPickleableWithDict]() {
159 pyPickleableWithDict.def("__setstate__", [](const py::object &self, const py::tuple &t) {
160 if (t.size() != 3) {
161 throw std::runtime_error("Invalid state!");
162}
163 /* Cast and construct */
164 auto &p = self.cast<PickleableWithDict &>();
165 new (&p) PickleableWithDict(t[0].cast<std::string>());
166
167 /* Assign C++ state */
168 p.extra = t[1].cast<int>();
169
170 /* Assign Python state */
171 self.attr("__dict__") = t[2];
172 });
173 });
174
175 py::class_<PickleableWithDictNew, PickleableWithDict>(m, "PickleableWithDictNew")
176 .def(py::init<std::string>())
177 .def(py::pickle(
178 [](const py::object &self) {
179 return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__"));
180 },
181 [](const py::tuple &t) {
182 if (t.size() != 3) {
183 throw std::runtime_error("Invalid state!");
184}
185
186 auto cpp_state = PickleableWithDictNew(t[0].cast<std::string>());
187 cpp_state.extra = t[1].cast<int>();
188
189 auto py_state = t[2].cast<py::dict>();
190 return std::make_pair(cpp_state, py_state);
191 }));
192#endif
193
195}
void wrap(py::module m)
static const self_t self
Definition: operators.h:72
#define TEST_SUBMODULE(name, variable)
void ignoreOldStyleInitWarnings(F &&body)
SimpleBase(const SimpleBase &)=default