diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-11 08:29:31 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-11 08:29:31 +0000 |
commit | 7d44c7ee8fee7bc6f401c47b5268778500b25558 (patch) | |
tree | 94fcb8e978f198e8108c2ba82ff43018e41bd259 /sca-cpp/trunk | |
parent | c2add2e09088fc3640d2ad784ddfd56d1ad75f7b (diff) |
Cleaned up python support code, removed unused functions.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@897787 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk')
-rw-r--r-- | sca-cpp/trunk/modules/python/driver.hpp | 43 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/python/eval.hpp | 64 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/python/io.hpp | 56 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/python/python-shell.cpp | 6 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/python/python-test.cpp | 46 |
5 files changed, 51 insertions, 164 deletions
diff --git a/sca-cpp/trunk/modules/python/driver.hpp b/sca-cpp/trunk/modules/python/driver.hpp index dced219fa0..a1467c41a8 100644 --- a/sca-cpp/trunk/modules/python/driver.hpp +++ b/sca-cpp/trunk/modules/python/driver.hpp @@ -19,8 +19,8 @@ /* $Rev$ $Date$ */ -#ifndef tuscany_python_pydriver_hpp -#define tuscany_python_pydriver_hpp +#ifndef tuscany_python_driver_hpp +#define tuscany_python_driver_hpp /** * Python evaluator main driver loop. @@ -29,48 +29,25 @@ #include "string.hpp" #include "stream.hpp" #include "monad.hpp" +#include "../scheme/driver.hpp" #include "eval.hpp" namespace tuscany { namespace python { -const string evalOutputPrompt("; "); -const string evalInputPrompt("=> "); - -const bool promptForInput(const string& str, ostream& out) { - out << endl << endl << str; - return true; -} - -const bool announceOutput(const string str, ostream& out) { - out << endl << str; - return true; -} - -const bool userPrint(const value val, ostream& out) { - writeValue(val, out); - return true; -} - const value evalDriverLoop(PyObject* script, istream& in, ostream& out) { - promptForInput(evalInputPrompt, out); - value input = readValue(in); + scheme::promptForInput(scheme::evalInputPrompt, out); + value input = scheme::readValue(in); if (isNil(input)) return input; - const value output = evalScript(input, script); - announceOutput(evalOutputPrompt, out); - userPrint(output, out); + const failable<value> output = evalScript(input, script); + scheme::announceOutput(scheme::evalOutputPrompt, out); + scheme::userPrint(content(output), out); return evalDriverLoop(script, in, out); } -const bool evalDriverRun(istream& in, ostream& out) { - setupDisplay(out); - evalDriverLoop(builtin(pythonRuntime), in, out); - return true; -} - const bool evalDriverRun(const char* path, istream& in, ostream& out) { - setupDisplay(out); + scheme::setupDisplay(out); ifstream is(path); failable<PyObject*> script = readScript(path, is); if (!hasContent(script)) @@ -82,4 +59,4 @@ const bool evalDriverRun(const char* path, istream& in, ostream& out) { } } -#endif /* tuscany_scheme_pydriver_hpp */ +#endif /* tuscany_python_driver_hpp */ diff --git a/sca-cpp/trunk/modules/python/eval.hpp b/sca-cpp/trunk/modules/python/eval.hpp index cb6cdea3e2..1b9392aed7 100644 --- a/sca-cpp/trunk/modules/python/eval.hpp +++ b/sca-cpp/trunk/modules/python/eval.hpp @@ -19,8 +19,8 @@ /* $Rev$ $Date$ */ -#ifndef tuscany_python_pyeval_hpp -#define tuscany_python_pyeval_hpp +#ifndef tuscany_python_eval_hpp +#define tuscany_python_eval_hpp /** * Python script evaluation logic. @@ -42,29 +42,14 @@ public: PythonRuntime() { Py_Initialize(); - // Import the builtin module - PyObject* p = PyString_FromString("__builtin__"); - builtin = PyImport_Import(p); - Py_DECREF(p); - setupIO(); } ~PythonRuntime() { - Py_DECREF(builtin); } -private: - friend PyObject* builtin(const PythonRuntime& r); - - PyObject* builtin; - } pythonRuntime; -PyObject* builtin(const PythonRuntime& r) { - return r.builtin; -} - /** * Declare conversion functions. */ @@ -111,7 +96,7 @@ PyTypeObject pyLambda_type = { /** * Create a new python object representing a lambda expression. */ -PyObject *mkPyLambda(const lambda<value(const list<value>&)> l) { +PyObject *mkPyLambda(const lambda<value(const list<value>&)>& l) { pyLambda* pyl = NULL; pyl = PyObject_NEW(pyLambda, &pyLambda_type); if (pyl != NULL) @@ -160,7 +145,7 @@ PyObject* valueToPyObject(const value& v) { * Convert a python tuple to a list of values. */ -const list<value> pyTupleToValuesHelper(PyObject* o, int i, int size) { +const list<value> pyTupleToValuesHelper(PyObject* o, const int i, const int size) { if (i == size) return list<value>(); return cons(pyObjectToValue(PyTuple_GetItem(o, i)), pyTupleToValuesHelper(o, i + 1, size)); @@ -220,22 +205,6 @@ const value pyObjectToValue(PyObject *o) { } /** - * Read a python script from an input stream. - */ -const failable<PyObject*> readScript(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); - if (mod == NULL) - return mkfailure<PyObject*>(string("Couldn't import module: ") + path + " : " + lastError()); - return mod; -} - -/** * Evaluate an expression against a script provided as a python object. */ const failable<value> evalScript(const value& expr, PyObject* script) { @@ -266,6 +235,22 @@ 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 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); + if (mod == NULL) + return mkfailure<PyObject*>(string("Couldn't import module: ") + path + " : " + lastError()); + return mod; +} + +/** * Evaluate an expression against a script provided as an input stream. */ const failable<value> evalScript(const value& expr, istream& is) { @@ -275,13 +260,6 @@ const failable<value> evalScript(const value& expr, istream& is) { return evalScript(expr, content(script)); } -/** - * Evaluate an expression against the python builtin module, no script is provided. - */ -const failable<value> evalExpr(const value& expr) { - return evalScript(expr, builtin(pythonRuntime)); -} - } } -#endif /* tuscany_python_pyeval_hpp */ +#endif /* tuscany_python_eval_hpp */ diff --git a/sca-cpp/trunk/modules/python/io.hpp b/sca-cpp/trunk/modules/python/io.hpp index 4292c32c34..10119a4f77 100644 --- a/sca-cpp/trunk/modules/python/io.hpp +++ b/sca-cpp/trunk/modules/python/io.hpp @@ -19,8 +19,8 @@ /* $Rev$ $Date$ */ -#ifndef tuscany_python_pyio_hpp -#define tuscany_python_pyio_hpp +#ifndef tuscany_python_io_hpp +#define tuscany_python_io_hpp /** * Hooks used to capture python stdout and stderr. @@ -33,46 +33,6 @@ namespace tuscany { namespace python { -#ifdef _REENTRANT -__thread -#endif -ostream* displayOutStream = NULL; - -#ifdef _REENTRANT -__thread -#endif -ostream* logOutStream = NULL; - -/** - * Setup the display stream. - */ -const bool setupDisplay(ostream& out) { - scheme::setupDisplay(out); - displayOutStream = &out; - return true; -} - -ostream& displayStream() { - if (displayOutStream == NULL) - return cout; - return *displayOutStream; -} - -/** - * Setup the log stream. - */ -const bool setupLog(ostream& out) { - scheme::setupLog(out); - logOutStream = &out; - return true; -} - -ostream& logStream() { - if (logOutStream == NULL) - return cerr; - return *logOutStream; -} - /** * Hook method used to redirect python output to a stream. */ @@ -81,7 +41,7 @@ PyObject* display(unused PyObject* self, PyObject* args) { if (!PyArg_ParseTuple(args, "s", &s)) return NULL; - displayStream() << s; + scheme::displayStream() << s; Py_INCREF(Py_None); return Py_None; @@ -99,7 +59,7 @@ PyObject* log(unused PyObject* self, PyObject* args) { if (!PyArg_ParseTuple(args, "s", &s)) return NULL; - logStream() << s; + scheme::logStream() << s; Py_INCREF(Py_None); return Py_None; @@ -141,12 +101,12 @@ bool setupIO() { * Return the last python error. */ const string lastError() { - ostream* pos = logOutStream; + ostream* pos = scheme::logOutStream; ostringstream eos; - logOutStream = &eos; + scheme::logOutStream = &eos; if (PyErr_Occurred()) PyErr_Print(); - logOutStream = pos; + scheme::logOutStream = pos; return str(eos); } @@ -164,4 +124,4 @@ const bool writeValue(const value& val, ostream& out) { } } -#endif /* tuscany_python_pyio_hpp */ +#endif /* tuscany_python_io_hpp */ diff --git a/sca-cpp/trunk/modules/python/python-shell.cpp b/sca-cpp/trunk/modules/python/python-shell.cpp index 862ca6062f..89b47b8d44 100644 --- a/sca-cpp/trunk/modules/python/python-shell.cpp +++ b/sca-cpp/trunk/modules/python/python-shell.cpp @@ -31,9 +31,9 @@ int main(const int argc, char** argv) { tuscany::gc_scoped_pool pool; - if (argc == 1) { - tuscany::python::evalDriverRun(tuscany::cin, tuscany::cout); - return 0; + if (argc != 2) { + tuscany::cerr << "Usage: python-shell <script.py>" << tuscany::endl; + return 1; } tuscany::python::evalDriverRun(argv[1], tuscany::cin, tuscany::cout); return 0; diff --git a/sca-cpp/trunk/modules/python/python-test.cpp b/sca-cpp/trunk/modules/python/python-test.cpp index fa3a221906..48687fa0ec 100644 --- a/sca-cpp/trunk/modules/python/python-test.cpp +++ b/sca-cpp/trunk/modules/python/python-test.cpp @@ -31,49 +31,22 @@ namespace tuscany { namespace python { -const value evalBuiltin(const string& py) { - istringstream is(py); - ostringstream os; - evalDriverRun(is, os); - return str(os); -} - -const string testPythonPrint( - "(print \"testPrint ok\")"); - -bool testEval() { - gc_scoped_pool pool; - assert(contains(evalBuiltin(testPythonPrint), "testPrint ok")); - return true; -} - const string testPythonAdd = "def add(x, y):\n" " return x + y\n"; bool testEvalExpr() { gc_scoped_pool pool; - { - const value exp = mklist<value>("abs", -5); - const failable<value> r = evalExpr(exp); - assert(hasContent(r)); - assert(content(r) == value(5)); - } - { - istringstream is(testPythonAdd); - failable<PyObject*> script = readScript("script", is); - assert(hasContent(script)); - const value exp = mklist<value>("add", 2, 3); - const failable<value> r = evalScript(exp, content(script)); - assert(hasContent(r)); - assert(content(r) == value(5)); - } - return true; -} -bool testEvalRun() { - gc_scoped_pool pool; - evalDriverRun(cin, cout); + istringstream is(testPythonAdd); + failable<PyObject*> script = readScript("script", is); + assert(hasContent(script)); + + const value exp = mklist<value>("add", 2, 3); + const failable<value> r = evalScript(exp, content(script)); + assert(hasContent(r)); + assert(content(r) == value(5)); + return true; } @@ -126,7 +99,6 @@ bool testEvalLambda() { int main() { tuscany::cout << "Testing..." << tuscany::endl; - tuscany::python::testEval(); tuscany::python::testEvalExpr(); tuscany::python::testEvalLambda(); |