File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -530,6 +530,10 @@ namespace objects
530530 if (scope ().ptr () != Py_None)
531531 scope ().attr (name) = result;
532532
533+ // For pickle. Will lead to informative error messages if pickling
534+ // is not enabled.
535+ result.attr (" __reduce__" ) = object (make_instance_reduce_function ());
536+
533537 return result;
534538 }
535539 }
@@ -627,7 +631,6 @@ namespace objects
627631
628632 void class_base::enable_pickling_ (bool getstate_manages_dict)
629633 {
630- setattr (" __reduce__" , object (make_instance_reduce_function ()));
631634 setattr (" __safe_for_unpickling__" , object (true ));
632635
633636 if (getstate_manages_dict)
Original file line number Diff line number Diff line change 88#include < boost/python/tuple.hpp>
99#include < boost/python/list.hpp>
1010#include < boost/python/dict.hpp>
11+ #include < boost/python/str.hpp>
1112
1213namespace boost { namespace python {
1314
@@ -19,6 +20,22 @@ namespace {
1920 object instance_class (instance_obj.attr (" __class__" ));
2021 result.append (instance_class);
2122 object none;
23+ if (!getattr (instance_obj, " __safe_for_unpickling__" , none))
24+ {
25+ str type_name (getattr (instance_class, " __name__" ));
26+ str module_name (getattr (instance_class, " __module__" , " " ));
27+ if (module_name)
28+ module_name += " ." ;
29+
30+ PyErr_SetObject (
31+ PyExc_RuntimeError,
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 ()
35+ );
36+
37+ throw_error_already_set ();
38+ }
2239 object getinitargs = getattr (instance_obj, " __getinitargs__" , none);
2340 tuple initargs;
2441 if (getinitargs.ptr () != none.ptr ()) {
Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ namespace {
4545 }
4646 };
4747
48+ // To support test of "pickling not enabled" error message.
49+ struct noop {};
4850}
4951
5052BOOST_PYTHON_MODULE (pickle1_ext)
@@ -54,4 +56,7 @@ BOOST_PYTHON_MODULE(pickle1_ext)
5456 .def (" greet" , &world::greet)
5557 .def_pickle (world_pickle_suite ())
5658 ;
59+
60+ // To support test of "pickling not enabled" error message.
61+ class_<noop>(" noop" );
5762}
Original file line number Diff line number Diff line change 1818 Hello from California!
1919 >>> print wl.greet()
2020 Hello from California!
21+
22+ >>> noop = pickle1_ext.noop()
23+ >>> try: pickle.dumps(noop)
24+ ... except RuntimeError, e: print str(e)[:55]
25+ Pickling of "pickle1_ext.noop" instances is not enabled
2126'''
2227
2328def run (args = None ):
You can’t perform that action at this time.
0 commit comments