Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4579

Re: Pickling extension types

From Robert Kern <robert.kern@gmail.com>
Subject Re: Pickling extension types
Date 2011-05-03 17:05 -0500
Organization The Church of Last Thursday
References <BANLkTi=sUu_VFKrW+KSk8-RDzr5vgSLKqQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1126.1304460339.9059.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Pickling extension types Robert Kern <robert.kern@gmail.com> - 2011-05-03 17:05 -0500

csiph-web