1 %module python_pickle 2 3 4 %include <std_string.i> 5 6 %extend PickleMe { 7 #if 0 8 // Note: %pythoncode can't be used with -builtin 9 %pythoncode %{ 10 def __reduce__(self): 11 print "In Python __reduce__" 12 return (type(self), (self.msg, )) 13 %} 14 #else 15 // Equivalent to Python code above __reduce__()16 PyObject *__reduce__() { 17 if (debug) 18 std::cout << "In C++ __reduce__" << std::endl; 19 PyObject *args = PyTuple_New(1); 20 PyTuple_SetItem(args, 0, SWIG_From_std_string(self->msg)); 21 22 swig_type_info *ty = SWIGTYPE_p_PickleMe; 23 SwigPyClientData *data = (SwigPyClientData *)ty->clientdata; 24 #if defined(SWIGPYTHON_BUILTIN) 25 PyObject *callable = (PyObject *)data->pytype; 26 #else 27 PyObject *callable = data->klass; 28 #endif 29 Py_INCREF(callable); 30 31 PyObject *ret = PyTuple_New(2); 32 PyTuple_SetItem(ret, 0, callable); 33 PyTuple_SetItem(ret, 1, args); 34 return ret; 35 } 36 #endif 37 } 38 39 %inline %{ 40 #include <iostream> 41 42 bool debug = false; 43 44 struct PickleMe { 45 std::string msg; PickleMePickleMe46 PickleMe(const std::string& msg) : msg(msg) { 47 if (debug) 48 std::cout << "In C++ constructor " << " [" << msg << "]" << std::endl; 49 } 50 }; 51 52 struct NotForPickling { 53 std::string msg; NotForPicklingNotForPickling54 NotForPickling(const std::string& msg) : msg(msg) {} 55 }; 56 %} 57