implementing reduce protocol in C

Tom Widrick lordmacro at hotmail.com
Thu Sep 19 21:45:05 EDT 2002


I have an extension type I'm trying to implement the reduce protocol 
with. I have successfully gotten type instances to pickle but cannot 
unpickle them. I get the following error.

newRoot = cPickle.loads(d)
TypeError: ('argument list must be a tuple', <type 'myobj'>, None)

By following the instructions at 
http://www.python.org/doc/current/lib/node65.html I have the following C 
code.

static PyObject *
myobj_reduce(self)
	MyObject *self;
{
	PyObject *args;
	PyObject *result;

	args = PyTuple_New(5);
	if(args == NULL)
		return NULL;

	PyTuple_SET_ITEM(args, 0, self->po_name);
	PyTuple_SET_ITEM(args, 1, self->po_parents);
	PyTuple_SET_ITEM(args, 2, self->po_children);
	PyTuple_SET_ITEM(args, 3, self->po_doc);
	PyTuple_SET_ITEM(args, 4, self->po_dict);

	result = Py_BuildValue("(OOO)", self->ob_type, Py_None, args);
	return result;
};

The type constructor wouldn't have worked as a call to unpickle the 
object so I've used basicnew instead.

static PyObject *
myobj_basicnew()
{
	MyObject *self;

	self = PyObject_New(MyObject, &MyObject_Type);
	return (PyObject *)self;
}

and a setstate method which doesn't matter because the previous error 
occurs before accessing it.

What is it that I am missing?

Tom





More information about the Python-list mailing list