Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:How to embed Python in Symbian C++

From Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Article
Created: cyke64 (12 Apr 2007)
Last edited: hamishwillee (31 May 2013)


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

This page was last modified on 31 May 2013, at 01:03.
32 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×