Allow comment characters (#) to be escaped:
[python/dscho.git] / Demo / embed / demo.c
blob579ba0706e5414843688a2dbea9b57d546796361
1 /* Example of embedding Python in another program */
3 #include "Python.h"
5 void initxyzzy(); /* Forward */
7 main(argc, argv)
8 int argc;
9 char **argv;
11 /* Pass argv[0] to the Python interpreter */
12 Py_SetProgramName(argv[0]);
14 /* Initialize the Python interpreter. Required. */
15 Py_Initialize();
17 /* Add a static module */
18 initxyzzy();
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 */
43 Py_Exit(0);
44 /*NOTREACHED*/
47 /* A static module */
49 static PyObject *
50 xyzzy_foo(self, args)
51 PyObject *self; /* Not used */
52 PyObject *args;
54 if (!PyArg_ParseTuple(args, ""))
55 return NULL;
56 return PyInt_FromLong(42L);
59 static PyMethodDef xyzzy_methods[] = {
60 {"foo", xyzzy_foo, 1},
61 {NULL, NULL} /* sentinel */
64 void
65 initxyzzy()
67 PyImport_AddModule("xyzzy");
68 Py_InitModule("xyzzy", xyzzy_methods);