diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-29 03:19:48 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-29 03:19:48 +0000 |
commit | 163a913a226cca335544231da5051562891c286b (patch) | |
tree | d7a3dbe8f0866575a356966a39a389396011f54c /sca-cpp/trunk/modules/python/eval.hpp | |
parent | 88bf2a256b02e1858993bf097f4dc743d389e3f0 (diff) |
Make sure components execute in the correct contribution directory and fix support for Python relative module imports.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@990483 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk/modules/python/eval.hpp')
-rw-r--r-- | sca-cpp/trunk/modules/python/eval.hpp | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/sca-cpp/trunk/modules/python/eval.hpp b/sca-cpp/trunk/modules/python/eval.hpp index 855804a5ae..daccb16ec7 100644 --- a/sca-cpp/trunk/modules/python/eval.hpp +++ b/sca-cpp/trunk/modules/python/eval.hpp @@ -42,6 +42,8 @@ public: if (Py_IsInitialized()) return; Py_InitializeEx(0); + const char* arg0 = ""; + PySys_SetArgv(0, const_cast<char**>(&arg0)); } }; @@ -133,7 +135,6 @@ PyObject *mkPyLambda(const lambda<value(const list<value>&)>& l) { /** * Convert a list of values to a python list. */ - PyObject* valuesToPyListHelper(PyObject* l, const list<value>& v) { if (isNil(v)) return l; @@ -231,6 +232,13 @@ const value pyObjectToValue(PyObject *o) { } /** + * Convert a python script path to a module name. + */ +const string moduleName(const string& path) { + return join(".", tokenize("/", substr(path, 0, length(path) -3))); +} + +/** * Evaluate an expression against a script provided as a python object. */ const failable<value> evalScript(const value& expr, PyObject* script) { @@ -272,14 +280,14 @@ const failable<value> evalScript(const value& expr, PyObject* script) { /** * Read a python script from an input stream. */ -const failable<PyObject*> readScript(const string& path, istream& is) { +const failable<PyObject*> readScript(const string& name, const string& path, istream& is) { const list<string> ls = streamList(is); ostringstream os; write(ls, os); PyObject* code = Py_CompileStringFlags(c_str(str(os)), c_str(path), Py_file_input, NULL); if (code == NULL) return mkfailure<PyObject*>(string("Couldn't compile script: ") + path + " : " + lastError()); - PyObject* mod = PyImport_ExecCodeModule(const_cast<char*>(c_str(path)), code); + PyObject* mod = PyImport_ExecCodeModuleEx(const_cast<char*>(c_str(name)), code, const_cast<char*>(c_str(path))); if (mod == NULL) return mkfailure<PyObject*>(string("Couldn't import module: ") + path + " : " + lastError()); return mod; @@ -289,7 +297,7 @@ const failable<PyObject*> readScript(const string& path, istream& is) { * Evaluate an expression against a script provided as an input stream. */ const failable<value> evalScript(const value& expr, istream& is) { - failable<PyObject*> script = readScript("script", is); + failable<PyObject*> script = readScript("script", "script.py", is); if (!hasContent(script)) return mkfailure<value>(reason(script)); return evalScript(expr, content(script)); |