Embeding Python... (Please help !)

Sebastien Auclair sxa at fluent.com
Fri Jan 3 12:40:21 EST 2003


Our problem is so trivial, it's crazy that we can't find anything on the net
that would help us.

We are trying to use the function PyArg_Parse and others in order to simply
get a correctly casted pointer to a Python Class instance.

That python class is in fact a subclass of a C++ class defining an
interface. Our python file knows this interface class through SIP bindings.

There are some examples of how we can get simple C type values (strings,
int, float...) from Python types but nothing on pointers to class... We
tried to use the O& option but it doesn't work.

EXAMPLE :

<<<<<<< PYTHON FILE >>>>>>>>>>>>>
import Interfaces # From sipping

def CreateInstance ():
    return Server ()

class Server (Interfaces.interface):
        def __init__(self):
                Interfaces.interface.__init__(self)


<<<<<<<<< INTERFACE .h DEFINITION FILE>>>>>>>>>>>
class interface{

    interface();
    ~interface();
    someFunction();
};
<<<<<<<< MAIN APPLICATION >>>>>>>

#include "Interfaces.h"
...
........python stuff to load and compile the python file...this works
fine... we tested it !
...

PyObject * m_pylib = PyImport_ExecCodeModule( const_cast<char*>("server"),
code );
Py_INCREF( m_pylib );

PyObject * mdict = PyModule_GetDict(m_pylib);
PyObject  * func1 = PyDict_GetItemString(mdict, "CreateInstance");
PyObject * v1 = PyObject_CallObject(func1,NULL);

interface * interf = (interface*) v1;  // OF COURSE THIS DOESN'T WORK BUT
YOU GET THE IDEA....

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Thanks for any help !
____________________________________________________________________________
____________











More information about the Python-list mailing list