mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 16:32:16 +00:00
* Made Cygwin archiving reliable, even when the user supplies a path with backslashes ---------------------------------------------------------------------- Modified Files: tools/build/gcc-tools.jam tools/build/new/boost-build.jam boost/python/detail/config.hpp libs/python/build/Jamfile libs/python/example/do_it_yourself_convts.cpp libs/python/example/dvect.cpp libs/python/example/example1.cpp libs/python/example/getting_started1.cpp libs/python/example/getting_started2.cpp libs/python/example/ivect.cpp libs/python/example/nested.cpp libs/python/example/noncopyable_export.cpp libs/python/example/noncopyable_import.cpp libs/python/example/pickle1.cpp libs/python/example/pickle2.cpp libs/python/example/pickle3.cpp libs/python/example/richcmp1.cpp libs/python/example/richcmp2.cpp libs/python/example/richcmp3.cpp libs/python/example/rwgk1.cpp libs/python/example/simple_vector.cpp libs/python/test/comprehensive.cpp Added Files: libs/python/example/rwgk2.cpp libs/python/example/rwgk3.cpp ---------------------------------------------------------------------- [SVN r11705]
94 lines
3.0 KiB
C++
94 lines
3.0 KiB
C++
// Example by Ralf W. Grosse-Kunstleve
|
|
|
|
/*
|
|
This example shows how to make an Extension Class "pickleable".
|
|
|
|
The world class below contains member data (secret_number) that
|
|
cannot be restored by any of the constructors. Therefore it is
|
|
necessary to provide the __getstate__/__setstate__ pair of pickle
|
|
interface methods.
|
|
|
|
For simplicity, the __dict__ is not included in the result of
|
|
__getstate__. This is not generally recommended, but a valid
|
|
approach if it is anticipated that the object's __dict__ will
|
|
always be empty. Note that safety guard are provided to catch the
|
|
cases where this assumption is not true.
|
|
|
|
pickle3.cpp shows how to include the object's __dict__ in the
|
|
result of __getstate__.
|
|
|
|
For more information refer to boost/libs/python/doc/pickle.html.
|
|
*/
|
|
|
|
#include <string>
|
|
|
|
#include <boost/python/class_builder.hpp>
|
|
namespace python = boost::python;
|
|
|
|
namespace { // Avoid cluttering the global namespace.
|
|
|
|
// A friendly class.
|
|
class world
|
|
{
|
|
public:
|
|
world(const std::string& country) : secret_number(0) {
|
|
this->country = country;
|
|
}
|
|
std::string greet() const { return "Hello from " + country + "!"; }
|
|
std::string get_country() const { return country; }
|
|
void set_secret_number(int number) { secret_number = number; }
|
|
int get_secret_number() const { return secret_number; }
|
|
private:
|
|
std::string country;
|
|
int secret_number;
|
|
};
|
|
|
|
// Support for pickle.
|
|
|
|
using BOOST_PYTHON_CONVERSION::from_python;
|
|
|
|
python::ref world_getinitargs(const world& w) {
|
|
python::tuple result(1);
|
|
result.set_item(0, w.get_country());
|
|
return result.reference(); // returning the reference avoids the copying.
|
|
}
|
|
|
|
python::ref world_getstate(const world& w) {
|
|
python::tuple result(1);
|
|
result.set_item(0, w.get_secret_number());
|
|
return result.reference(); // returning the reference avoids the copying.
|
|
}
|
|
|
|
void world_setstate(world& w, python::tuple state) {
|
|
if (state.size() != 1) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"Unexpected argument in call to __setstate__.");
|
|
throw python::error_already_set();
|
|
}
|
|
int number = from_python(state[0].get(), python::type<int>());
|
|
if (number != 42)
|
|
w.set_secret_number(number);
|
|
}
|
|
}
|
|
|
|
BOOST_PYTHON_MODULE_INIT(pickle2)
|
|
{
|
|
// Create an object representing this extension module.
|
|
python::module_builder this_module("pickle2");
|
|
|
|
// Create the Python type object for our extension class.
|
|
python::class_builder<world> world_class(this_module, "world");
|
|
|
|
// Add the __init__ function.
|
|
world_class.def(python::constructor<std::string>());
|
|
// Add a regular member function.
|
|
world_class.def(&world::greet, "greet");
|
|
world_class.def(&world::get_secret_number, "get_secret_number");
|
|
world_class.def(&world::set_secret_number, "set_secret_number");
|
|
|
|
// Support for pickle.
|
|
world_class.def(world_getinitargs, "__getinitargs__");
|
|
world_class.def(world_getstate, "__getstate__");
|
|
world_class.def(world_setstate, "__setstate__");
|
|
}
|