1 /* Example of embedding Python in another program */
5 void initxyzzy(void); /* Forward */
7 main(int argc
, char **argv
)
9 /* Pass argv[0] to the Python interpreter */
10 Py_SetProgramName(argv
[0]);
12 /* Initialize the Python interpreter. Required. */
15 /* Add a static module */
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...) */
22 PySys_SetArgv(argc
, argv
);
24 /* Do some application specific code */
25 printf("Hello, brave new world\n\n");
27 /* Execute some Python statements (in module __main__) */
28 PyRun_SimpleString("import sys\n");
29 PyRun_SimpleString("print sys.builtin_module_names\n");
30 PyRun_SimpleString("print sys.modules.keys()\n");
31 PyRun_SimpleString("print sys.executable\n");
32 PyRun_SimpleString("print sys.argv\n");
34 /* Note that you can call any public function of the Python
35 interpreter here, e.g. call_object(). */
37 /* Some more application specific code */
38 printf("\nGoodbye, cruel world\n");
40 /* Exit, cleaning up the interpreter */
47 /* 'self' is not used */
49 xyzzy_foo(PyObject
*self
, PyObject
* args
)
51 return PyInt_FromLong(42L);
54 static PyMethodDef xyzzy_methods
[] = {
55 {"foo", xyzzy_foo
, METH_NOARGS
,
56 "Return the meaning of everything."},
57 {NULL
, NULL
} /* sentinel */
63 PyImport_AddModule("xyzzy");
64 Py_InitModule("xyzzy", xyzzy_methods
);