@@ -31,7 +31,8 @@ TEST_SUBMODULE(pickling, m) {
3131 using Pickleable::Pickleable;
3232 };
3333
34- py::class_<Pickleable>(m, " Pickleable" )
34+ py::class_<Pickleable> pyPickleable (m, " Pickleable" );
35+ pyPickleable
3536 .def (py::init<std::string>())
3637 .def (" value" , &Pickleable::value)
3738 .def (" extra1" , &Pickleable::extra1)
@@ -43,17 +44,20 @@ TEST_SUBMODULE(pickling, m) {
4344 .def (" __getstate__" , [](const Pickleable &p) {
4445 /* Return a tuple that fully encodes the state of the object */
4546 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 >());
5647 });
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+ });
5761
5862 py::class_<PickleableNew, Pickleable>(m, " PickleableNew" )
5963 .def (py::init<std::string>())
@@ -87,27 +91,31 @@ TEST_SUBMODULE(pickling, m) {
8791 using PickleableWithDict::PickleableWithDict;
8892 };
8993
90- py::class_<PickleableWithDict>(m, " PickleableWithDict" , py::dynamic_attr ())
94+ py::class_<PickleableWithDict> pyPickleableWithDict (m, " PickleableWithDict" , py::dynamic_attr ());
95+ pyPickleableWithDict
9196 .def (py::init<std::string>())
9297 .def_readwrite (" value" , &PickleableWithDict::value)
9398 .def_readwrite (" extra" , &PickleableWithDict::extra)
9499 .def (" __getstate__" , [](py::object self) {
95100 /* Also include __dict__ in state */
96101 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 ];
110102 });
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+ });
111119
112120 py::class_<PickleableWithDictNew, PickleableWithDict>(m, " PickleableWithDictNew" )
113121 .def (py::init<std::string>())
0 commit comments