1 /* Example of embedding Python in another program */
5 void initxyzzy(); /* Forward */
11 /* Pass argv[0] to the Python interpreter */
12 Py_SetProgramName(argv
[0]);
14 /* Initialize the Python interpreter. Required. */
17 /* Add a static module */
20 /* Define sys.argv. It is up to the application if you
21 want this; you can also let it undefined (since the Python
22 code is generally not a main program it has no business
23 touching sys.argv...) */
24 PySys_SetArgv(argc
, argv
);
26 /* Do some application specific code */
27 printf("Hello, brave new world\n\n");
29 /* Execute some Python statements (in module __main__) */
30 PyRun_SimpleString("import sys\n");
31 PyRun_SimpleString("print sys.builtin_module_names\n");
32 PyRun_SimpleString("print sys.modules.keys()\n");
33 PyRun_SimpleString("print sys.executable\n");
34 PyRun_SimpleString("print sys.argv\n");
36 /* Note that you can call any public function of the Python
37 interpreter here, e.g. call_object(). */
39 /* Some more application specific code */
40 printf("\nGoodbye, cruel world\n");
42 /* Exit, cleaning up the interpreter */
51 PyObject
*self
; /* Not used */
54 if (!PyArg_ParseTuple(args
, ""))
56 return PyInt_FromLong(42L);
59 static PyMethodDef xyzzy_methods
[] = {
60 {"foo", xyzzy_foo
, 1},
61 {NULL
, NULL
} /* sentinel */
67 PyImport_AddModule("xyzzy");
68 Py_InitModule("xyzzy", xyzzy_methods
);