Fix the HTML tarball target to generate the HTML if needed instead of
[python/dscho.git] / Demo / embed / demo.c
blob9a1883f2f4d01872dd73298eb3feddd9512ce0a7
1 /* Example of embedding Python in another program */
3 #include "Python.h"
5 static char *argv0;
7 void initxyzzy(); /* Forward */
9 main(argc, argv)
10 int argc;
11 char **argv;
13 /* Save a copy of argv0 */
14 argv0 = argv[0];
16 /* Initialize the Python interpreter. Required. */
17 Py_Initialize();
19 /* Add a static module */
20 initxyzzy();
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 */
44 Py_Exit(0);
45 /*NOTREACHED*/
48 /* This function is called by the interpreter to get its own name */
49 char *
50 getprogramname()
52 return argv0;
55 /* A static module */
57 static PyObject *
58 xyzzy_foo(self, args)
59 PyObject *self; /* Not used */
60 PyObject *args;
62 if (!PyArg_ParseTuple(args, ""))
63 return NULL;
64 return PyInt_FromLong(42L);
67 static PyMethodDef xyzzy_methods[] = {
68 {"foo", xyzzy_foo, 1},
69 {NULL, NULL} /* sentinel */
72 void
73 initxyzzy()
75 PyImport_AddModule("xyzzy");
76 Py_InitModule("xyzzy", xyzzy_methods);