Pickling extension types
Stefan Kuzminski
stefan.kuzminski at gmail.com
Tue May 3 12:48:27 EDT 2011
Hi all,
I have an extension type written in C, but I cannot get it to pickle, any
insights would be greatly appreciated.
I see in the docs that I should define a __reduce__ method and that does get
called, but I don't know specifically the type of the 'callable object' that
should be the first thing in the tuple returned by __reduce__.
thx,
S
start listing..
#include "Python.h"
static PyObject *mv_new(PyTypeObject *type, PyObject *args, PyObject *kw);
static int mv_cmp(PyObject *self, PyObject *other);
/*
* print MV
*/
static PyObject *mv_repr(MV *mv){
PyObject *rtn;
rtn = PyString_FromFormat("MV(%d)", mv->index);
return rtn;
}
/*
* get mv's index
*/
static PyObject *mv_idx(PyObject *self, PyObject *ags){
MV *mv;
mv = (MV*)self;
return PyInt_FromLong( (long)mv->index );
}
/*
* get mv's index as str
*/
static PyObject *mv_reduce(PyObject *self, PyObject *ags){
MV *mv;
mv = (MV*)self;
return PyString_FromString( "foo" );
}
/*
* get mv's index as str
*/
static PyObject *mv_reduce(PyObject *self, PyObject *ags){
MV *mv;
mv = (MV*)self;
PyObject *return_tuple;
// build the return tuple
return_tuple = PyTuple_New(5);
// THIS IS THE QUESTION, what to set as "callable object??"
// PyTuple_SetItem( return_tuple, 0, mv_new );
PyTuple_SetItem( return_tuple, 1 PyTuple_New());
return return_tuple;
}
static PyMethodDef mv_methods[] = {
{"index", mv_idx, METH_VARARGS},
{"__reduce__", mv_reduce, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
PyTypeObject PyMV_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* Number of items for varobject */
"MV", /* Name of this type */
sizeof(MV), /* Basic object size */
0, /* Item size for varobject */
(destructor)PyObject_Del, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
mv_cmp, /* tp_compare */
(reprfunc)mv_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
mv_idx, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
mv_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
mv_new, /* tp_new */
};
/*
* cmp MV
*/
static int mv_cmp(PyObject *mv, PyObject *other){
if( other->ob_type == &PyMV_Type ) {
if( ((MV*)mv)->index == ((MV*)other)->index ) {
return 0;
} else {
return -1;
}
}
return -1;
}
/*
* new MV
*/
static PyObject *mv_new(PyTypeObject *type, PyObject *args, PyObject *kw){
MV *mv;
int index;
mv = PyObject_New(MV, &PyMV_Type);
if (mv == NULL){
return NULL;
}
// get the args
if( !PyArg_ParseTuple( args,(char *)"i:MV", &index )){
return NULL;
}
// store index
mv->index = index;
return (PyObject*)mv;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110503/a57491b7/attachment.html>
More information about the Python-list
mailing list