Pickling extension types

Robert Kern robert.kern at gmail.com
Tue May 3 18:05:23 EDT 2011


On 5/3/11 4:34 PM, Stefan Kuzminski wrote:
> Thanks for the clues, I made a modification so that reduce returns this..
>
>    return Py_BuildValue("O(OOOOO)", Py_TYPE(self), PyTuple_New(0), Py_None,
> Py_None, Py_None );
>
> and now I get this different error when trying to pickle the type..
>
>   ----------------------------------------------------------------------
> Traceback (most recent call last):
>    File "test_missing.py", line 16, in test
>      print cPickle.dumps( mv )
> PicklingError: Can't pickle <type 'MV'>: attribute lookup __builtin__.MV failed

You need to do two things:

1. Set tp_name to something like "mymodule.MV" where "mymodule" is the name of 
your extension module.

2. Make sure that you add the type object to your module's namespace.

PyMODINIT_FUNC
initmymodule(void) {
   ...
   Py_INCREF(&PyMV_Type);
   PyModule_AddObject(m, "MV", (PyObject*)&PyMV_Type);
}

C.f.

http://docs.python.org/extending/newtypes.html

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list