Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 1 | /* Example of embedding Python in another program */ |
| 2 | |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame^] | 3 | #include "Python.h" |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 4 | |
| 5 | static char *argv0; |
| 6 | |
| 7 | main(argc, argv) |
| 8 | int argc; |
| 9 | char **argv; |
| 10 | { |
| 11 | /* Save a copy of argv0 */ |
| 12 | argv0 = argv[0]; |
| 13 | |
| 14 | /* Initialize the Python interpreter. Required. */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame^] | 15 | Py_Initialize(); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 16 | |
| 17 | /* Define sys.argv. It is up to the application if you |
| 18 | want this; you can also let it undefined (since the Python |
| 19 | code is generally not a main program it has no business |
| 20 | touching sys.argv...) */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame^] | 21 | PySys_SetArgv(argc, argv); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 22 | |
| 23 | /* Do some application specific code */ |
| 24 | printf("Hello, brave new world\n\n"); |
| 25 | |
| 26 | /* Execute some Python statements (in module __main__) */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame^] | 27 | PyRun_SimpleString("import sys\n"); |
| 28 | PyRun_SimpleString("print sys.builtin_module_names\n"); |
| 29 | PyRun_SimpleString("print sys.argv\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 30 | |
| 31 | /* Note that you can call any public function of the Python |
| 32 | interpreter here, e.g. call_object(). */ |
| 33 | |
| 34 | /* Some more application specific code */ |
| 35 | printf("\nGoodbye, cruel world\n"); |
| 36 | |
| 37 | /* Exit, cleaning up the interpreter */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame^] | 38 | Py_Exit(0); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 39 | /*NOTREACHED*/ |
| 40 | } |
| 41 | |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame^] | 42 | /* This function is called by the interpreter to get its own name */ |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 43 | char * |
| 44 | getprogramname() |
| 45 | { |
| 46 | return argv0; |
| 47 | } |