@@ -31,7 +31,8 @@ TEST_SUBMODULE(pickling, m) {
31
31
using Pickleable::Pickleable;
32
32
};
33
33
34
- py::class_<Pickleable>(m, " Pickleable" )
34
+ py::class_<Pickleable> pyPickleable (m, " Pickleable" );
35
+ pyPickleable
35
36
.def (py::init<std::string>())
36
37
.def (" value" , &Pickleable::value)
37
38
.def (" extra1" , &Pickleable::extra1)
@@ -43,17 +44,20 @@ TEST_SUBMODULE(pickling, m) {
43
44
.def (" __getstate__" , [](const Pickleable &p) {
44
45
/* Return a tuple that fully encodes the state of the object */
45
46
return py::make_tuple (p.value (), p.extra1 (), p.extra2 ());
46
- })
47
- .def (" __setstate__" , [](Pickleable &p, py::tuple t) {
48
- if (t.size () != 3 )
49
- throw std::runtime_error (" Invalid state!" );
50
- /* Invoke the constructor (need to use in-place version) */
51
- new (&p) Pickleable (t[0 ].cast <std::string>());
52
-
53
- /* Assign any additional state */
54
- p.setExtra1 (t[1 ].cast <int >());
55
- p.setExtra2 (t[2 ].cast <int >());
56
47
});
48
+ ignoreOldStyleInitWarnings ([&pyPickleable]() {
49
+ pyPickleable
50
+ .def (" __setstate__" , [](Pickleable &p, py::tuple t) {
51
+ if (t.size () != 3 )
52
+ throw std::runtime_error (" Invalid state!" );
53
+ /* Invoke the constructor (need to use in-place version) */
54
+ new (&p) Pickleable (t[0 ].cast <std::string>());
55
+
56
+ /* Assign any additional state */
57
+ p.setExtra1 (t[1 ].cast <int >());
58
+ p.setExtra2 (t[2 ].cast <int >());
59
+ });
60
+ });
57
61
58
62
py::class_<PickleableNew, Pickleable>(m, " PickleableNew" )
59
63
.def (py::init<std::string>())
@@ -87,27 +91,31 @@ TEST_SUBMODULE(pickling, m) {
87
91
using PickleableWithDict::PickleableWithDict;
88
92
};
89
93
90
- py::class_<PickleableWithDict>(m, " PickleableWithDict" , py::dynamic_attr ())
94
+ py::class_<PickleableWithDict> pyPickleableWithDict (m, " PickleableWithDict" , py::dynamic_attr ());
95
+ pyPickleableWithDict
91
96
.def (py::init<std::string>())
92
97
.def_readwrite (" value" , &PickleableWithDict::value)
93
98
.def_readwrite (" extra" , &PickleableWithDict::extra)
94
99
.def (" __getstate__" , [](py::object self) {
95
100
/* Also include __dict__ in state */
96
101
return py::make_tuple (self.attr (" value" ), self.attr (" extra" ), self.attr (" __dict__" ));
97
- })
98
- .def (" __setstate__" , [](py::object self, py::tuple t) {
99
- if (t.size () != 3 )
100
- throw std::runtime_error (" Invalid state!" );
101
- /* Cast and construct */
102
- auto & p = self.cast <PickleableWithDict&>();
103
- new (&p) PickleableWithDict (t[0 ].cast <std::string>());
104
-
105
- /* Assign C++ state */
106
- p.extra = t[1 ].cast <int >();
107
-
108
- /* Assign Python state */
109
- self.attr (" __dict__" ) = t[2 ];
110
102
});
103
+ ignoreOldStyleInitWarnings ([&pyPickleableWithDict]() {
104
+ pyPickleableWithDict
105
+ .def (" __setstate__" , [](py::object self, py::tuple t) {
106
+ if (t.size () != 3 )
107
+ throw std::runtime_error (" Invalid state!" );
108
+ /* Cast and construct */
109
+ auto & p = self.cast <PickleableWithDict&>();
110
+ new (&p) PickleableWithDict (t[0 ].cast <std::string>());
111
+
112
+ /* Assign C++ state */
113
+ p.extra = t[1 ].cast <int >();
114
+
115
+ /* Assign Python state */
116
+ self.attr (" __dict__" ) = t[2 ];
117
+ });
118
+ });
111
119
112
120
py::class_<PickleableWithDictNew, PickleableWithDict>(m, " PickleableWithDictNew" )
113
121
.def (py::init<std::string>())
0 commit comments