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 | |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame^] | 7 | void initxyzzy(); /* Forward */ |
| 8 | |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 9 | main(argc, argv) |
| 10 | int argc; |
| 11 | char **argv; |
| 12 | { |
| 13 | /* Save a copy of argv0 */ |
| 14 | argv0 = argv[0]; |
| 15 | |
| 16 | /* Initialize the Python interpreter. Required. */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame] | 17 | Py_Initialize(); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame^] | 19 | /* Add a static module */ |
| 20 | initxyzzy(); |
| 21 | |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 22 | /* Define sys.argv. It is up to the application if you |
| 23 | want this; you can also let it undefined (since the Python |
| 24 | code is generally not a main program it has no business |
| 25 | touching sys.argv...) */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame] | 26 | PySys_SetArgv(argc, argv); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 27 | |
| 28 | /* Do some application specific code */ |
| 29 | printf("Hello, brave new world\n\n"); |
| 30 | |
| 31 | /* Execute some Python statements (in module __main__) */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame] | 32 | PyRun_SimpleString("import sys\n"); |
| 33 | PyRun_SimpleString("print sys.builtin_module_names\n"); |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame^] | 34 | PyRun_SimpleString("print sys.modules.keys()\n"); |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame] | 35 | PyRun_SimpleString("print sys.argv\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 36 | |
| 37 | /* Note that you can call any public function of the Python |
| 38 | interpreter here, e.g. call_object(). */ |
| 39 | |
| 40 | /* Some more application specific code */ |
| 41 | printf("\nGoodbye, cruel world\n"); |
| 42 | |
| 43 | /* Exit, cleaning up the interpreter */ |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame] | 44 | Py_Exit(0); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 45 | /*NOTREACHED*/ |
| 46 | } |
| 47 | |
Guido van Rossum | 3caad8c | 1995-03-28 09:22:53 +0000 | [diff] [blame] | 48 | /* 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] | 49 | char * |
| 50 | getprogramname() |
| 51 | { |
| 52 | return argv0; |
| 53 | } |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame^] | 54 | |
| 55 | /* A static module */ |
| 56 | |
| 57 | static PyObject * |
| 58 | xyzzy_foo(self, args) |
| 59 | PyObject *self; /* Not used */ |
| 60 | PyObject *args; |
| 61 | { |
| 62 | if (!PyArg_ParseTuple(args, "")) |
| 63 | return NULL; |
| 64 | return PyInt_FromLong(42L); |
| 65 | } |
| 66 | |
| 67 | static PyMethodDef xyzzy_methods[] = { |
| 68 | {"foo", xyzzy_foo, 1}, |
| 69 | {NULL, NULL} /* sentinel */ |
| 70 | }; |
| 71 | |
| 72 | void |
| 73 | initxyzzy() |
| 74 | { |
| 75 | PyImport_AddModule("xyzzy"); |
| 76 | Py_InitModule("xyzzy", xyzzy_methods); |
| 77 | } |