Calling a S60 Python function from Symbian C++
A string value is passed to the Python function, and a string value is returned
1 2 TInt retVal(KErrNone); 3 4 // Create a Python interpreter 5 CSPyInterpreter* it = CSPyInterpreter::NewInterpreterL(); 6 CleanupStack::PushL(it); 7 8 // Save state of any current Python interpreter, and acquire the 9 // interpreter lock 10 PyEval_RestoreThread(PYTHON_TLS->thread_state); 11 12 char *module_name = "Bar" ; 13 char *foo = "foo" ; 14 char *response = NULL ; 15 16 TInt32 r_len = 0 ; 17 18 PyObject *pModule = PyImport_ImportModule(module_name) ; 19 20 if ( pModule != NULL ) 21 { 22 LOG_WRITE_L("CSenPythonSession::loaded ServiceConnection"); 23 24 PyObject *module_dict = PyModule_GetDict(pModule); 25 PyObject *expression = PyDict_GetItemString(module_dict, pre_handler); 26 PyObject *arglist = Py_BuildValue("(s#)", aString.Ptr(),aString.Length()) ; 27 28 PyObject *result = PyEval_CallObject(expression, arglist); 29 30 response = PyString_AsString( result ) ; 31 32 r_len = strlen( response ) ; 33 } 34 35 // Make a Symbian descriptor pointer to the char * response 36 TPtrC8 symResponse((TUint8*)response, r_len ) ; 37 38 // Clean-up, and restore thread state 39 40 PyEval_SaveThread(); 41 CleanupStack::PopAndDestroy(it);