blob: 22bfaff4fd2e395e2bc8a819510714e30b55db22 [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
Martin v. Löwis869bad92008-10-17 15:54:44 +00005PyObject* PyInit_xyzzy(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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009 /* Ignore passed-in argc/argv. If desired, conversion
10 should use mbstowcs to convert them. */
11 wchar_t *args[] = {L"embed", L"hello", 0};
Martin v. Löwis869bad92008-10-17 15:54:44 +000012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 /* Pass argv[0] to the Python interpreter */
14 Py_SetProgramName(args[0]);
Martin v. Löwis869bad92008-10-17 15:54:44 +000015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 /* Add a static module */
17 PyImport_AppendInittab("xyzzy", PyInit_xyzzy);
Guido van Rossum705d5171994-10-08 19:30:50 +000018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 /* Initialize the Python interpreter. Required. */
20 Py_Initialize();
Guido van Rossum705d5171994-10-08 19:30:50 +000021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 /* 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(2, args);
Guido van Rossum705d5171994-10-08 19:30:50 +000027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 /* Do some application specific code */
29 printf("Hello, brave new world\n\n");
Guido van Rossum705d5171994-10-08 19:30:50 +000030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 /* 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.executable)\n");
36 PyRun_SimpleString("print(sys.argv)\n");
Guido van Rossum705d5171994-10-08 19:30:50 +000037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 /* Note that you can call any public function of the Python
39 interpreter here, e.g. call_object(). */
Guido van Rossum705d5171994-10-08 19:30:50 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 /* Some more application specific code */
42 printf("\nGoodbye, cruel world\n");
Guido van Rossum705d5171994-10-08 19:30:50 +000043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 /* Exit, cleaning up the interpreter */
45 Py_Exit(0);
46 /*NOTREACHED*/
Guido van Rossum705d5171994-10-08 19:30:50 +000047}
48
Guido van Rossum81e84c91997-12-25 04:51:41 +000049/* A static module */
50
Thomas Wouters78890102000-07-22 19:25:51 +000051/* 'self' is not used */
Guido van Rossum81e84c91997-12-25 04:51:41 +000052static PyObject *
Guido van Rossum7339f4c2001-01-10 17:09:00 +000053xyzzy_foo(PyObject *self, PyObject* args)
Guido van Rossum81e84c91997-12-25 04:51:41 +000054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 return PyLong_FromLong(42L);
Guido van Rossum81e84c91997-12-25 04:51:41 +000056}
57
58static PyMethodDef xyzzy_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 {"foo", xyzzy_foo, METH_NOARGS,
60 "Return the meaning of everything."},
61 {NULL, NULL} /* sentinel */
Guido van Rossum81e84c91997-12-25 04:51:41 +000062};
63
Martin v. Löwis869bad92008-10-17 15:54:44 +000064static struct PyModuleDef xyzzymodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 {}, /* m_base */
66 "xyzzy", /* m_name */
67 0, /* m_doc */
68 0, /* m_size */
69 xyzzy_methods, /* m_methods */
70 0, /* m_reload */
71 0, /* m_traverse */
72 0, /* m_clear */
73 0, /* m_free */
Martin v. Löwis869bad92008-10-17 15:54:44 +000074};
75
76PyObject*
77PyInit_xyzzy(void)
Guido van Rossum81e84c91997-12-25 04:51:41 +000078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 return PyModule_Create(&xyzzymodule);
Guido van Rossum81e84c91997-12-25 04:51:41 +000080}