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 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +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 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 12 | /* Initialize the Python interpreter. Required. */ |
| 13 | Py_Initialize(); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 14 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 15 | /* Add a static module */ |
| 16 | initxyzzy(); |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 17 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 18 | /* Define sys.argv. It is up to the application if you |
Andrew M. Kuchling | de0aeaa | 2010-06-11 00:16:08 +0000 | [diff] [blame] | 19 | want this; you can also leave it undefined (since the Python |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 20 | code is generally not a main program it has no business |
Andrew M. Kuchling | de0aeaa | 2010-06-11 00:16:08 +0000 | [diff] [blame] | 21 | touching sys.argv...) |
| 22 | |
| 23 | If the third argument is true, sys.path is modified to include |
| 24 | either the directory containing the script named by argv[0], or |
| 25 | the current working directory. This can be risky; if you run |
| 26 | an application embedding Python in a directory controlled by |
| 27 | someone else, attackers could put a Trojan-horse module in the |
| 28 | directory (say, a file named os.py) that your application would |
| 29 | then import and run. |
| 30 | */ |
| 31 | PySys_SetArgvEx(argc, argv, 0); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 32 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 33 | /* Do some application specific code */ |
| 34 | printf("Hello, brave new world\n\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 36 | /* Execute some Python statements (in module __main__) */ |
| 37 | PyRun_SimpleString("import sys\n"); |
| 38 | PyRun_SimpleString("print sys.builtin_module_names\n"); |
| 39 | PyRun_SimpleString("print sys.modules.keys()\n"); |
| 40 | PyRun_SimpleString("print sys.executable\n"); |
| 41 | PyRun_SimpleString("print sys.argv\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 43 | /* Note that you can call any public function of the Python |
| 44 | interpreter here, e.g. call_object(). */ |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 46 | /* Some more application specific code */ |
| 47 | printf("\nGoodbye, cruel world\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 48 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 49 | /* Exit, cleaning up the interpreter */ |
| 50 | Py_Exit(0); |
| 51 | /*NOTREACHED*/ |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 54 | /* A static module */ |
| 55 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 56 | /* 'self' is not used */ |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 57 | static PyObject * |
Guido van Rossum | 7339f4c | 2001-01-10 17:09:00 +0000 | [diff] [blame] | 58 | xyzzy_foo(PyObject *self, PyObject* args) |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 59 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 60 | return PyInt_FromLong(42L); |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static PyMethodDef xyzzy_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 64 | {"foo", xyzzy_foo, METH_NOARGS, |
| 65 | "Return the meaning of everything."}, |
| 66 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 70 | initxyzzy(void) |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 71 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 72 | PyImport_AddModule("xyzzy"); |
| 73 | Py_InitModule("xyzzy", xyzzy_methods); |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 74 | } |