[C++-sig] Interfacing pure abstract classes with Boost.Python

Pere Mato Vila Pere.Mato at cern.ch
Thu Jan 23 19:40:40 CET 2003


Hi all,

  I have problems interfacing pure abstract classes, which I do not
intend to extent from the Python side, with version 2 of Boost.Python.

The class IHistogram is defined as

class IHistogram   {
public: 
  virtual int dimension (  ) const = 0;
  ...
};

I expose it as

  python::class_<IHistogram, boost::noncopyable > ("IHistogram",
python::no_init )
    .def("dim",   &IHistogram::dimension )
  ...

And I provide the method iHistogramSvc::book1D(...) which returns an
IHistogram* and is exposed as

  python::class_<GaudiPython::iHistogramSvc>
     ( "IHistogramSvc", python::init<IHistogramSvc*>() )
    .def("book",       &GaudiPython::iHistogramSvc::book1D )

So far so good. I am able to compile and produce the Python extension
module. But, when I run the Python code I get:

>>> gaudi.IHistogram1D
<class 'gaudimodule.IHistogram1D'>
>>> h1 = his.book('1', 'histogram title', 10, 0, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: No to_python (by-value) converter found for C++ type: class
IHistogram1D


  What I am doing wrong? In the old version I was able to work around
the problem by adding the lines in the C++ module extension 

PyObject* to_python(IHistogram* p) {
    return
boost::python::python_extension_class_converters<IHistogram>::smart_ptr_
to_python(p);
}

  And obviously this is not working.

------------------------------------------------------------
Pere Mato  CERN, EP Division, CH 1211 Geneva 23, Switzerland
           e-mail: Pere.Mato at cern.ch    tel: +41 22 76 78696
           fax:  +41 22 76 79425        gsm: +41 79 20 10855

 




More information about the Cplusplus-sig mailing list