blob: 6005f1396bf1b3c963f3c65f8c950e0a39e23272 [file] [log] [blame]
Guido van Rossum705d5171994-10-08 19:30:50 +00001/* Example of embedding Python in another program */
2
Guido van Rossum3caad8c1995-03-28 09:22:53 +00003#include "Python.h"
Guido van Rossum705d5171994-10-08 19:30:50 +00004
Thomas Wouters78890102000-07-22 19:25:51 +00005void initxyzzy(void); /* Forward */
Guido van Rossum81e84c91997-12-25 04:51:41 +00006
Thomas Wouters78890102000-07-22 19:25:51 +00007main(int argc, char **argv)
Guido van Rossum705d5171994-10-08 19:30:50 +00008{
Guido van Rossum496bc7f1999-03-09 17:07:24 +00009 /* Pass argv[0] to the Python interpreter */
10 Py_SetProgramName(argv[0]);
Guido van Rossum705d5171994-10-08 19:30:50 +000011
12 /* Initialize the Python interpreter. Required. */
Guido van Rossum3caad8c1995-03-28 09:22:53 +000013 Py_Initialize();
Guido van Rossum705d5171994-10-08 19:30:50 +000014
Guido van Rossum81e84c91997-12-25 04:51:41 +000015 /* Add a static module */
16 initxyzzy();
17
Guido van Rossum705d5171994-10-08 19:30:50 +000018 /* 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...) */
Guido van Rossum3caad8c1995-03-28 09:22:53 +000022 PySys_SetArgv(argc, argv);
Guido van Rossum705d5171994-10-08 19:30:50 +000023
24 /* Do some application specific code */
25 printf("Hello, brave new world\n\n");
26
27 /* Execute some Python statements (in module __main__) */
Guido van Rossum3caad8c1995-03-28 09:22:53 +000028 PyRun_SimpleString("import sys\n");
29 PyRun_SimpleString("print sys.builtin_module_names\n");
Guido van Rossum81e84c91997-12-25 04:51:41 +000030 PyRun_SimpleString("print sys.modules.keys()\n");
Guido van Rossum496bc7f1999-03-09 17:07:24 +000031 PyRun_SimpleString("print sys.executable\n");
Guido van Rossum3caad8c1995-03-28 09:22:53 +000032 PyRun_SimpleString("print sys.argv\n");
Guido van Rossum705d5171994-10-08 19:30:50 +000033
34 /* Note that you can call any public function of the Python
35 interpreter here, e.g. call_object(). */
36
37 /* Some more application specific code */
38 printf("\nGoodbye, cruel world\n");
39
40 /* Exit, cleaning up the interpreter */
Guido van Rossum3caad8c1995-03-28 09:22:53 +000041 Py_Exit(0);
Guido van Rossum705d5171994-10-08 19:30:50 +000042 /*NOTREACHED*/
43}
44
Guido van Rossum81e84c91997-12-25 04:51:41 +000045/* A static module */
46
Thomas Wouters78890102000-07-22 19:25:51 +000047/* 'self' is not used */
Guido van Rossum81e84c91997-12-25 04:51:41 +000048static PyObject *
Guido van Rossum7339f4c2001-01-10 17:09:00 +000049xyzzy_foo(PyObject *self, PyObject* args)
Guido van Rossum81e84c91997-12-25 04:51:41 +000050{
Guido van Rossum81e84c91997-12-25 04:51:41 +000051 return PyInt_FromLong(42L);
52}
53
54static PyMethodDef xyzzy_methods[] = {
Fred Drake3176b082001-11-17 06:30:20 +000055 {"foo", xyzzy_foo, METH_NOARGS,
56 "Return the meaning of everything."},
Guido van Rossum81e84c91997-12-25 04:51:41 +000057 {NULL, NULL} /* sentinel */
58};
59
60void
Thomas Wouters78890102000-07-22 19:25:51 +000061initxyzzy(void)
Guido van Rossum81e84c91997-12-25 04:51:41 +000062{
63 PyImport_AddModule("xyzzy");
64 Py_InitModule("xyzzy", xyzzy_methods);
65}