1
- // clang-format off
2
1
/*
3
2
tests/test_pickling.cpp -- pickle support
4
3
11
10
12
11
#include " pybind11_tests.h"
13
12
14
- // clang-format on
15
-
16
13
#include < memory>
17
14
#include < stdexcept>
18
15
#include < utility>
@@ -63,19 +60,18 @@ void wrap(py::module m) {
63
60
64
61
} // namespace exercise_trampoline
65
62
66
- // clang-format off
67
-
68
63
TEST_SUBMODULE (pickling, m) {
69
64
// test_roundtrip
70
65
class Pickleable {
71
66
public:
72
- explicit Pickleable (const std::string &value) : m_value(value) { }
67
+ explicit Pickleable (const std::string &value) : m_value(value) {}
73
68
const std::string &value () const { return m_value; }
74
69
75
70
void setExtra1 (int extra1) { m_extra1 = extra1; }
76
71
void setExtra2 (int extra2) { m_extra2 = extra2; }
77
72
int extra1 () const { return m_extra1; }
78
73
int extra2 () const { return m_extra2; }
74
+
79
75
private:
80
76
std::string m_value;
81
77
int m_extra1 = 0 ;
@@ -88,8 +84,7 @@ TEST_SUBMODULE(pickling, m) {
88
84
};
89
85
90
86
py::class_<Pickleable> pyPickleable (m, " Pickleable" );
91
- pyPickleable
92
- .def (py::init<std::string>())
87
+ pyPickleable.def (py::init<std::string>())
93
88
.def (" value" , &Pickleable::value)
94
89
.def (" extra1" , &Pickleable::extra1)
95
90
.def (" extra2" , &Pickleable::extra2)
@@ -105,7 +100,7 @@ TEST_SUBMODULE(pickling, m) {
105
100
pyPickleable.def (" __setstate__" , [](Pickleable &p, const py::tuple &t) {
106
101
if (t.size () != 3 ) {
107
102
throw std::runtime_error (" Invalid state!" );
108
- }
103
+ }
109
104
/* Invoke the constructor (need to use in-place version) */
110
105
new (&p) Pickleable (t[0 ].cast <std::string>());
111
106
@@ -124,7 +119,7 @@ TEST_SUBMODULE(pickling, m) {
124
119
[](const py::tuple &t) {
125
120
if (t.size () != 3 ) {
126
121
throw std::runtime_error (" Invalid state!" );
127
- }
122
+ }
128
123
auto p = PickleableNew (t[0 ].cast <std::string>());
129
124
130
125
p.setExtra1 (t[1 ].cast <int >());
@@ -136,7 +131,7 @@ TEST_SUBMODULE(pickling, m) {
136
131
// test_roundtrip_with_dict
137
132
class PickleableWithDict {
138
133
public:
139
- explicit PickleableWithDict (const std::string &value) : value(value) { }
134
+ explicit PickleableWithDict (const std::string &value) : value(value) {}
140
135
141
136
std::string value;
142
137
int extra;
@@ -147,7 +142,8 @@ TEST_SUBMODULE(pickling, m) {
147
142
using PickleableWithDict::PickleableWithDict;
148
143
};
149
144
150
- py::class_<PickleableWithDict> pyPickleableWithDict (m, " PickleableWithDict" , py::dynamic_attr ());
145
+ py::class_<PickleableWithDict> pyPickleableWithDict (
146
+ m, " PickleableWithDict" , py::dynamic_attr ());
151
147
pyPickleableWithDict.def (py::init<std::string>())
152
148
.def_readwrite (" value" , &PickleableWithDict::value)
153
149
.def_readwrite (" extra" , &PickleableWithDict::extra)
@@ -159,7 +155,7 @@ TEST_SUBMODULE(pickling, m) {
159
155
pyPickleableWithDict.def (" __setstate__" , [](const py::object &self, const py::tuple &t) {
160
156
if (t.size () != 3 ) {
161
157
throw std::runtime_error (" Invalid state!" );
162
- }
158
+ }
163
159
/* Cast and construct */
164
160
auto &p = self.cast <PickleableWithDict &>();
165
161
new (&p) PickleableWithDict (t[0 ].cast <std::string>());
@@ -176,12 +172,13 @@ TEST_SUBMODULE(pickling, m) {
176
172
.def (py::init<std::string>())
177
173
.def (py::pickle (
178
174
[](const py::object &self) {
179
- return py::make_tuple (self.attr (" value" ), self.attr (" extra" ), self.attr (" __dict__" ));
175
+ return py::make_tuple (
176
+ self.attr (" value" ), self.attr (" extra" ), self.attr (" __dict__" ));
180
177
},
181
178
[](const py::tuple &t) {
182
179
if (t.size () != 3 ) {
183
180
throw std::runtime_error (" Invalid state!" );
184
- }
181
+ }
185
182
186
183
auto cpp_state = PickleableWithDictNew (t[0 ].cast <std::string>());
187
184
cpp_state.extra = t[1 ].cast <int >();
0 commit comments