2 //=============================================================================
3 // File : DataFlowExecutor_PyDynInvoke.cxx
5 // Author : Jean Rahuel, CEA
8 //=============================================================================
13 #include "DataFlowExecutor_InNode.hxx"
15 using namespace CORBA ;
17 static PyObject * MyPyObjRef = NULL ;
18 static PyObject * MyPyObjIor = NULL ;
19 static PyObject * MyPyRunMethod = NULL ;
21 static PyMethodDef MethodPyRunMethod[] = {
22 { "RunMethod", PyRunMethod, METH_VARARGS },
26 bool GraphExecutor::InNode::InitPython() {
27 cdebug_in << "InitPython" << endl ;
29 PyObject * Module = Py_InitModule( "InitPyRunMethod" , MethodPyRunMethod ) ;
30 PyObject * Dictionnary = PyModule_GetDict( Module ) ;
31 // InitPyDynInvokeError = PyErr_NewException( "InitPyDynInvokeError" , NULL , NULL ) ;
32 // PyDict_SetItemString( Dictionnary , InitPyDynInvokeError ) ;
33 cdebug << ThreadNo() << "Py_Initialized() " << endl ;
36 aPyFunc = "print 'InitPyRunMethod'\n" ;
37 aPyFunc += "import InitPyRunMethod\n" ;
38 aPyFunc += "print 'sys'\n" ;
39 aPyFunc += "import sys\n" ;
40 aPyFunc += "print 'CORBA'\n" ;
41 aPyFunc += "import CORBA\n" ;
42 aPyFunc += "print 'omniORB'\n" ;
43 aPyFunc += "import omniORB\n" ;
44 aPyFunc += "print 'PyObjRef'\n" ;
45 aPyFunc += "def PyObjRef( IORObjStr ) :\n" ;
46 aPyFunc += " print 'PyObjRef',IORObjStr\n" ;
47 aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
48 aPyFunc += " print 'PyObjRef orb',orb\n" ;
49 aPyFunc += " objref = orb.string_to_object( IORObjStr )\n" ;
50 aPyFunc += " print 'PyObjRef IORObjStr objref',IORObjStr,objref\n" ;
51 aPyFunc += " return objref\n" ;
52 aPyFunc += "InitPyRunMethod.RunMethod( PyObjRef )\n" ;
53 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
54 cdebug << ThreadNo() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
57 MyPyObjRef = MyPyRunMethod ;
59 aPyFunc = "import InitPyRunMethod\n" ;
60 aPyFunc += "import sys\n" ;
61 aPyFunc += "import CORBA\n" ;
62 aPyFunc += "import omniORB\n" ;
63 aPyFunc += "def PyObjIor( ObjRef ) :\n" ;
64 aPyFunc += " print 'PyObjIor',ObjRef\n" ;
65 aPyFunc += " orb = CORBA.ORB_init( sys.argv , CORBA.ORB_ID )\n" ;
66 aPyFunc += " print 'PyObjIor orb',orb\n" ;
67 aPyFunc += " objIor = orb.object_to_string( ObjRef )\n" ;
68 aPyFunc += " print 'PyObjIor ObjRef objIor',ObjRef,objIor\n" ;
69 aPyFunc += " return objIor\n" ;
70 aPyFunc += "InitPyRunMethod.RunMethod( PyObjIor )\n" ;
71 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
72 cdebug << ThreadNo() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
75 MyPyObjIor = MyPyRunMethod ;
77 cdebug_out << "InitPython" << endl ;
81 PyObject * GraphExecutor::InNode::InitPyDynInvoke(
83 const SUPERV::ListOfStrings * aPythonFunction ) {
87 cdebug_in << "InitPyDynInvoke '" << PyFuncName << "' length "
88 << (*aPythonFunction).length() << endl ;
90 if ( (*aPythonFunction).length() ) {
92 aPyFunc += "import InitPyRunMethod\n" ;
93 for ( i = 0 ; i < (*aPythonFunction).length() ; i++ ) {
94 aPyFunc += (*aPythonFunction)[ i ] ;
97 aPyFunc += "InitPyRunMethod.RunMethod( " ;
98 aPyFunc += PyFuncName ;
100 cdebug << "InitPyDynInvoke PyRun_SimpleString " << endl << aPyFunc << endl ;
101 if ( PyRun_SimpleString( (char *) aPyFunc.c_str() ) ) {
102 cdebug << ThreadNo() << " PyRun_SimpleString ERROR " << endl << aPyFunc << endl ;
106 PyFuncRunned( true ) ;
107 // _MyPyRunMethod = MyPyRunMethod ; // To mutex
108 cdebug << ThreadNo() << "PyRun_SimpleString( " << (*aPythonFunction)[ 0 ]
111 cdebug_out << "InitPyDynInvoke" << endl ;
113 return MyPyRunMethod ;
120 extern "C" PyObject * PyRunMethod( PyObject * dummy , PyObject * Args ) {
121 // cout << "PyRunMethod -->" << endl ;
122 PyObject * Result = NULL ;
123 PyObject * Temp = NULL ;
124 if ( PyArg_ParseTuple( Args , "O:set_callback" , & Temp ) ) {
125 // cout << "PyArg_ParsedTuple" << endl ;
126 if ( !PyCallable_Check( Temp ) ) {
127 // PyErr_SetString( PyExc_TypeError , "PyRunMethod must be callable" ) ;
128 // cout << "PyRunMethod must be callable" << endl ;
132 Py_XDECREF( MyPyRunMethod ) ;
133 MyPyRunMethod = Temp ;
134 Py_INCREF( Py_None ) ;
137 // cout << "<-- PyRunMethod" << endl ;
141 bool GraphExecutor::InNode::PyDynInvoke( PyObject * MyPyRunMethod ,
143 ServicesAnyData * inParams , int nInParams ,
144 ServicesAnyData * outParams , int nOutParams ) {
149 int n_in = nInParams ;
150 int n_out = nOutParams ;
153 cdebug_in << ThreadNo() << "GraphExecutor::InNode::PyDynInvoke " << method << " " << n_in
154 << " InArgs " << n_out << " OutArgs" << endl ;
158 PyObject * ArgsList = NULL ;
159 PyObject * ArgValue = NULL ;
160 PyObject * ArgResult = NULL ;
161 PyObject * Result = NULL ;
162 PyObject * MyPyObjRefList = NULL ;
163 PyObject * ResultObj = NULL ;
164 PyObject * MyPyObjIorList = NULL ;
165 PyObject * ResultIor = NULL ;
167 ArgsList = PyTuple_New( n_in ) ;
168 CORBA::Object_ptr ObjRef ;
171 for ( i = 0 ; i < n_in ; i++ ) {
172 data = inParams[i].Value ;
173 sname = inParams[i].Name.c_str() ;
174 switch ( data.type()->kind() ) {
175 case CORBA::tk_string : {
178 ArgValue = Py_BuildValue( "s" , t ) ;
179 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
180 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << t << " (string) "
181 << ArgsList->ob_refcnt << endl ;
184 case CORBA::tk_double : {
187 ArgValue = Py_BuildValue( "d" , d ) ;
188 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
189 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << d << " (double) "
190 << ArgsList->ob_refcnt << endl ;
193 case CORBA::tk_long : {
196 ArgValue = Py_BuildValue( "l" , l ) ;
197 PyTuple_SetItem( ArgsList , i , ArgValue ) ;
198 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value " << l
199 << " (long) ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
202 case CORBA::tk_objref : {
203 MyPyObjRefList = PyTuple_New( 1 ) ;
204 PyObject * ObjValue ;
206 IORObjRef = ObjectToString( ObjRef ) ;
207 ObjValue = Py_BuildValue( "s" , IORObjRef ) ;
208 PyTuple_SetItem( MyPyObjRefList , 0 , ObjValue ) ;
209 cdebug << "ArgIn" << i << " : " << sname << method << " " << " Value " << IORObjRef << " (objref) "
210 << MyPyObjRefList->ob_refcnt << endl ;
211 ResultObj = PyEval_CallObject( MyPyObjRef , MyPyObjRefList ) ;
212 cdebug << "MyPyObjRefList->ob_refcnt" << MyPyObjRefList->ob_refcnt << " "
213 << "ResultObj->ob_refcnt" << ResultObj->ob_refcnt << endl ;
214 PyObject_Print( ResultObj , stdout , 0 ) ;
215 PyTuple_SetItem( ArgsList , i , ResultObj ) ;
216 cdebug << "ArgIn" << i << " : " << sname << " " << method << " Value (objref) ArgsList->ob_refcnt"
217 << ArgsList->ob_refcnt << " ResultObj->ob_refcnt" << ResultObj->ob_refcnt
222 cdebug << "ArgIn" << i << " : " << sname << " Value " << "(other ERROR)" << endl ;
227 Result = PyEval_CallObject( MyPyRunMethod , ArgsList ) ;
228 cdebug << "ArgsList->ob_refcnt" << ArgsList->ob_refcnt << endl ;
230 if ( Result == NULL ) {
231 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error Result == NULL" << endl ;
235 for ( i = 0 ; i < n_out ; i++ ) {
236 data = outParams[i].Value ;
237 sname = outParams[i].Name.c_str() ;
238 switch ( data.type()->kind() ) {
239 case CORBA::tk_string : {
241 ArgValue = PyTuple_GetItem( Result , i ) ;
242 if ( ArgValue == NULL ) {
245 if ( !PyString_Check( ArgValue ) ) {
246 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (string)" << endl ;
248 t = PyString_AsString( ArgValue ) ;
250 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << t << " (string)"
254 case CORBA::tk_double : {
256 ArgValue = PyTuple_GetItem( Result , i ) ;
257 if ( !PyFloat_Check( ArgValue ) ) {
258 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (double)" << endl ;
260 if ( ArgValue == NULL ) {
263 d = PyFloat_AsDouble( ArgValue ) ;
265 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << d << " (double)"
269 case CORBA::tk_long : {
271 ArgValue = PyTuple_GetItem( Result , i ) ;
272 if ( ArgValue == NULL ) {
275 if ( !PyInt_Check( ArgValue ) ) {
276 cdebug << "ArgOut" << i << " : " << sname << " " << method << " ERROR (long)" << endl ;
278 l = PyInt_AsLong( ArgValue ) ;
280 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << l << " (long)" << endl ;
283 case CORBA::tk_objref : {
284 MyPyObjIorList = PyTuple_New( 1 ) ;
285 PyObject * ObjIor = PyTuple_GetItem( Result , i ) ;
286 if ( ObjIor == NULL ) {
289 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "<< endl ;
290 cout << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) : " ;
291 Py_INCREF( ObjIor ) ;
292 PyObject_Print( ObjIor , stdout , 0 ) ;
293 PyTuple_SetItem( MyPyObjIorList , 0 , ObjIor ) ;
294 Py_DECREF( ObjIor ) ;
295 ResultIor = PyEval_CallObject( MyPyObjIor , MyPyObjIorList ) ;
296 // ObjRef = PyCObject_AsVoidPtr( ArgValue ) ;
297 // ObjRef = (CORBA::Object_ptr ) PyLong_AsVoidPtr( ArgValue ) ;
299 // ArgValue = PyTuple_GetItem( Result , i ) ;
301 IOR = PyString_AsString( ResultIor ) ;
302 ObjRef = StringToObject( IOR ) ;
304 IORObjRef = ObjectToString( ObjRef ) ;
305 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(object reference) "
306 << IORObjRef << endl ;
309 cdebug_out << "GraphExecutor::InNode::PyDynInvoke Error ResultIor == NULL"
310 << method << " " << endl ;
316 cdebug << "ArgOut" << i << " : " << sname << " " << method << " Value " << "(other ERROR)" << endl ;
319 outParams[i].Value = data ;
322 if ( MyPyObjIorList ) {
323 int cnt = MyPyObjIorList->ob_refcnt ;
325 cnt = MyPyObjIorList->ob_refcnt - 1 ;
326 Py_DECREF( MyPyObjIorList ) ;
327 cdebug << "MyPyObjIorList->ob_refcnt" << MyPyObjIorList->ob_refcnt << endl ;
331 int cnt = ResultIor->ob_refcnt ;
333 cnt = ResultIor->ob_refcnt - 1 ;
334 Py_DECREF( ResultIor ) ;
335 cdebug << "ResultIor->ob_refcnt" << ResultIor->ob_refcnt << endl ;
338 Py_DECREF( Result ) ;
341 Py_DECREF( ArgsList ) ;
342 if ( MyPyObjRefList ) {
343 int cnt = MyPyObjRefList->ob_refcnt ;
345 cnt = MyPyObjRefList->ob_refcnt - 1 ;
346 Py_DECREF( MyPyObjRefList ) ;
347 cdebug << "MyPyObjRefList->ob_refcnt" << MyPyObjRefList->ob_refcnt << endl ;
351 int cnt = ResultObj->ob_refcnt ;
353 cnt = ResultObj->ob_refcnt - 1 ;
354 Py_DECREF( ResultObj ) ;
355 cdebug << "ResultObj->ob_refcnt" << ResultObj->ob_refcnt << endl ;
359 cdebug_out << "GraphExecutor::InNode::PyDynInvoke " << method << endl ;