[C++-sig] Pickling problem

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Mon Mar 1 11:42:59 CET 2004


I think I've found the problem (or at least part of it).

The method getBase() of the class Proxy returns a Base pointer. When
called from python, boost detect the dynamic type of the object (here
Derived) and returns the real object. But, when I create a
boost::python::object from C++ this resolution is not done and the
object is known as a Base one. So, when pickling, it tries to pickle a
Base object (which cannot be pickled). Is there a way to force the
detection of the dynamic type when creating a boost::python::object ?

I would like to have this behaviour :

C++ code:

boost::python::object get_base(Proxy& p)
{
  return boost::python::object(p.getBase());
}

BOOST_PYTHON_MODULE(test_abstract)
{
  ...
  def("get_base", get_base);
  ...
}

Python:

>>> from test_abstract import *
>>> p = Proxy(Derived(10))
>>> get_base(p)
Derived(10)

But for now I have :

>>> from test_abstract import *
>>> p = Proxy(Derived(10))
>>> get_base(p)
<test_abstract.Base object at 0x4022d5dc>

How can I have the first behaviour ?

Pierre

On Mon, 2004-03-01 at 10:56, Pierre Barbier de Reuille wrote:
> Hi !
> 
> I have a problem with pickling a proxy class. I join the C++ code as an
> attached file.
> 
> I have three classes :
>  - Base : an abstract class
>  - Derived : a subclass of Base
>  - Proxy : the proxy class that contains an instance of Base
> 
> Derived objects can be pickled and the pickling of Proxy just return a
> tuple with a Base object in it. Here is a python sample :
> 
> 
> >>> import test_abstract
> >>> p = test_abstract.Proxy(test_abstract.Derived(10))
> >>> import cPickle
> >>> cPickle.dumps(p.getBase())
> 'ctest_abstract\nDerived\np1\n(I10\ntRp2\n.'
> >>> cPickle.dumps(p)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> cPickle.PicklingError: Can't pickle <type 'Boost.Python.instance'>:
> import of module Boost.Python failed
> 
> I really don't know what to do !
> 
> Thanks,
> 
> Pierre
-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68 





More information about the Cplusplus-sig mailing list