Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 1 | \chapter{Embedding Python in Another Application |
| 2 | \label{embedding}} |
| 3 | |
| 4 | The previous chapters discussed how to extend Python, that is, how to |
| 5 | extend the functionality of Python by attaching a library of C |
| 6 | functions to it. It is also possible to do it the other way around: |
| 7 | enrich your C/\Cpp{} application by embedding Python in it. Embedding |
| 8 | provides your application with the ability to implement some of the |
| 9 | functionality of your application in Python rather than C or \Cpp. |
| 10 | This can be used for many purposes; one example would be to allow |
| 11 | users to tailor the application to their needs by writing some scripts |
| 12 | in Python. You can also use it yourself if some of the functionality |
| 13 | can be written in Python more easily. |
| 14 | |
| 15 | Embedding Python is similar to extending it, but not quite. The |
| 16 | difference is that when you extend Python, the main program of the |
| 17 | application is still the Python interpreter, while if you embed |
| 18 | Python, the main program may have nothing to do with Python --- |
| 19 | instead, some parts of the application occasionally call the Python |
| 20 | interpreter to run some Python code. |
| 21 | |
| 22 | So if you are embedding Python, you are providing your own main |
| 23 | program. One of the things this main program has to do is initialize |
| 24 | the Python interpreter. At the very least, you have to call the |
| 25 | function \cfunction{Py_Initialize()} (on Mac OS, call |
| 26 | \cfunction{PyMac_Initialize()} instead). There are optional calls to |
| 27 | pass command line arguments to Python. Then later you can call the |
| 28 | interpreter from any part of the application. |
| 29 | |
| 30 | There are several different ways to call the interpreter: you can pass |
| 31 | a string containing Python statements to |
| 32 | \cfunction{PyRun_SimpleString()}, or you can pass a stdio file pointer |
| 33 | and a file name (for identification in error messages only) to |
| 34 | \cfunction{PyRun_SimpleFile()}. You can also call the lower-level |
| 35 | operations described in the previous chapters to construct and use |
| 36 | Python objects. |
| 37 | |
| 38 | A simple demo of embedding Python can be found in the directory |
| 39 | \file{Demo/embed/} of the source distribution. |
| 40 | |
| 41 | |
| 42 | \begin{seealso} |
| 43 | \seetitle[../api/api.html]{Python/C API Reference Manual}{The |
| 44 | details of Python's C interface are given in this manual. |
| 45 | A great deal of necessary information can be found here.} |
| 46 | \end{seealso} |
| 47 | |
| 48 | |
| 49 | \section{Very High Level Embedding |
| 50 | \label{high-level-embedding}} |
| 51 | |
| 52 | The simplest form of embedding Python is the use of the very |
| 53 | high level interface. This interface is intended to execute a |
| 54 | Python script without needing to interact with the application |
| 55 | directly. This can for example be used to perform some operation |
| 56 | on a file. |
| 57 | |
| 58 | \begin{verbatim} |
| 59 | #include <Python.h> |
| 60 | |
Fred Drake | adaca02 | 2001-09-06 16:17:24 +0000 | [diff] [blame] | 61 | int |
| 62 | main(int argc, char *argv[]) |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 63 | { |
| 64 | Py_Initialize(); |
| 65 | PyRun_SimpleString("from time import time,ctime\n" |
| 66 | "print 'Today is',ctime(time())\n"); |
| 67 | Py_Finalize(); |
| 68 | return 0; |
| 69 | } |
| 70 | \end{verbatim} |
| 71 | |
| 72 | The above code first initializes the Python interpreter with |
| 73 | \cfunction{Py_Initialize()}, followed by the execution of a hard-coded |
| 74 | Python script that print the date and time. Afterwards, the |
| 75 | \cfunction{Py_Finalize()} call shuts the interpreter down, followed by |
| 76 | the end of the program. In a real program, you may want to get the |
| 77 | Python script from another source, perhaps a text-editor routine, a |
| 78 | file, or a database. Getting the Python code from a file can better |
| 79 | be done by using the \cfunction{PyRun_SimpleFile()} function, which |
| 80 | saves you the trouble of allocating memory space and loading the file |
| 81 | contents. |
| 82 | |
| 83 | |
| 84 | \section{Beyond Very High Level Embedding: An overview |
| 85 | \label{lower-level-embedding}} |
| 86 | |
| 87 | The high level interface gives you the ability to execute |
| 88 | arbitrary pieces of Python code from your application, but |
| 89 | exchanging data values is quite cumbersome to say the least. If |
| 90 | you want that, you should use lower level calls. At the cost of |
| 91 | having to write more C code, you can achieve almost anything. |
| 92 | |
| 93 | It should be noted that extending Python and embedding Python |
| 94 | is quite the same activity, despite the different intent. Most |
| 95 | topics discussed in the previous chapters are still valid. To |
| 96 | show this, consider what the extension code from Python to C |
| 97 | really does: |
| 98 | |
| 99 | \begin{enumerate} |
| 100 | \item Convert data values from Python to C, |
| 101 | \item Perform a function call to a C routine using the |
| 102 | converted values, and |
| 103 | \item Convert the data values from the call from C to Python. |
| 104 | \end{enumerate} |
| 105 | |
| 106 | When embedding Python, the interface code does: |
| 107 | |
| 108 | \begin{enumerate} |
| 109 | \item Convert data values from C to Python, |
| 110 | \item Perform a function call to a Python interface routine |
| 111 | using the converted values, and |
| 112 | \item Convert the data values from the call from Python to C. |
| 113 | \end{enumerate} |
| 114 | |
| 115 | As you can see, the data conversion steps are simply swapped to |
| 116 | accomodate the different direction of the cross-language transfer. |
| 117 | The only difference is the routine that you call between both |
| 118 | data conversions. When extending, you call a C routine, when |
| 119 | embedding, you call a Python routine. |
| 120 | |
| 121 | This chapter will not discuss how to convert data from Python |
| 122 | to C and vice versa. Also, proper use of references and dealing |
| 123 | with errors is assumed to be understood. Since these aspects do not |
| 124 | differ from extending the interpreter, you can refer to earlier |
| 125 | chapters for the required information. |
| 126 | |
| 127 | |
| 128 | \section{Pure Embedding |
| 129 | \label{pure-embedding}} |
| 130 | |
| 131 | The first program aims to execute a function in a Python |
| 132 | script. Like in the section about the very high level interface, |
| 133 | the Python interpreter does not directly interact with the |
| 134 | application (but that will change in th next section). |
| 135 | |
| 136 | The code to run a function defined in a Python script is: |
| 137 | |
| 138 | \verbatiminput{run-func.c} |
| 139 | |
| 140 | This code loads a Python script using \code{argv[1]}, and calls the |
| 141 | function named in \code{argv[2]}. Its integer arguments are the other |
| 142 | values of the \code{argv} array. If you compile and link this |
| 143 | program (let's call the finished executable \program{call}), and use |
| 144 | it to execute a Python script, such as: |
| 145 | |
| 146 | \begin{verbatim} |
| 147 | def multiply(a,b): |
| 148 | print "Thy shall add", a, "times", b |
| 149 | c = 0 |
| 150 | for i in range(0, a): |
| 151 | c = c + b |
| 152 | return c |
| 153 | \end{verbatim} |
| 154 | |
| 155 | then the result should be: |
| 156 | |
| 157 | \begin{verbatim} |
| 158 | $ call multiply 3 2 |
| 159 | Thy shall add 3 times 2 |
| 160 | Result of call: 6 |
| 161 | \end{verbatim} % $ |
| 162 | |
| 163 | Although the program is quite large for its functionality, most of the |
| 164 | code is for data conversion between Python and C, and for error |
| 165 | reporting. The interesting part with respect to embedding Python |
| 166 | starts with |
| 167 | |
| 168 | \begin{verbatim} |
| 169 | Py_Initialize(); |
| 170 | pName = PyString_FromString(argv[1]); |
| 171 | /* Error checking of pName left out */ |
| 172 | pModule = PyImport_Import(pName); |
| 173 | \end{verbatim} |
| 174 | |
| 175 | After initializing the interpreter, the script is loaded using |
| 176 | \cfunction{PyImport_Import()}. This routine needs a Python string |
| 177 | as its argument, which is constructed using the |
| 178 | \cfunction{PyString_FromString()} data conversion routine. |
| 179 | |
| 180 | \begin{verbatim} |
| 181 | pDict = PyModule_GetDict(pModule); |
| 182 | /* pDict is a borrowed reference */ |
| 183 | |
| 184 | pFunc = PyDict_GetItemString(pDict, argv[2]); |
| 185 | /* pFun is a borrowed reference */ |
| 186 | |
| 187 | if (pFunc && PyCallable_Check(pFunc)) { |
| 188 | ... |
| 189 | } |
| 190 | \end{verbatim} |
| 191 | |
| 192 | Once the script is loaded, its dictionary is retrieved with |
| 193 | \cfunction{PyModule_GetDict()}. The dictionary is then searched using |
| 194 | the normal dictionary access routines for the function name. If the |
| 195 | name exists, and the object retunred is callable, you can safely |
| 196 | assume that it is a function. The program then proceeds by |
| 197 | constructing a tuple of arguments as normal. The call to the python |
| 198 | function is then made with: |
| 199 | |
| 200 | \begin{verbatim} |
| 201 | pValue = PyObject_CallObject(pFunc, pArgs); |
| 202 | \end{verbatim} |
| 203 | |
| 204 | Upon return of the function, \code{pValue} is either \NULL{} or it |
| 205 | contains a reference to the return value of the function. Be sure to |
| 206 | release the reference after examining the value. |
| 207 | |
| 208 | |
| 209 | \section{Extending Embedded Python |
| 210 | \label{extending-with-embedding}} |
| 211 | |
| 212 | Until now, the embedded Python interpreter had no access to |
| 213 | functionality from the application itself. The Python API allows this |
| 214 | by extending the embedded interpreter. That is, the embedded |
| 215 | interpreter gets extended with routines provided by the application. |
| 216 | While it sounds complex, it is not so bad. Simply forget for a while |
| 217 | that the application starts the Python interpreter. Instead, consider |
| 218 | the application to be a set of subroutines, and write some glue code |
| 219 | that gives Python access to those routines, just like you would write |
| 220 | a normal Python extension. For example: |
| 221 | |
| 222 | \begin{verbatim} |
| 223 | static int numargs=0; |
| 224 | |
| 225 | /* Return the number of arguments of the application command line */ |
| 226 | static PyObject* |
| 227 | emb_numargs(PyObject *self, PyObject *args) |
| 228 | { |
| 229 | if(!PyArg_ParseTuple(args, ":numargs")) |
| 230 | return NULL; |
| 231 | return Py_BuildValue("i", numargs); |
| 232 | } |
| 233 | |
| 234 | static PyMethodDef EmbMethods[]={ |
| 235 | {"numargs", emb_numargs, METH_VARARGS}, |
| 236 | {NULL, NULL} |
| 237 | }; |
| 238 | \end{verbatim} |
| 239 | |
| 240 | Insert the above code just above the \cfunction{main()} function. |
| 241 | Also, insert the following two statements directly after |
| 242 | \cfunction{Py_Initialize()}: |
| 243 | |
| 244 | \begin{verbatim} |
| 245 | numargs = argc; |
| 246 | Py_InitModule("emb", EmbMethods); |
| 247 | \end{verbatim} |
| 248 | |
| 249 | These two lines initialize the \code{numargs} variable, and make the |
| 250 | \function{emb.numargs()} function accessible to the embedded Python |
| 251 | interpreter. With these extensions, the Python script can do things |
| 252 | like |
| 253 | |
| 254 | \begin{verbatim} |
| 255 | import emb |
| 256 | print "Number of arguments", emb.numargs() |
| 257 | \end{verbatim} |
| 258 | |
| 259 | In a real application, the methods will expose an API of the |
| 260 | application to Python. |
| 261 | |
| 262 | |
| 263 | %\section{For the future} |
| 264 | % |
| 265 | %You don't happen to have a nice library to get textual |
| 266 | %equivalents of numeric values do you :-) ? |
| 267 | %Callbacks here ? (I may be using information from that section |
| 268 | %?!) |
| 269 | %threads |
| 270 | %code examples do not really behave well if errors happen |
| 271 | % (what to watch out for) |
| 272 | |
| 273 | |
| 274 | \section{Embedding Python in \Cpp{} |
| 275 | \label{embeddingInCplusplus}} |
| 276 | |
| 277 | It is also possible to embed Python in a \Cpp{} program; precisely how this |
| 278 | is done will depend on the details of the \Cpp{} system used; in general you |
| 279 | will need to write the main program in \Cpp{}, and use the \Cpp{} compiler |
| 280 | to compile and link your program. There is no need to recompile Python |
| 281 | itself using \Cpp{}. |
| 282 | |
| 283 | |
| 284 | \section{Linking Requirements |
| 285 | \label{link-reqs}} |
| 286 | |
| 287 | While the \program{configure} script shipped with the Python sources |
| 288 | will correctly build Python to export the symbols needed by |
| 289 | dynamically linked extensions, this is not automatically inherited by |
| 290 | applications which embed the Python library statically, at least on |
| 291 | \UNIX. This is an issue when the application is linked to the static |
| 292 | runtime library (\file{libpython.a}) and needs to load dynamic |
| 293 | extensions (implemented as \file{.so} files). |
| 294 | |
| 295 | The problem is that some entry points are defined by the Python |
| 296 | runtime solely for extension modules to use. If the embedding |
| 297 | application does not use any of these entry points, some linkers will |
| 298 | not include those entries in the symbol table of the finished |
| 299 | executable. Some additional options are needed to inform the linker |
| 300 | not to remove these symbols. |
| 301 | |
| 302 | Determining the right options to use for any given platform can be |
| 303 | quite difficult, but fortunately the Python configuration already has |
| 304 | those values. To retrieve them from an installed Python interpreter, |
| 305 | start an interactive interpreter and have a short session like this: |
| 306 | |
| 307 | \begin{verbatim} |
| 308 | >>> import distutils.sysconfig |
| 309 | >>> distutils.sysconfig.get_config_var('LINKFORSHARED') |
| 310 | '-Xlinker -export-dynamic' |
| 311 | \end{verbatim} |
| 312 | \refstmodindex{distutils.sysconfig} |
| 313 | |
| 314 | The contents of the string presented will be the options that should |
| 315 | be used. If the string is empty, there's no need to add any |
| 316 | additional options. The \constant{LINKFORSHARED} definition |
| 317 | corresponds to the variable of the same name in Python's top-level |
| 318 | \file{Makefile}. |