diff --git a/doc/v2/class.html b/doc/v2/class.html
index d9105e0b..46c6320d 100644
--- a/doc/v2/class.html
+++ b/doc/v2/class.html
@@ -281,6 +281,7 @@ namespace boost { namespace python
// pickle support
template <typename PickleSuite>
self& def_pickle(PickleSuite const&);
+ self& enable_pickling();
};
}}
@@ -709,6 +710,23 @@ class_& def_pickle(PickleSuite const&);
checks.
+
+class_& enable_pickling(); ++ +
__reduce__ method and
+ the __safe_for_unpickling__ attribute.
+
+ *thisdef_pickle(). Enables implementation of
+ pickle support from Python.
+ class_<world>("world", args<const std::string&>())
+ // ...
+ .enable_pickling()
+ // ...
+
+
+This enables the standard Python pickle interface as described
+in the Python documentation. By "injecting" a
+__getinitargs__ method into the definition of the wrapped
+class we make all instances pickleable:
+
++ # import the wrapped world class + from pickle4_ext import world + + # definition of __getinitargs__ + def world_getinitargs(self): + return (self.get_country(),) + + # now inject __getinitargs__ (Python is a dynamic language!) + world.__getinitargs__ = world_getinitargs ++ +See also the +tutorial section on injecting additional methods from Python. + +
-Updated: Aug 2002.
+Updated: Feb 2004.
diff --git a/include/boost/python/class.hpp b/include/boost/python/class.hpp
index 4974ec86..31805a34 100644
--- a/include/boost/python/class.hpp
+++ b/include/boost/python/class.hpp
@@ -415,6 +415,12 @@ class class_ : public objects::class_base
return *this;
}
+ self& enable_pickling()
+ {
+ this->base::enable_pickling_(false);
+ return *this;
+ }
+
self& staticmethod(char const* name)
{
this->make_method_static(name);
diff --git a/include/boost/python/object/class.hpp b/include/boost/python/object/class.hpp
index 45bffbf7..22692011 100644
--- a/include/boost/python/object/class.hpp
+++ b/include/boost/python/object/class.hpp
@@ -32,7 +32,7 @@ struct BOOST_PYTHON_DECL class_base : python::api::object
// Implementation detail. Hiding this in the private section would
// require use of template friend declarations.
- void enable_pickling(bool getstate_manages_dict);
+ void enable_pickling_(bool getstate_manages_dict);
protected:
void add_property(char const* name, object const& fget);
diff --git a/include/boost/python/object/pickle_support.hpp b/include/boost/python/object/pickle_support.hpp
index cf1c5e99..ea81e8df 100644
--- a/include/boost/python/object/pickle_support.hpp
+++ b/include/boost/python/object/pickle_support.hpp
@@ -59,7 +59,7 @@ namespace detail {
inaccessible* (*setstate_fn)(),
bool)
{
- cl.enable_pickling(false);
+ cl.enable_pickling_(false);
cl.def("__getinitargs__", getinitargs_fn);
}
@@ -75,7 +75,7 @@ namespace detail {
void (*setstate_fn)(Tsetstate, Ttuple),
bool getstate_manages_dict)
{
- cl.enable_pickling(getstate_manages_dict);
+ cl.enable_pickling_(getstate_manages_dict);
cl.def("__getstate__", getstate_fn);
cl.def("__setstate__", setstate_fn);
}
@@ -93,7 +93,7 @@ namespace detail {
void (*setstate_fn)(Tsetstate, Ttuple),
bool getstate_manages_dict)
{
- cl.enable_pickling(getstate_manages_dict);
+ cl.enable_pickling_(getstate_manages_dict);
cl.def("__getinitargs__", getinitargs_fn);
cl.def("__getstate__", getstate_fn);
cl.def("__setstate__", setstate_fn);
diff --git a/src/object/class.cpp b/src/object/class.cpp
index ea667532..97bd7bbe 100644
--- a/src/object/class.cpp
+++ b/src/object/class.cpp
@@ -599,7 +599,7 @@ namespace objects
this->setattr("__init__", object(f));
}
- void class_base::enable_pickling(bool getstate_manages_dict)
+ void class_base::enable_pickling_(bool getstate_manages_dict)
{
setattr("__reduce__", object(make_instance_reduce_function()));
setattr("__safe_for_unpickling__", object(true));
diff --git a/test/Jamfile b/test/Jamfile
index a8a1c67b..87f8688a 100644
--- a/test/Jamfile
+++ b/test/Jamfile
@@ -151,6 +151,7 @@ bpl-test crossmod_exception
[ bpl-test pickle1 ]
[ bpl-test pickle2 ]
[ bpl-test pickle3 ]
+[ bpl-test pickle4 ]
[ bpl-test nested ]
diff --git a/test/pickle4.cpp b/test/pickle4.cpp
new file mode 100644
index 00000000..21fe9619
--- /dev/null
+++ b/test/pickle4.cpp
@@ -0,0 +1,41 @@
+// Example by Ralf W. Grosse-Kunstleve
+
+/*
+ This example shows how to enable pickling without using the
+ pickle_suite. The pickling interface (__getinitargs__) is
+ implemented in Python.
+
+ For more information refer to boost/libs/python/doc/pickle.html.
+ */
+
+#include