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