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 | |
Martin v. Löwis | 869bad9 | 2008-10-17 15:54:44 +0000 | [diff] [blame] | 5 | PyObject* PyInit_xyzzy(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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 9 | /* Ignore passed-in argc/argv. If desired, conversion |
| 10 | should use mbstowcs to convert them. */ |
| 11 | wchar_t *args[] = {L"embed", L"hello", 0}; |
Martin v. Löwis | 869bad9 | 2008-10-17 15:54:44 +0000 | [diff] [blame] | 12 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | /* Pass argv[0] to the Python interpreter */ |
| 14 | Py_SetProgramName(args[0]); |
Martin v. Löwis | 869bad9 | 2008-10-17 15:54:44 +0000 | [diff] [blame] | 15 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 16 | /* Add a static module */ |
| 17 | PyImport_AppendInittab("xyzzy", PyInit_xyzzy); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 18 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | /* Initialize the Python interpreter. Required. */ |
| 20 | Py_Initialize(); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 21 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | /* Define sys.argv. It is up to the application if you |
Benjamin Peterson | 2ebf8ce | 2010-06-27 21:48:35 +0000 | [diff] [blame^] | 23 | want this; you can also leave it undefined (since the Python |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | code is generally not a main program it has no business |
Benjamin Peterson | 2ebf8ce | 2010-06-27 21:48:35 +0000 | [diff] [blame^] | 25 | touching sys.argv...) |
| 26 | |
| 27 | If the third argument is true, sys.path is modified to include |
| 28 | either the directory containing the script named by argv[0], or |
| 29 | the current working directory. This can be risky; if you run |
| 30 | an application embedding Python in a directory controlled by |
| 31 | someone else, attackers could put a Trojan-horse module in the |
| 32 | directory (say, a file named os.py) that your application would |
| 33 | then import and run. |
| 34 | */ |
| 35 | PySys_SetArgvEx(argc, argv, 0); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | /* Do some application specific code */ |
| 38 | printf("Hello, brave new world\n\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 39 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | /* Execute some Python statements (in module __main__) */ |
| 41 | PyRun_SimpleString("import sys\n"); |
| 42 | PyRun_SimpleString("print(sys.builtin_module_names)\n"); |
| 43 | PyRun_SimpleString("print(sys.modules.keys())\n"); |
| 44 | PyRun_SimpleString("print(sys.executable)\n"); |
| 45 | PyRun_SimpleString("print(sys.argv)\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 46 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | /* Note that you can call any public function of the Python |
| 48 | interpreter here, e.g. call_object(). */ |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | /* Some more application specific code */ |
| 51 | printf("\nGoodbye, cruel world\n"); |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 52 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | /* Exit, cleaning up the interpreter */ |
| 54 | Py_Exit(0); |
| 55 | /*NOTREACHED*/ |
Guido van Rossum | 705d517 | 1994-10-08 19:30:50 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 58 | /* A static module */ |
| 59 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 60 | /* 'self' is not used */ |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 61 | static PyObject * |
Guido van Rossum | 7339f4c | 2001-01-10 17:09:00 +0000 | [diff] [blame] | 62 | xyzzy_foo(PyObject *self, PyObject* args) |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 63 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | return PyLong_FromLong(42L); |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static PyMethodDef xyzzy_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | {"foo", xyzzy_foo, METH_NOARGS, |
| 69 | "Return the meaning of everything."}, |
| 70 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Martin v. Löwis | 869bad9 | 2008-10-17 15:54:44 +0000 | [diff] [blame] | 73 | static struct PyModuleDef xyzzymodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | {}, /* m_base */ |
| 75 | "xyzzy", /* m_name */ |
| 76 | 0, /* m_doc */ |
| 77 | 0, /* m_size */ |
| 78 | xyzzy_methods, /* m_methods */ |
| 79 | 0, /* m_reload */ |
| 80 | 0, /* m_traverse */ |
| 81 | 0, /* m_clear */ |
| 82 | 0, /* m_free */ |
Martin v. Löwis | 869bad9 | 2008-10-17 15:54:44 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | PyObject* |
| 86 | PyInit_xyzzy(void) |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 87 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | return PyModule_Create(&xyzzymodule); |
Guido van Rossum | 81e84c9 | 1997-12-25 04:51:41 +0000 | [diff] [blame] | 89 | } |