Join Now
Quality Rating:
  • Currently 3.0 / 5
(3.0 / 5 - 1 vote cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 22:48, 2 March 2008.

How to embed Python in Symbian C++

From Forum Nokia Wiki

it's pretty common to want to extend S60 Python by adding functionality available to the underlying S60 C++ APIs. You can extend Python with C++.

But, you can also embed a Python interpreter inside !

Add the following code , which does a lot of setup, but calls the function foo in the module Bar.


TInt retVal(KErrNone);
 
  // Create a  Python interpreter
  CSPyInterpreter* it = CSPyInterpreter::NewInterpreterL();
  CleanupStack::PushL(it);
 
  // Save state of any current Python interpreter, and acquire the
  // interpreter lock
  PyEval_RestoreThread(PYTHON_TLS->thread_state);
 
  char *module_name = "Bar" ;
  char *foo = "foo" ;
  char *response = NULL ;
 
  TInt32 r_len = 0 ;
  PyObject *pModule = PyImport_ImportModule(module_name) ;
 
  if ( pModule != NULL )
    {
      PyObject *module_dict = PyModule_GetDict(pModule);
      PyObject *expression = PyDict_GetItemString(module_dict, pre_handler);
      PyObject *arglist = Py_BuildValue("(s#)", aString.Ptr(),aString.Length()) ;
 
      PyObject *result = PyEval_CallObject(expression, arglist);
 
      response = PyString_AsString( result ) ;
 
      r_len = strlen( response ) ;
    }
 
    // Make a Symbian descriptor pointer to the char * response
    TPtrC8 symResponse((TUint8*)response, r_len ) ;
 
    // Clean-up, and restore thread state
 
    PyEval_SaveThread();
    CleanupStack::PopAndDestroy(it);


#
# module Bar.py
# 
 
# function 'foo' in the module 'Bar'. 
# A string value is passed to the Python function
# and a string value is returned
 
def foo(message):
    return 'foo got message: ' + message

link : documentation Python Chapter 9.1 Extending and embedding

Related Discussions
Thread Thread Starter Forum Replies Last Post
Guidelines and public interface for custom C++ widgets IsoPahaSusi Python 1 2005-06-18 12:46
[Help]Getting the emulator up and running Jeroen1000 Python 9 2008-06-04 06:11
How to install Ethernet Plug-in for S60 2nd Ed FP3 C++ SDK IceFellow Python 8 2006-10-06 11:51
Accelerometer\Sensor apps for N95 - Java? Python? C++? gillyd General Discussion 2 2008-01-08 04:50
source of concept demos _Askin_ Mobile Web Server 3 2008-05-10 17:46
 
Powered by MediaWiki