[Tutor] What's wrong with this code ?
Sebastien Auclair
sxa@fluent.com
Mon Jan 6 12:43:02 2003
See code example below !
We are embeding python and our C++ software is trying to get a C++ pointer
to a C++ object instantiated in a python module.
This python Class "MyClass" is a sub-class of the C++ "An_Insterface*" class
declared in library "A_Lib" that is of course accessible through SIP
bindings.
The few CPP lines :
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
An_Interface* m_interface = NULL;
PyObject *mdict = NULL;
PyObject *func1 = NULL;
PyObject * v1 = NULL;
PyObject * sipClass;
int bErrorOccured = 0;
initlibsip();
mdict = PyModule_GetDict(m_pylib); // m_pylib is the compiled python
file... this isvalid...It was tested.
func1 = PyDict_GetItemString(mdict, "CreateInstance"); // The function
that returns an instance of the desired class. (as a pointer)
v1 = PyObject_CallObject(func1,NULL); // v1 will then contain some value
other than NULL
if (v1 == Py_None || sipIsSubClassInstance(v1,sipClass)){
// Interesting !! This (if) test will always be FALSE (For
both)
}
///// HERE ARE THE CONVERSION ATTTEMPS !!!!!!!!!!!!!!
m_interface = (An_Interface*)sipConvertToCpp
(v1,sipClass,&bErrorOccured); // returns NULL with bErrorOccured == 1
m_interface = sipForceConvertTo_An_Interface(v1, &bErrorOccured); //
Returns NULL with bErrorOccured == 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Of course, the two last lines are both trying to do the same thing. It
isjust to show you what we tried.
The python file, in short, looks like this :
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
def CreateInstance( ):
return MyClass ()
class MyClass (A_Lib.An_Interface):
def __init__(self):
A_Lib.An_Interface.__init__(self)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>