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
This commit is contained in:
parent
c2add2e090
commit
7d44c7ee8f
5 changed files with 51 additions and 164 deletions
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
@ -219,22 +204,6 @@ const value pyObjectToValue(PyObject *o) {
|
|||
return value();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
|
@ -265,6 +234,22 @@ const failable<value> evalScript(const value& expr, PyObject* script) {
|
|||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue