1 // (C) Copyright R.W. Grosse-Kunstleve 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/python/make_function.hpp>
7 #include <boost/python/object/class.hpp>
8 #include <boost/python/tuple.hpp>
9 #include <boost/python/list.hpp>
10 #include <boost/python/dict.hpp>
11 #include <boost/python/str.hpp>
13 namespace boost
{ namespace python
{
17 tuple
instance_reduce(object instance_obj
)
20 object
instance_class(instance_obj
.attr("__class__"));
21 result
.append(instance_class
);
23 if (!getattr(instance_obj
, "__safe_for_unpickling__", none
))
25 str
type_name(getattr(instance_class
, "__name__"));
26 str
module_name(getattr(instance_class
, "__module__", object("")));
32 ( "Pickling of \"%s\" instances is not enabled"
33 " (http://www.boost.org/libs/python/doc/v2/pickle.html)"
34 % (module_name
+type_name
)).ptr()
37 throw_error_already_set();
39 object getinitargs
= getattr(instance_obj
, "__getinitargs__", none
);
41 if (!getinitargs
.is_none()) {
42 initargs
= tuple(getinitargs());
44 result
.append(initargs
);
45 object getstate
= getattr(instance_obj
, "__getstate__", none
);
46 object instance_dict
= getattr(instance_obj
, "__dict__", none
);
47 long len_instance_dict
= 0;
48 if (!instance_dict
.is_none()) {
49 len_instance_dict
= len(instance_dict
);
51 if (!getstate
.is_none()) {
52 if (len_instance_dict
> 0) {
53 object getstate_manages_dict
= getattr(
54 instance_obj
, "__getstate_manages_dict__", none
);
55 if (getstate_manages_dict
.is_none()) {
56 PyErr_SetString(PyExc_RuntimeError
,
57 "Incomplete pickle support"
58 " (__getstate_manages_dict__ not set)");
59 throw_error_already_set();
62 result
.append(getstate());
64 else if (len_instance_dict
> 0) {
65 result
.append(instance_dict
);
72 object
const& make_instance_reduce_function()
74 static object
result(&instance_reduce
);
78 }} // namespace boost::python