Use of the Python scripting language is an excellent way how to extend the functionality of the C++ program. It is ideal especially for the game scripting or for complex configuration files. The Python itself can be downloaded from the www.sourceforge.net pages. For the embedding on the S60 3rd platform the sis file (PythonForS60_1_4_5_3rdEd.sis) is necessary to be installed on the phone. In fact the only necessary component for the embedding is the Python interpreter DLL library python222.dll included within the installation sis file. If embedding the sis file into application sis, which invokes the Python from C++, is not the suitable way or capabilities of the DLL must be changed or there is simply some other reason to not use the installation directly, we have the possibility to download sources (pys60-1.4.5_src.zip) and build the python interpreter DLL on our own. With the help of description in the readme.txt from the sources zip file it is quite easy. If the own build of the interpreter DLL will be part of the sis installation package you have to remember to change its UID and name to not clash with the original Python installation.
The advantage of the Python is also its availability on Windows Mobile platform, so if you are writing the portable C++ application it is currently the only choice (as far as I know) for the scripting language as other good languages like Lua or GameMonkey are not ported to both platforms.
How to embed
For embedding scripts into C++ code there are two possibilities, either use directly the Python/C API or use the CSPyInterpreter Symbian wrapper class. While use of the wrapper class is easier to use, it does not allow handling input/output script (module) parameters, calling selected functions and getting back the result.
To be able to use the Python in C++ code the SDK from the www.sourceforge.net pages has to be downloaded and installed. For each S60 3rd platform edition there is the separate package (e.g. PythonForS60_1_4_5_SDK_3rdEd.zip) – it contains include header files and the Python222.lib export library we need to link against.
Using the CSPyInterpreter class is pretty simple, as shown in the code snippet below:
Python script writeToFile.py:
In a more complex example we need:
1. Convert data values from Symbian C++ to native C representation and then to Python representation
2. Perform a function call from a Python script using converted values
3. Convert data values from the Python back to the Symbian C++
Following code snippets do the stuff. First some initialization is done; the c:\scripts path is specified as a script repository. If the path is not specified .py scripts are expected to be saved in the c:\resource directory. Then all parameters are converted to the PyObject type and the script is loaded by calling the PyImport_Import() method. Later on the function object is retrieved from the script and is called. The result is converted from PyObject to char* and then to TPtrC8.
The second example shows the situation, where the method to sum two number is called, so we need to pass to Python script two integer values we want to be added. This is done by PyTuple_XXX() methods, which construct kind of dynamic array into which we consequently insert PyObject arguments for the script function.
S60 Python home page: