Embeding Python... (Please help !)

Phil Thompson phil at river-bank.demon.co.uk
Sat Jan 4 12:14:02 EST 2003


On Friday 03 January 2003 5:40 pm, Sebastien Auclair wrote:
> 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....

Python doesn't know anything about how SIP wraps C++ pointers - you need to use the SIP library/module...

int iserr = 0;
interface *interf = (interface *)sipConvertToCpp(v1,sipClass_interface,&iserr);

Phil





More information about the Python-list mailing list