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