]> Dogcows Code - chaz/openbox/blob - src/python.cc
execute files such that i can track if an exception was thrown in it
[chaz/openbox] / src / python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "python.hh"
4 #include "openbox.hh"
5 #include "actions.hh"
6 #include "python.hh"
7 #include "bindings.hh"
8 #include "otk/display.hh"
9 #include "otk/util.hh"
10
11 extern "C" {
12 #include <Python.h>
13
14 #include "gettext.h"
15 #define _(str) gettext(str)
16 }
17
18 namespace ob {
19
20 void python_init(char *argv0)
21 {
22 // start the python engine
23 Py_SetProgramName(argv0);
24 Py_Initialize();
25 // prepend the openbox directories for python scripts to the sys path
26 PyRun_SimpleString("import sys");
27 PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
28 PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
29 otk::expandTilde("~/.openbox/python") +
30 "')").c_str()));
31 }
32
33 void python_destroy()
34 {
35 Py_Finalize();
36 }
37
38 bool python_exec(const std::string &path)
39 {
40 FILE *rcpyfd = fopen(path.c_str(), "r");
41 if (!rcpyfd) {
42 fprintf(stderr, _("Unabled to open python file %s\n"), path.c_str());
43 return false;
44 }
45
46 //PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
47
48 PyObject *module = PyImport_AddModule("__main__");
49 assert(module);
50 PyObject *dict = PyModule_GetDict(module);
51 assert(dict);
52 PyObject *result = PyRun_File(rcpyfd, const_cast<char*>(path.c_str()),
53 Py_file_input, dict, dict);
54 bool ret = result != NULL;
55 if (result == NULL)
56 PyErr_Print();
57
58 Py_XDECREF(result);
59
60 Py_DECREF(dict);
61
62 fclose(rcpyfd);
63 return ret;
64 }
65
66 }
This page took 0.037926 seconds and 5 git commands to generate.