]> icculus.org git repositories - mikachu/openbox.git/blob - c/python.c
merge the C branch into HEAD
[mikachu/openbox.git] / c / python.c
1 #include <Python.h>
2 #include <glib.h>
3
4 #ifdef HAVE_STDLIB_H
5 #  include <stdlib.h>
6 #endif
7
8 void python_startup()
9 {
10     PyObject *sys, *sysdict, *syspath, *path1, *path2;
11     char *home, *homescriptdir;
12
13     Py_Initialize();
14
15     /* fix up the system path */
16
17     sys = PyImport_ImportModule((char*)"sys"); /* new */
18     sysdict = PyModule_GetDict(sys); /* borrowed */
19     syspath = PyDict_GetItemString(sysdict, (char*)"path"); /* borrowed */
20
21     path1 = PyString_FromString(SCRIPTDIR); /* new */
22     PyList_Insert(syspath, 0, path1);
23     Py_DECREF(path1);
24
25     home = getenv("HOME");
26     if (home != NULL) {
27         homescriptdir = g_strdup_printf("%s/.openbox", home);
28         path2 = PyString_FromString(homescriptdir); /* new */
29         g_free(homescriptdir);
30
31         PyList_Insert(syspath, 0, path2);
32         Py_DECREF(path2);
33     } else
34         g_warning("Failed to read the $HOME environment variable");
35
36     Py_DECREF(sys);
37 }
38
39 void python_shutdown()
40 {
41     Py_Finalize();
42 }
43
44 gboolean python_import(char *module)
45 {
46     PyObject *mod;
47
48     mod = PyImport_ImportModule(module); /* new */
49     if (mod == NULL) {
50         PyErr_Print();
51         return FALSE;
52     }
53     Py_DECREF(mod);
54     return TRUE;
55 }