python is simple and powerful.
anyone new to Python may have the following question: what is the use of python. But to know what it can do, you must first learn to use it. Python syntax is quite simple. Here is a tutorial: Compile.
I have been using C for most of the time. Therefore, calling python in C is a topic of interest to me. I read the Code of some online predecessors, and I also have a general understanding.
1. setting the compiling environment is actually setting the python header file and library file directory
2. initialize the python Interpreter:
py_initialize ();
3. call python
4. release resources
py_finalize ();
We should focus on the third step, calling python. In C, we can directly call Python scripts or call functions in Python.
1. directly call python. You can use the following functions:Pyrun_simplestring ().
2. Call functions in Python. The key issue is how to transfer the type in C to Python. For this reason, Python provides such a function:
Pyobject * py_buildvalue (const char * format ,...);
Pyobject * py_vabuildvalue (const char * format, va_list vargs );
For details about how to use these functions, refer to the python instructions.
Here I implemented a function to call functions in Python simply:
View code
1 /*
2 Module: name of the python Module
3 Function: name of the function to be called.
4 Format: Variable Parameter template passed to the py_vabuildvalue Function
5 */
6 Int Pycall ( Const Char * Module, Const Char * Function, Const Char * Format ,...)
7 {
8 Pyobject * Pmod = NULL;
9 Pyobject * Pfunc = NULL;
10 Pyobject * Pparm = NULL;
11 Pyobject * Pretval = NULL;
12
13 // Import Module
14 If ( ! (Pmod = Pyimport_importmodule (module ))){
15 Return - 1 ;
16 }
17 // Search functions
18 If ( ! (Pfunc = Pyobject_getattrstring (pmod, function ))){
19 Return - 2 ;
20 }
21
22 // Create Parameters
23 Va_list vargs;
24 Va_start (vargs, format );
25 Pparm = Py_vabuildvalue (format, vargs );
26 Va_end (vargs );
27
28 // Function call
29 Pretval = Pyeval_callobject (pfunc, pparm );
30
31 // Assume that the returned value is an integer.
32 Int RET;
33 Pyarg_parse (pretval, " I " , & RET );
34 Return RET;
35 }
36
37 Int Main ( Int Argc, Char * Argv [])
38 {
39 Py_initialize ();
40
41 /* This is a simple description.
42 ** The format parameter is expanded with () to indicate the meaning of the tuples. The number of tuples corresponds to the parameters in the Python script.
43 ** Because the number of parameters of the fun, fun1, and fun2 functions is 0, 1, and 2, the number of tuples must be 0, 1, or 2. Otherwise, the call will fail.
44 ** Failed. Of course, each element in the tuples can be of any type, for example:
45 ** Pycall ("pytest", "fun2", "(I, {s: S, S: s})", 2, "name", "linxr ", "Age", "25 ");
46 */
47 Printf ( " Ret = % d \ n " , Pycall ( " Pytest " , " Fun " , " () " );
48 Printf ( " Ret = % d \ n " , Pycall ( " Pytest " , " Fun1 " , " (I) " , 12 );
49 Printf ( " Ret = % d \ n " , Pycall ( " Pytest " , " Fun2 " , " (Is) " , 12 , " 12 " );
50
51 Py_finalize ();
52
53 Return 0 ;
54 }