1 /* Example of embedding Python in another program */
7 void initxyzzy(); /* Forward */
13 /* Save a copy of argv0 */
16 /* Initialize the Python interpreter. Required. */
19 /* Add a static module */
22 /* Define sys.argv. It is up to the application if you
23 want this; you can also let it undefined (since the Python
24 code is generally not a main program it has no business
25 touching sys.argv...) */
26 PySys_SetArgv(argc
, argv
);
28 /* Do some application specific code */
29 printf("Hello, brave new world\n\n");
31 /* Execute some Python statements (in module __main__) */
32 PyRun_SimpleString("import sys\n");
33 PyRun_SimpleString("print sys.builtin_module_names\n");
34 PyRun_SimpleString("print sys.modules.keys()\n");
35 PyRun_SimpleString("print sys.argv\n");
37 /* Note that you can call any public function of the Python
38 interpreter here, e.g. call_object(). */
40 /* Some more application specific code */
41 printf("\nGoodbye, cruel world\n");
43 /* Exit, cleaning up the interpreter */
48 /* This function is called by the interpreter to get its own name */
59 PyObject
*self
; /* Not used */
62 if (!PyArg_ParseTuple(args
, ""))
64 return PyInt_FromLong(42L);
67 static PyMethodDef xyzzy_methods
[] = {
68 {"foo", xyzzy_foo
, 1},
69 {NULL
, NULL
} /* sentinel */
75 PyImport_AddModule("xyzzy");
76 Py_InitModule("xyzzy", xyzzy_methods
);