blob: 99d39ca6e33299ddccbe58e37c58de8e1b72e93f [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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +000013 /* Pass argv[0] to the Python interpreter */
14 Py_SetProgramName(args[0]);
Martin v. Löwis869bad92008-10-17 15:54:44 +000015
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000016 /* Add a static module */
17 PyImport_AppendInittab("xyzzy", PyInit_xyzzy);
Guido van Rossum705d5171994-10-08 19:30:50 +000018
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000019 /* Initialize the Python interpreter. Required. */
20 Py_Initialize();
Guido van Rossum705d5171994-10-08 19:30:50 +000021
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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
Georg Brandlcea7e552010-08-01 18:56:30 +000025 touching sys.argv...)
26
27 If the third argument is true, sys.path is modified to include
28 either the directory containing the script named by argv[0], or
29 the current working directory. This can be risky; if you run
30 an application embedding Python in a directory controlled by
31 someone else, attackers could put a Trojan-horse module in the
32 directory (say, a file named os.py) that your application would
33 then import and run.
34 */
35 PySys_SetArgvEx(2, args, 0);
Guido van Rossum705d5171994-10-08 19:30:50 +000036
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000037 /* Do some application specific code */
38 printf("Hello, brave new world\n\n");
Guido van Rossum705d5171994-10-08 19:30:50 +000039
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000040 /* Execute some Python statements (in module __main__) */
41 PyRun_SimpleString("import sys\n");
42 PyRun_SimpleString("print(sys.builtin_module_names)\n");
43 PyRun_SimpleString("print(sys.modules.keys())\n");
44 PyRun_SimpleString("print(sys.executable)\n");
45 PyRun_SimpleString("print(sys.argv)\n");
Guido van Rossum705d5171994-10-08 19:30:50 +000046
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000047 /* Note that you can call any public function of the Python
48 interpreter here, e.g. call_object(). */
Guido van Rossum705d5171994-10-08 19:30:50 +000049
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000050 /* Some more application specific code */
51 printf("\nGoodbye, cruel world\n");
Guido van Rossum705d5171994-10-08 19:30:50 +000052
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000053 /* Exit, cleaning up the interpreter */
54 Py_Exit(0);
55 /*NOTREACHED*/
Guido van Rossum705d5171994-10-08 19:30:50 +000056}
57
Guido van Rossum81e84c91997-12-25 04:51:41 +000058/* A static module */
59
Thomas Wouters78890102000-07-22 19:25:51 +000060/* 'self' is not used */
Guido van Rossum81e84c91997-12-25 04:51:41 +000061static PyObject *
Guido van Rossum7339f4c2001-01-10 17:09:00 +000062xyzzy_foo(PyObject *self, PyObject* args)
Guido van Rossum81e84c91997-12-25 04:51:41 +000063{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000064 return PyLong_FromLong(42L);
Guido van Rossum81e84c91997-12-25 04:51:41 +000065}
66
67static PyMethodDef xyzzy_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000068 {"foo", xyzzy_foo, METH_NOARGS,
69 "Return the meaning of everything."},
70 {NULL, NULL} /* sentinel */
Guido van Rossum81e84c91997-12-25 04:51:41 +000071};
72
Martin v. Löwis869bad92008-10-17 15:54:44 +000073static struct PyModuleDef xyzzymodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000074 {}, /* m_base */
75 "xyzzy", /* m_name */
76 0, /* m_doc */
77 0, /* m_size */
78 xyzzy_methods, /* m_methods */
79 0, /* m_reload */
80 0, /* m_traverse */
81 0, /* m_clear */
82 0, /* m_free */
Martin v. Löwis869bad92008-10-17 15:54:44 +000083};
84
85PyObject*
86PyInit_xyzzy(void)
Guido van Rossum81e84c91997-12-25 04:51:41 +000087{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000088 return PyModule_Create(&xyzzymodule);
Guido van Rossum81e84c91997-12-25 04:51:41 +000089}