test_pickling.cpp (11986:c12e4625ab56) | test_pickling.cpp (12037:d28054ac6ec9) |
---|---|
1/* 2 tests/test_pickling.cpp -- pickle support 3 4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 5 6 All rights reserved. Use of this source code is governed by a 7 BSD-style license that can be found in the LICENSE file. 8*/ --- 43 unchanged lines hidden (view full) --- 52 /* Invoke the constructor (need to use in-place version) */ 53 new (&p) Pickleable(t[0].cast<std::string>()); 54 55 /* Assign any additional state */ 56 p.setExtra1(t[1].cast<int>()); 57 p.setExtra2(t[2].cast<int>()); 58 }); 59 | 1/* 2 tests/test_pickling.cpp -- pickle support 3 4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 5 6 All rights reserved. Use of this source code is governed by a 7 BSD-style license that can be found in the LICENSE file. 8*/ --- 43 unchanged lines hidden (view full) --- 52 /* Invoke the constructor (need to use in-place version) */ 53 new (&p) Pickleable(t[0].cast<std::string>()); 54 55 /* Assign any additional state */ 56 p.setExtra1(t[1].cast<int>()); 57 p.setExtra2(t[2].cast<int>()); 58 }); 59 |
60#if !defined(PYPY_VERSION) |
|
60 py::class_<PickleableWithDict>(m, "PickleableWithDict", py::dynamic_attr()) 61 .def(py::init<std::string>()) 62 .def_readwrite("value", &PickleableWithDict::value) 63 .def_readwrite("extra", &PickleableWithDict::extra) 64 .def("__getstate__", [](py::object self) { 65 /* Also include __dict__ in state */ 66 return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__")); 67 }) 68 .def("__setstate__", [](py::object self, py::tuple t) { 69 if (t.size() != 3) 70 throw std::runtime_error("Invalid state!"); 71 /* Cast and construct */ 72 auto& p = self.cast<PickleableWithDict&>(); | 61 py::class_<PickleableWithDict>(m, "PickleableWithDict", py::dynamic_attr()) 62 .def(py::init<std::string>()) 63 .def_readwrite("value", &PickleableWithDict::value) 64 .def_readwrite("extra", &PickleableWithDict::extra) 65 .def("__getstate__", [](py::object self) { 66 /* Also include __dict__ in state */ 67 return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__")); 68 }) 69 .def("__setstate__", [](py::object self, py::tuple t) { 70 if (t.size() != 3) 71 throw std::runtime_error("Invalid state!"); 72 /* Cast and construct */ 73 auto& p = self.cast<PickleableWithDict&>(); |
73 new (&p) Pickleable(t[0].cast | 74 new (&p) PickleableWithDict(t[0].cast<std::string>()); |
74 75 /* Assign C++ state */ 76 p.extra = t[1].cast<int>(); 77 78 /* Assign Python state */ 79 self.attr("__dict__") = t[2]; 80 }); | 75 76 /* Assign C++ state */ 77 p.extra = t[1].cast<int>(); 78 79 /* Assign Python state */ 80 self.attr("__dict__") = t[2]; 81 }); |
82#endif |
|
81}); | 83}); |