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