1- // clang-format off
21/*
32 tests/test_pickling.cpp -- pickle support
43
1110
1211#include " pybind11_tests.h"
1312
14- // clang-format on
15-
1613#include < memory>
1714#include < stdexcept>
1815#include < utility>
@@ -63,19 +60,18 @@ void wrap(py::module m) {
6360
6461} // namespace exercise_trampoline
6562
66- // clang-format off
67-
6863TEST_SUBMODULE (pickling, m) {
6964 // test_roundtrip
7065 class Pickleable {
7166 public:
72- explicit Pickleable (const std::string &value) : m_value(value) { }
67+ explicit Pickleable (const std::string &value) : m_value(value) {}
7368 const std::string &value () const { return m_value; }
7469
7570 void setExtra1 (int extra1) { m_extra1 = extra1; }
7671 void setExtra2 (int extra2) { m_extra2 = extra2; }
7772 int extra1 () const { return m_extra1; }
7873 int extra2 () const { return m_extra2; }
74+
7975 private:
8076 std::string m_value;
8177 int m_extra1 = 0 ;
@@ -88,8 +84,7 @@ TEST_SUBMODULE(pickling, m) {
8884 };
8985
9086 py::class_<Pickleable> pyPickleable (m, " Pickleable" );
91- pyPickleable
92- .def (py::init<std::string>())
87+ pyPickleable.def (py::init<std::string>())
9388 .def (" value" , &Pickleable::value)
9489 .def (" extra1" , &Pickleable::extra1)
9590 .def (" extra2" , &Pickleable::extra2)
@@ -105,7 +100,7 @@ TEST_SUBMODULE(pickling, m) {
105100 pyPickleable.def (" __setstate__" , [](Pickleable &p, const py::tuple &t) {
106101 if (t.size () != 3 ) {
107102 throw std::runtime_error (" Invalid state!" );
108- }
103+ }
109104 /* Invoke the constructor (need to use in-place version) */
110105 new (&p) Pickleable (t[0 ].cast <std::string>());
111106
@@ -124,7 +119,7 @@ TEST_SUBMODULE(pickling, m) {
124119 [](const py::tuple &t) {
125120 if (t.size () != 3 ) {
126121 throw std::runtime_error (" Invalid state!" );
127- }
122+ }
128123 auto p = PickleableNew (t[0 ].cast <std::string>());
129124
130125 p.setExtra1 (t[1 ].cast <int >());
@@ -136,7 +131,7 @@ TEST_SUBMODULE(pickling, m) {
136131 // test_roundtrip_with_dict
137132 class PickleableWithDict {
138133 public:
139- explicit PickleableWithDict (const std::string &value) : value(value) { }
134+ explicit PickleableWithDict (const std::string &value) : value(value) {}
140135
141136 std::string value;
142137 int extra;
@@ -147,7 +142,8 @@ TEST_SUBMODULE(pickling, m) {
147142 using PickleableWithDict::PickleableWithDict;
148143 };
149144
150- py::class_<PickleableWithDict> pyPickleableWithDict (m, " PickleableWithDict" , py::dynamic_attr ());
145+ py::class_<PickleableWithDict> pyPickleableWithDict (
146+ m, " PickleableWithDict" , py::dynamic_attr ());
151147 pyPickleableWithDict.def (py::init<std::string>())
152148 .def_readwrite (" value" , &PickleableWithDict::value)
153149 .def_readwrite (" extra" , &PickleableWithDict::extra)
@@ -159,7 +155,7 @@ TEST_SUBMODULE(pickling, m) {
159155 pyPickleableWithDict.def (" __setstate__" , [](const py::object &self, const py::tuple &t) {
160156 if (t.size () != 3 ) {
161157 throw std::runtime_error (" Invalid state!" );
162- }
158+ }
163159 /* Cast and construct */
164160 auto &p = self.cast <PickleableWithDict &>();
165161 new (&p) PickleableWithDict (t[0 ].cast <std::string>());
@@ -176,12 +172,13 @@ TEST_SUBMODULE(pickling, m) {
176172 .def (py::init<std::string>())
177173 .def (py::pickle (
178174 [](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__" ));
180177 },
181178 [](const py::tuple &t) {
182179 if (t.size () != 3 ) {
183180 throw std::runtime_error (" Invalid state!" );
184- }
181+ }
185182
186183 auto cpp_state = PickleableWithDictNew (t[0 ].cast <std::string>());
187184 cpp_state.extra = t[1 ].cast <int >();
0 commit comments