pickle3.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost
  2. // Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. /*
  5. This example shows how to make an Extension Class "pickleable".
  6. The world class below contains member data (secret_number) that
  7. cannot be restored by any of the constructors. Therefore it is
  8. necessary to provide the __getstate__/__setstate__ pair of pickle
  9. interface methods.
  10. The object's __dict__ is included in the result of __getstate__.
  11. This requires more code (compare with pickle2.cpp), but is
  12. unavoidable if the object's __dict__ is not always empty.
  13. For more information refer to boost/libs/python/doc/pickle.html.
  14. */
  15. #include <boost/python/module.hpp>
  16. #include <boost/python/def.hpp>
  17. #include <boost/python/class.hpp>
  18. #include <boost/python/tuple.hpp>
  19. #include <boost/python/dict.hpp>
  20. #include <boost/python/extract.hpp>
  21. #include <boost/python/back_reference.hpp>
  22. namespace boost_python_test {
  23. // A friendly class.
  24. class world
  25. {
  26. public:
  27. world(const std::string& _country) : secret_number(0) {
  28. this->country = _country;
  29. }
  30. std::string greet() const { return "Hello from " + country + "!"; }
  31. std::string get_country() const { return country; }
  32. void set_secret_number(int number) { secret_number = number; }
  33. int get_secret_number() const { return secret_number; }
  34. private:
  35. std::string country;
  36. int secret_number;
  37. };
  38. struct world_pickle_suite : boost::python::pickle_suite
  39. {
  40. static
  41. boost::python::tuple
  42. getinitargs(const world& w)
  43. {
  44. return boost::python::make_tuple(w.get_country());
  45. }
  46. static
  47. boost::python::tuple
  48. getstate(boost::python::object w_obj)
  49. {
  50. world const& w = boost::python::extract<world const&>(w_obj)();
  51. return boost::python::make_tuple(
  52. w_obj.attr("__dict__"),
  53. w.get_secret_number());
  54. }
  55. static
  56. void
  57. setstate(boost::python::object w_obj, boost::python::tuple state)
  58. {
  59. using namespace boost::python;
  60. world& w = extract<world&>(w_obj)();
  61. if (len(state) != 2)
  62. {
  63. PyErr_SetObject(PyExc_ValueError,
  64. ("expected 2-item tuple in call to __setstate__; got %s"
  65. % state).ptr()
  66. );
  67. throw_error_already_set();
  68. }
  69. // restore the object's __dict__
  70. dict d = extract<dict>(w_obj.attr("__dict__"))();
  71. d.update(state[0]);
  72. // restore the internal state of the C++ object
  73. long number = extract<long>(state[1]);
  74. if (number != 42)
  75. w.set_secret_number(number);
  76. }
  77. static bool getstate_manages_dict() { return true; }
  78. };
  79. }
  80. BOOST_PYTHON_MODULE(pickle3_ext)
  81. {
  82. using namespace boost_python_test;
  83. boost::python::class_<world>(
  84. "world", boost::python::init<const std::string&>())
  85. .def("greet", &world::greet)
  86. .def("get_secret_number", &world::get_secret_number)
  87. .def("set_secret_number", &world::set_secret_number)
  88. .def_pickle(world_pickle_suite())
  89. ;
  90. }