test_pickling.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. tests/test_pickling.cpp -- pickle support
  3. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. TEST_SUBMODULE(pickling, m) {
  9. // test_roundtrip
  10. class Pickleable {
  11. public:
  12. Pickleable(const std::string &value) : m_value(value) { }
  13. const std::string &value() const { return m_value; }
  14. void setExtra1(int extra1) { m_extra1 = extra1; }
  15. void setExtra2(int extra2) { m_extra2 = extra2; }
  16. int extra1() const { return m_extra1; }
  17. int extra2() const { return m_extra2; }
  18. private:
  19. std::string m_value;
  20. int m_extra1 = 0;
  21. int m_extra2 = 0;
  22. };
  23. class PickleableNew : public Pickleable {
  24. public:
  25. using Pickleable::Pickleable;
  26. };
  27. py::class_<Pickleable>(m, "Pickleable")
  28. .def(py::init<std::string>())
  29. .def("value", &Pickleable::value)
  30. .def("extra1", &Pickleable::extra1)
  31. .def("extra2", &Pickleable::extra2)
  32. .def("setExtra1", &Pickleable::setExtra1)
  33. .def("setExtra2", &Pickleable::setExtra2)
  34. // For details on the methods below, refer to
  35. // http://docs.python.org/3/library/pickle.html#pickling-class-instances
  36. .def("__getstate__", [](const Pickleable &p) {
  37. /* Return a tuple that fully encodes the state of the object */
  38. return py::make_tuple(p.value(), p.extra1(), p.extra2());
  39. })
  40. .def("__setstate__", [](Pickleable &p, py::tuple t) {
  41. if (t.size() != 3)
  42. throw std::runtime_error("Invalid state!");
  43. /* Invoke the constructor (need to use in-place version) */
  44. new (&p) Pickleable(t[0].cast<std::string>());
  45. /* Assign any additional state */
  46. p.setExtra1(t[1].cast<int>());
  47. p.setExtra2(t[2].cast<int>());
  48. });
  49. py::class_<PickleableNew, Pickleable>(m, "PickleableNew")
  50. .def(py::init<std::string>())
  51. .def(py::pickle(
  52. [](const PickleableNew &p) {
  53. return py::make_tuple(p.value(), p.extra1(), p.extra2());
  54. },
  55. [](py::tuple t) {
  56. if (t.size() != 3)
  57. throw std::runtime_error("Invalid state!");
  58. auto p = PickleableNew(t[0].cast<std::string>());
  59. p.setExtra1(t[1].cast<int>());
  60. p.setExtra2(t[2].cast<int>());
  61. return p;
  62. }
  63. ));
  64. #if !defined(PYPY_VERSION)
  65. // test_roundtrip_with_dict
  66. class PickleableWithDict {
  67. public:
  68. PickleableWithDict(const std::string &value) : value(value) { }
  69. std::string value;
  70. int extra;
  71. };
  72. class PickleableWithDictNew : public PickleableWithDict {
  73. public:
  74. using PickleableWithDict::PickleableWithDict;
  75. };
  76. py::class_<PickleableWithDict>(m, "PickleableWithDict", py::dynamic_attr())
  77. .def(py::init<std::string>())
  78. .def_readwrite("value", &PickleableWithDict::value)
  79. .def_readwrite("extra", &PickleableWithDict::extra)
  80. .def("__getstate__", [](py::object self) {
  81. /* Also include __dict__ in state */
  82. return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__"));
  83. })
  84. .def("__setstate__", [](py::object self, py::tuple t) {
  85. if (t.size() != 3)
  86. throw std::runtime_error("Invalid state!");
  87. /* Cast and construct */
  88. auto& p = self.cast<PickleableWithDict&>();
  89. new (&p) PickleableWithDict(t[0].cast<std::string>());
  90. /* Assign C++ state */
  91. p.extra = t[1].cast<int>();
  92. /* Assign Python state */
  93. self.attr("__dict__") = t[2];
  94. });
  95. py::class_<PickleableWithDictNew, PickleableWithDict>(m, "PickleableWithDictNew")
  96. .def(py::init<std::string>())
  97. .def(py::pickle(
  98. [](py::object self) {
  99. return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__"));
  100. },
  101. [](const py::tuple &t) {
  102. if (t.size() != 3)
  103. throw std::runtime_error("Invalid state!");
  104. auto cpp_state = PickleableWithDictNew(t[0].cast<std::string>());
  105. cpp_state.extra = t[1].cast<int>();
  106. auto py_state = t[2].cast<py::dict>();
  107. return std::make_pair(cpp_state, py_state);
  108. }
  109. ));
  110. #endif
  111. }