You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2010/01/11 09:29:31 UTC

svn commit: r897787 - in /tuscany/sca-cpp/trunk/modules/python: driver.hpp eval.hpp io.hpp python-shell.cpp python-test.cpp

Author: jsdelfino
Date: Mon Jan 11 08:29:31 2010
New Revision: 897787

URL: http://svn.apache.org/viewvc?rev=897787&view=rev
Log:
Cleaned up python support code, removed unused functions.

Modified:
    tuscany/sca-cpp/trunk/modules/python/driver.hpp
    tuscany/sca-cpp/trunk/modules/python/eval.hpp
    tuscany/sca-cpp/trunk/modules/python/io.hpp
    tuscany/sca-cpp/trunk/modules/python/python-shell.cpp
    tuscany/sca-cpp/trunk/modules/python/python-test.cpp

Modified: tuscany/sca-cpp/trunk/modules/python/driver.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/python/driver.hpp?rev=897787&r1=897786&r2=897787&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/python/driver.hpp (original)
+++ tuscany/sca-cpp/trunk/modules/python/driver.hpp Mon Jan 11 08:29:31 2010
@@ -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 @@
 
 }
 }
-#endif /* tuscany_scheme_pydriver_hpp */
+#endif /* tuscany_python_driver_hpp */

Modified: tuscany/sca-cpp/trunk/modules/python/eval.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/python/eval.hpp?rev=897787&r1=897786&r2=897787&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/python/eval.hpp (original)
+++ tuscany/sca-cpp/trunk/modules/python/eval.hpp Mon Jan 11 08:29:31 2010
@@ -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 @@
     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 @@
 /**
  * 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 @@
  * 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 @@
 }
 
 /**
- * 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 @@
 }
 
 /**
+ * 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 @@
     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 */

Modified: tuscany/sca-cpp/trunk/modules/python/io.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/python/io.hpp?rev=897787&r1=897786&r2=897787&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/python/io.hpp (original)
+++ tuscany/sca-cpp/trunk/modules/python/io.hpp Mon Jan 11 08:29:31 2010
@@ -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 @@
     if (!PyArg_ParseTuple(args, "s", &s))
         return NULL;
 
-    displayStream() << s;
+    scheme::displayStream() << s;
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -99,7 +59,7 @@
     if (!PyArg_ParseTuple(args, "s", &s))
         return NULL;
 
-    logStream() << s;
+    scheme::logStream() << s;
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -141,12 +101,12 @@
  * 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 @@
 
 }
 }
-#endif /* tuscany_python_pyio_hpp */
+#endif /* tuscany_python_io_hpp */

Modified: tuscany/sca-cpp/trunk/modules/python/python-shell.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/python/python-shell.cpp?rev=897787&r1=897786&r2=897787&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/python/python-shell.cpp (original)
+++ tuscany/sca-cpp/trunk/modules/python/python-shell.cpp Mon Jan 11 08:29:31 2010
@@ -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;

Modified: tuscany/sca-cpp/trunk/modules/python/python-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/python/python-test.cpp?rev=897787&r1=897786&r2=897787&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/python/python-test.cpp (original)
+++ tuscany/sca-cpp/trunk/modules/python/python-test.cpp Mon Jan 11 08:29:31 2010
@@ -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 @@
 int main() {
     tuscany::cout << "Testing..." << tuscany::endl;
 
-    tuscany::python::testEval();
     tuscany::python::testEvalExpr();
     tuscany::python::testEvalLambda();