|
Original |
Translation |
|
48
|
This chapter will not discuss how to convert data from Python to C and vice versa. Also, proper use of references and dealing with errors is assumed to be understood. Since these aspects do not differ from extending the interpreter, you can refer to earlier chapters for the required information.
|
|
|
49
|
|
|
|
50
|
The first program aims to execute a function in a Python script. Like in the section about the very high level interface, the Python interpreter does not directly interact with the application (but that will change in the next section).
|
|
|
51
|
The code to run a function defined in a Python script is:
|
|
|
52
|
|
53
|
This code loads a Python script using ``argv[1]``, and calls the function named in ``argv[2]``. Its integer arguments are the other values of the ``argv`` array. If you compile and link this program (let's call the finished executable :program:`call`), and use it to execute a Python script, such as::
|
|
|
54
|
def multiply(a,b): print("Will compute", a, "times", b) c = 0 for i in range(0, a): c = c + b return c
|
|
|
55
|
then the result should be::
|
|
|
56
|
$ call multiply multiply 3 2 Will compute 3 times 2 Result of call: 6
|
|
|
57
|
Although the program is quite large for its functionality, most of the code is for data conversion between Python and C, and for error reporting. The interesting part with respect to embedding Python starts with ::
|
|