Jack Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 1 | /* Example of embedding Python in another program */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | #ifdef macintosh |
| 5 | #include "macglue.h" |
| 6 | #include <SIOUX.h> |
| 7 | #endif /* macintosh */ |
| 8 | |
| 9 | static char *argv0; |
| 10 | |
| 11 | main(argc, argv) |
| 12 | int argc; |
| 13 | char **argv; |
| 14 | { |
| 15 | #ifdef macintosh |
| 16 | /* So the user can set argc/argv to something interesting */ |
| 17 | argc = ccommand(&argv); |
| 18 | #endif |
| 19 | /* Save a copy of argv0 */ |
| 20 | argv0 = argv[0]; |
| 21 | |
| 22 | /* Initialize the Python interpreter. Required. */ |
| 23 | #ifdef macintosh |
| 24 | PyMac_Initialize(); |
| 25 | #else |
| 26 | Py_Initialize(); |
| 27 | #endif |
| 28 | |
| 29 | /* Define sys.argv. It is up to the application if you |
| 30 | want this; you can also let it undefined (since the Python |
| 31 | code is generally not a main program it has no business |
| 32 | touching sys.argv...) */ |
| 33 | PySys_SetArgv(argc, argv); |
| 34 | |
| 35 | /* Do some application specific code */ |
| 36 | printf("Hello, brave new world\n\n"); |
| 37 | |
| 38 | /* Execute some Python statements (in module __main__) */ |
| 39 | PyRun_SimpleString("import sys\n"); |
| 40 | PyRun_SimpleString("print sys.builtin_module_names\n"); |
| 41 | PyRun_SimpleString("print sys.argv\n"); |
| 42 | |
| 43 | /* Note that you can call any public function of the Python |
| 44 | interpreter here, e.g. call_object(). */ |
| 45 | |
| 46 | /* Some more application specific code */ |
| 47 | printf("\nGoodbye, cruel world\n"); |
| 48 | #ifdef macintosh |
| 49 | printf("Type return or so-\n"); |
| 50 | getchar(); |
| 51 | #endif |
| 52 | /* Exit, cleaning up the interpreter */ |
| 53 | Py_Exit(0); |
| 54 | /*NOTREACHED*/ |
| 55 | } |
| 56 | |
| 57 | /* This function is called by the interpreter to get its own name */ |
| 58 | char * |
| 59 | getprogramname() |
| 60 | { |
| 61 | return argv0; |
| 62 | } |