blob: b9ea6f57b9e3323d280346337a151ca20b40e038 [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{
Martin v. Löwis869bad92008-10-17 15:54:44 +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};
12
Guido van Rossum496bc7f1999-03-09 17:07:24 +000013 /* Pass argv[0] to the Python interpreter */
Martin v. Löwis869bad92008-10-17 15:54:44 +000014 Py_SetProgramName(args[0]);
15
16 /* Add a static module */
17 PyImport_AppendInittab("xyzzy", PyInit_xyzzy);
Guido van Rossum705d5171994-10-08 19:30:50 +000018
19 /* Initialize the Python interpreter. Required. */
Guido van Rossum3caad8c1995-03-28 09:22:53 +000020 Py_Initialize();
Guido van Rossum705d5171994-10-08 19:30:50 +000021
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...) */
Martin v. Löwis869bad92008-10-17 15:54:44 +000026 PySys_SetArgv(2, args);
Guido van Rossum705d5171994-10-08 19:30:50 +000027
28 /* Do some application specific code */
29 printf("Hello, brave new world\n\n");
30
31 /* Execute some Python statements (in module __main__) */
Guido van Rossum3caad8c1995-03-28 09:22:53 +000032 PyRun_SimpleString("import sys\n");
Martin v. Löwis869bad92008-10-17 15:54:44 +000033 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
38 /* Note that you can call any public function of the Python
39 interpreter here, e.g. call_object(). */
40
41 /* Some more application specific code */
42 printf("\nGoodbye, cruel world\n");
43
44 /* Exit, cleaning up the interpreter */
Guido van Rossum3caad8c1995-03-28 09:22:53 +000045 Py_Exit(0);
Guido van Rossum705d5171994-10-08 19:30:50 +000046 /*NOTREACHED*/
47}
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{
Christian Heimes217cfd12007-12-02 14:31:20 +000055 return PyLong_FromLong(42L);
Guido van Rossum81e84c91997-12-25 04:51:41 +000056}
57
58static PyMethodDef xyzzy_methods[] = {
Fred Drake3176b082001-11-17 06:30:20 +000059 {"foo", xyzzy_foo, METH_NOARGS,
60 "Return the meaning of everything."},
Guido van Rossum81e84c91997-12-25 04:51:41 +000061 {NULL, NULL} /* sentinel */
62};
63
Martin v. Löwis869bad92008-10-17 15:54:44 +000064static struct PyModuleDef xyzzymodule = {
65 {}, /* 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 */
74};
75
76PyObject*
77PyInit_xyzzy(void)
Guido van Rossum81e84c91997-12-25 04:51:41 +000078{
Martin v. Löwis869bad92008-10-17 15:54:44 +000079 return PyModule_Create(&xyzzymodule);
Guido van Rossum81e84c91997-12-25 04:51:41 +000080}