extern "C" {
#include <Python.h>
+
+#include "gettext.h"
+#define _(str) gettext(str)
}
namespace ob {
Py_Finalize();
}
-bool python_exec(const std::string &path)
+int python_exec(const std::string &path)
{
FILE *rcpyfd = fopen(path.c_str(), "r");
if (!rcpyfd) {
- printf("Failed to load python file %s\n", path.c_str());
- return false;
+ fprintf(stderr, _("Unabled to open python file %s\n"), path.c_str());
+ return 1;
}
- PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
+
+ //PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
+
+ PyObject *module = PyImport_AddModule("__main__");
+ assert(module);
+ PyObject *dict = PyModule_GetDict(module);
+ assert(dict);
+ PyObject *result = PyRun_File(rcpyfd, const_cast<char*>(path.c_str()),
+ Py_file_input, dict, dict);
+ int ret = result == NULL ? 2 : 0;
+ if (result == NULL)
+ PyErr_Print();
+
+ Py_XDECREF(result);
+
+ Py_DECREF(dict);
+
fclose(rcpyfd);
- return true;
+ return ret;
}
}