From: Oliver B. <oli...@go...> - 2012-05-04 12:56:39
|
Small addition: of course you can create wrapper instances by yourself. Maybe this is what you need. Have a look at generated wrapper code, specifically the constructor wrappers. Then you'll know how to do it... Regards, Oliver On 05/04/2012 02:26 PM, Oliver Buchtala wrote: > Hi Yoann, > > Bob gave you all necessary hints to solve this. > Though, what you want to achieve is somewhat out of SWIG's typical usage. > Usually, C/C++ code is wrapped into a target language to be used from > there. > Nonetheless, there is support for the other direction, too, which is > called 'director'. > The feature allows to extend C++ base classes from within the target > language, e.g., extend a C++ class with a Python class. For that, code > is generated which could be useful for you. > > You need to delve into this topic - though, that is advanced stuff. > Then find out how to make use of it in your application. > > Regards, > Oliver > > On 05/04/2012 02:05 PM, Yoann Chaumy wrote: >> Hi, >> >> Yes, I have read the swig documentation. >> >> I have created a simple C++ class, and can successfully use it in my >> python scripts >> >> But my aim is to create a C++ instance, and pass it to my python >> script. The script will modify the instance and return it to the C++ >> application. >> >> I have progress, now my main call a function in my Python script : >> >> >> PyObject *pName, *pModule, *pDict, *pFunc, *pValue; >> PyObject *pClass, *pInstance, *pArgs; >> >> // Initialize the Python Interpreter >> Py_Initialize(); >> >> // Build the name object >> pName = PyString_FromString("Perso"); >> >> if(pName == NULL) >> cout<< "error" << endl; >> >> // Load the module object >> pModule = PyImport_Import(pName); >> >> if(pName == NULL) >> cout<< "error" << endl; >> >> // pDict is a borrowed reference >> pDict = PyModule_GetDict(pModule); >> if(pDict == NULL) >> cout<< "error" << endl; >> >> // pFunc is also a borrowed reference >> pFunc = PyDict_GetItemString(pDict, "test"); >> if(pFunc == NULL) >> cout<< "error fonction" << endl; >> >> if (PyCallable_Check(pFunc)) >> { >> pArgs = PyTuple_New(1); // create list of arg >> pValue = PyInt_FromLong(123); >> PyTuple_SetItem(pArgs, 0, pValue); >> PyObject_CallObject(pFunc, pArgs); >> } else >> { >> PyErr_Print(); >> } >> >> // Clean up >> Py_DECREF(pModule); >> Py_DECREF(pName); >> >> Py_Finalize(); >> >> >> Now I can send parameter to my script but it is a integer, and my aim >> is to send a instance, is it possible ? >> I have try to use Py_BuildValue to cast my object into PyObject but >> no success >> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- >> >> Well, it's certainly one way to execute an external Python script >> from C++. However, if you are wanting to interact with the external >> script from within the C++ code that manages the Python environment >> (above), then the process is considerably more involved. >> >> I assume that, since you've posted this question to the SWIG mailing >> list, you are implicitly asking about using SWIG to accomplish this >> task. Have you read the SWIG documentation for Python >> <http://www.swig.org/Doc2.0/Python.html#Python>? In particular, >> you'll probably want to employ directors >> <http://www.swig.org/Doc2.0/Python.html#Python_nn34>. >> >> You'll better help us help you if you are at least somewhat familiar >> with the process of using SWIG to achieve your goal. >> >> >> Render me gone, ||| >> Bob ^(===)^ >> ---------------------------------oOO--(_)--OOo--------------------------------- >> I'm not so good with advice...can I interest you in a sarcastic comment? >> >> ------------------------------------------------------------------------------ >> Live Security Virtual Conference Exclusive live event will cover all >> the ways today's security and threat landscape has changed and how IT >> managers can respond. Discussions will include endpoint security, >> mobile security and the latest in malware threats. >> http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ >> _______________________________________________ Swig-user mailing >> list Swi...@li... >> https://lists.sourceforge.net/lists/listinfo/swig-user >> >> >> ------------------------------------------------------------------------------ >> Live Security Virtual Conference >> Exclusive live event will cover all the ways today's security and >> threat landscape has changed and how IT managers can respond. Discussions >> will include endpoint security, mobile security and the latest in malware >> threats.http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ >> >> >> _______________________________________________ >> Swig-user mailing list >> Swi...@li... >> https://lists.sourceforge.net/lists/listinfo/swig-user > |