mirror of
https://github.com/boostorg/python.git
synced 2026-01-20 04:42:28 +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]
144 lines
4.9 KiB
C++
144 lines
4.9 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.
|
|
|
|
The object's __dict__ is included in the result of __getstate__.
|
|
This requires more code (compare with pickle2.cpp), but is
|
|
unavoidable if the object's __dict__ is not always empty.
|
|
|
|
For more information refer to boost/libs/python/doc/pickle.html.
|
|
*/
|
|
|
|
#include <string>
|
|
|
|
#include <boost/python/class_builder.hpp>
|
|
namespace python = boost::python;
|
|
|
|
namespace boost { namespace python {
|
|
|
|
ref getattr(PyObject* o, const std::string& attr_name) {
|
|
return ref(PyObject_GetAttrString(o, const_cast<char*>(attr_name.c_str())));
|
|
}
|
|
ref getattr(const ref& r, const std::string& attr_name) {
|
|
return getattr(r.get(), attr_name);
|
|
}
|
|
|
|
}}
|
|
|
|
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.
|
|
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(python::tuple const & args,
|
|
python::dictionary const & keywords);
|
|
|
|
PyObject* world_setstate(python::tuple const & args,
|
|
python::dictionary const & keywords);
|
|
}
|
|
|
|
BOOST_PYTHON_MODULE_INIT(pickle3)
|
|
{
|
|
// Create an object representing this extension module.
|
|
python::module_builder this_module("pickle3");
|
|
|
|
// 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_raw(world_getstate, "__getstate__");
|
|
world_class.def_raw(world_setstate, "__setstate__");
|
|
world_class.getstate_manages_dict();
|
|
}
|
|
|
|
namespace {
|
|
|
|
using BOOST_PYTHON_CONVERSION::from_python;
|
|
using boost::python::type;
|
|
using boost::python::ref;
|
|
using boost::python::tuple;
|
|
using boost::python::list;
|
|
using boost::python::dictionary;
|
|
using boost::python::getattr;
|
|
|
|
ref world_getstate(tuple const & args, dictionary const & keywords)
|
|
{
|
|
if(args.size() != 1 || keywords.size() != 0) {
|
|
PyErr_SetString(PyExc_TypeError, "wrong number of arguments");
|
|
throw boost::python::error_already_set();
|
|
}
|
|
const world& w = from_python(args[0].get(), type<const world&>());
|
|
ref mydict = getattr(args[0], "__dict__");
|
|
tuple result(2);
|
|
// store the object's __dict__
|
|
result.set_item(0, mydict);
|
|
// store the internal state of the C++ object
|
|
result.set_item(1, w.get_secret_number());
|
|
return result.reference(); // returning the reference avoids the copying.
|
|
}
|
|
|
|
PyObject* world_setstate(tuple const & args, dictionary const & keywords)
|
|
{
|
|
if(args.size() != 2 || keywords.size() != 0) {
|
|
PyErr_SetString(PyExc_TypeError, "wrong number of arguments");
|
|
throw boost::python::error_already_set();
|
|
}
|
|
world& w = from_python(args[0].get(), type<world&>());
|
|
ref mydict = getattr(args[0], "__dict__");
|
|
tuple state = from_python(args[1].get(), type<tuple>());
|
|
if (state.size() != 2) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"Unexpected argument in call to __setstate__.");
|
|
throw python::error_already_set();
|
|
}
|
|
// restore the object's __dict__
|
|
dictionary odict = from_python(mydict.get(), type<dictionary>());
|
|
const dictionary& pdict = from_python(state[0].get(), type<const dictionary&>());
|
|
list pkeys(pdict.keys());
|
|
for (int i = 0; i < pkeys.size(); i++) {
|
|
ref k(pkeys[i]);
|
|
//odict[k] = pdict[k]; // XXX memory leak!
|
|
odict[k] = pdict.get_item(k); // this does not leak.
|
|
}
|
|
// restore the internal state of the C++ object
|
|
int number = from_python(state[1].get(), type<int>());
|
|
if (number != 42)
|
|
w.set_secret_number(number);
|
|
return python::detail::none();
|
|
}
|
|
}
|