[C++-sig] sending a c++ class to a python function

Josh Stratton strattonbrazil at gmail.com
Mon Aug 29 05:22:49 CEST 2011

I'm getting an error when I try to pass down my object that results in
a seg fault.  I've registered my class I'm sending down, but when I
actually send it, my program exits at this line in the library right
after I call the importFile() function...

        return call<obj>(get_managed_object(self, tag),

// here's the class I'm trying to send down
class Scene
    MeshP                       mesh(int key);
    void                        clearScene();
    CameraP                     createCamera(QString name);
    MeshP                       createMesh(QString name);
    void                        setMesh(int meshKey, MeshP mesh) {
_meshes[meshKey] = mesh; }
    QHashIterator<int, MeshP>   meshes() { return
QHashIterator<int,MeshP>(_meshes); }
    QHashIterator<int, CameraP> cameras() { return
QHashIterator<int,CameraP>(_cameras); }
    CameraP                     fetchCamera(QString name);
    QList<QString>              importExtensions();
    void                        importFile(QString fileName);
    void                        evalPythonFile(QString fileName);
    int                                uniqueCameraKey();
    int                                uniqueMeshKey();
    QString                            uniqueName(QString prefix);

    QHash<int,MeshP>                   _meshes;
    QHash<int,CameraP>                 _cameras;
    //QHash<int,Light*>      _lights;
    QSet<QString>                      _names;
//    PythonQtObjectPtr                  _context;
    object                             _pyMainModule;
    object                             _pyMainNamespace;
public slots:
    void                               pythonStdOut(const QString &s)
{ std::cout << s.toStdString() << std::flush; }
    void                               pythonStdErr(const QString &s)
{ std::cout << s.toStdString() << std::flush; }

// first I create the mapping, which I'm not sure is correct, trying
to follow: http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/
struct SceneToPython
    static PyObject* convert(Scene const& scene)
        return boost::python::incref(boost::python::object(scene).ptr());

// then I register it

// then I send it down from inside my Scene object
    try {
        object processFileFunc =
        processFileFunc(this, fileName); // seems to error here
    } catch (boost::python::error_already_set const &) {
        QString perror = parse_python_exception();
        std::cerr << "Error in Python: " << perror.toStdString() << std::endl;

I'm not really sure what actually is wrong besides something being
setup incorrectly.  Do I need to make a python-to-C++ converter as
well even if I'm not sending it back to C++?  Or is my convert()
function just improperly implemented?  I wasn't sure how much I need
to actually get it to map correctly.  Thanks.

More information about the Cplusplus-sig mailing list