diff options
Diffstat (limited to '')
-rw-r--r-- | sca-cpp/trunk/modules/python/eval.hpp | 204 |
1 files changed, 102 insertions, 102 deletions
diff --git a/sca-cpp/trunk/modules/python/eval.hpp b/sca-cpp/trunk/modules/python/eval.hpp index 5c0be7d261..3f9c6c20a2 100644 --- a/sca-cpp/trunk/modules/python/eval.hpp +++ b/sca-cpp/trunk/modules/python/eval.hpp @@ -53,8 +53,8 @@ class PythonRuntime; /** * Maintain a garbage collected reference to a Python object. */ -const bool pyIncRef(PyObject* o); -const bool pyDecRef(PyObject* o, PythonRuntime* py); +const bool pyIncRef(PyObject* const o); +const bool pyDecRef(PyObject* const o, PythonRuntime* const py); /** * Write to debug log from Python. @@ -70,7 +70,7 @@ const value pyDebug(const list<value>& args) { class PythonRuntime { public: - PythonRuntime() { + PythonRuntime() : owner(true) { debug("python::pythonruntime"); // Save current process id @@ -115,7 +115,7 @@ public: PySys_SetArgv(0, const_cast<char**>(&arg0)); // Install debug log function - PyObject* mkPyLambda(const lambda<value(const list<value>&)>& l, PythonRuntime* py); + PyObject* mkPyLambda(const lvvlambda& l, PythonRuntime* py); PyObject* pyd= mkPyLambda(pyDebug, this); PyObject* sys = PyImport_ImportModule("sys"); PyObject_SetAttrString(sys, "debug", pyd); @@ -127,10 +127,21 @@ public: #endif } +#ifdef WANT_THREADS + PythonRuntime(const PythonRuntime& py) : owner(false), mutex(py.mutex), pidmutex(py.pidmutex), pid(py.pid) { + } +#else + PythonRuntime(const PythonRuntime& py) : owner(false), pid(py.pid) { + } +#endif + + PythonRuntime& operator=(const PythonRuntime& py) = delete; + ~PythonRuntime() { } private: + const bool owner; #ifdef WANT_THREADS pthread_mutex_t mutex; pthread_mutex_t pidmutex; @@ -145,9 +156,9 @@ private: /** * Return the last python error. */ -const string lastErrorTrace(PyObject *trace) { +const string lastErrorTrace(PyObject* const trace) { if (trace == NULL) - return ""; + return emptyString; PyTracebackObject* tb = (PyTracebackObject*)trace; const int limit = 16; int depth = 0; @@ -167,16 +178,16 @@ const string lastErrorTrace(PyObject *trace) { return str(os); } -const string lastError(PythonRuntime* py) { +const string lastError(PythonRuntime* const py) { if(PyErr_Occurred()) { PyObject* type = NULL; PyObject* val = NULL; PyObject* trace = NULL; PyErr_Fetch(&type, &val, &trace); if (type != NULL && val != NULL) { - PyObject* stype = PyObject_Repr(type); - PyObject* sval = PyObject_Repr(val); - string msg = string() + PyString_AsString(stype) + " : " + PyString_AsString(sval) + lastErrorTrace(trace); + PyObject* const stype = PyObject_Repr(type); + PyObject* const sval = PyObject_Repr(val); + const string msg = string() + PyString_AsString(stype) + " : " + PyString_AsString(sval) + lastErrorTrace(trace); pyDecRef(stype, py); pyDecRef(sval, py); PyErr_Restore(type, val, trace); @@ -187,7 +198,7 @@ const string lastError(PythonRuntime* py) { PyErr_Clear(); return "Unknown Python error"; } - return ""; + return emptyString; } /** @@ -195,7 +206,7 @@ const string lastError(PythonRuntime* py) { */ class PythonRuntimeLock { public: - PythonRuntimeLock(PythonRuntime* py) : py(py) { + PythonRuntimeLock(PythonRuntime* const py) : py(py) { #ifdef WANT_THREADS pthread_mutex_lock(&py->mutex); #endif @@ -208,7 +219,7 @@ public: } private: - PythonRuntime* py; + PythonRuntime* const py; }; /** @@ -216,7 +227,7 @@ private: */ class PythonThreadIn { public: - PythonThreadIn(PythonRuntime* py) : py(py) { + PythonThreadIn(PythonRuntime* const py) : py(py) { // Reinitialize Python thread support after a fork const unsigned long pid = processId(); @@ -261,7 +272,7 @@ public: } private: - PythonRuntime* py; + PythonRuntime* const py; #ifdef WANT_THREADS PyGILState_STATE gstate; #endif @@ -272,7 +283,7 @@ private: */ class PythonThreadOut { public: - PythonThreadOut(PythonRuntime* py) : py(py) { + PythonThreadOut(PythonRuntime* const py) : py(py) { #ifdef WANT_THREADS //debug("python::gil::save"); tstate = PyEval_SaveThread(); @@ -289,7 +300,7 @@ public: } private: - PythonRuntime* py; + PythonRuntime* const py; #ifdef WANT_THREADS PyThreadState* tstate; #endif @@ -300,7 +311,7 @@ private: */ class PyGCRef { public: - PyGCRef(PyObject* o, PythonRuntime* py) : o(o), py(py) { + PyGCRef(PyObject* const o, PythonRuntime* const py) : o(o), py(py) { } ~PyGCRef() { @@ -311,21 +322,21 @@ public: } private: - PyObject* o; - PythonRuntime* py; + PyObject* const o; + PythonRuntime* const py; }; /** * Maintain a garbage collected reference to a Python object. */ -const bool pyIncRef(PyObject* o) { +const bool pyIncRef(PyObject* const o) { if (o == NULL) return true; Py_INCREF(o); return true; } -const bool pyDecRef(unused PyObject* o, unused PythonRuntime* py) { +const bool pyDecRef(unused PyObject* const o, unused PythonRuntime* const py) { if (o == NULL) return true; //new (gc_new<PyGCRef>()) PyGCRef(o, py); @@ -336,40 +347,40 @@ const bool pyDecRef(unused PyObject* o, unused PythonRuntime* py) { /** * Declare conversion functions. */ -PyObject* valueToPyObject(const value& v, PythonRuntime* py); -const value pyObjectToValue(PyObject *o, PythonRuntime* py); -PyObject* valuesToPyTuple(const list<value>& v, PythonRuntime* py); -const list<value> pyTupleToValues(PyObject* o, PythonRuntime* py); +PyObject* const valueToPyObject(const value& v, PythonRuntime* const py); +const value pyObjectToValue(PyObject* const o, PythonRuntime* const py); +PyObject* const valuesToPyTuple(const list<value>& v, PythonRuntime* const py); +const list<value> pyTupleToValues(PyObject* const o, PythonRuntime* const py); /** * Callable python type used to represent a lambda expression. */ typedef struct { PyObject_HEAD - lambda<value(const list<value>&)>* func; + lvvlambda* func; PythonRuntime* py; } pyLambda; -PyObject *mkPyLambda(const lambda<value(const list<value>&)>& l, PythonRuntime* py); +PyObject* const mkPyLambda(const lvvlambda& l, PythonRuntime* const py); -void pyLambda_dealloc(PyObject* self) { +void pyLambda_dealloc(PyObject* const self) { //debug(self, "python::pylambda_dealloc"); PyObject_Del(self); } -const string pyRepr(PyObject* o, PythonRuntime* py) { - PyObject* r = PyObject_Repr(o); +const string pyRepr(PyObject* const o, PythonRuntime* const py) { + PyObject* const r = PyObject_Repr(o); const string s = PyString_AsString(r); pyDecRef(r, py); return s; } -const value pyLambda_callout(const pyLambda* pyl, const list<value>& args, PythonRuntime* py) { +const value pyLambda_callout(const pyLambda* const pyl, const list<value>& args, PythonRuntime* const py) { PythonThreadOut pyout(py); return (*(pyl->func))(args); } -PyObject* pyLambda_call(PyObject* self, PyObject* args, unused PyObject* kwds) { +PyObject* const pyLambda_call(PyObject* const self, PyObject* const args, unused PyObject* const kwds) { debug("python::call"); const pyLambda* pyl = (const pyLambda*)self; const value result = pyLambda_callout(pyl, pyTupleToValues(args, pyl->py), pyl->py); @@ -378,22 +389,7 @@ PyObject* pyLambda_call(PyObject* self, PyObject* args, unused PyObject* kwds) { return pyr; } -struct pyProxy { - const value name; - const lambda<value(const list<value>&)> func; - - pyProxy(const value& name, const lambda<value(const list<value>&)>& func) : name(name), func(func) { - } - - const value operator()(const list<value>& args) const { - debug(name, "python::proxy::name"); - const value result = func(cons<value>(name, args)); - debug(result, "python::proxy::result"); - return result; - } -}; - -PyObject* pyLambda_getattr(PyObject *self, PyObject *attrname) { +PyObject* const pyLambda_getattr(PyObject* const self, PyObject* const attrname) { const string name = PyString_AsString(attrname); if (substr(name, 0, 1) == "_") return PyObject_GenericGetAttr(self, attrname); @@ -403,9 +399,18 @@ PyObject* pyLambda_getattr(PyObject *self, PyObject *attrname) { return self; } - const pyLambda* pyl = (pyLambda*)self; + const pyLambda* const pyl = (const pyLambda* const)self; debug(name, "python::getattr::name"); - PyObject* pyr = mkPyLambda(pyProxy(name, *(pyl->func)), pyl->py); + + const lvvlambda func = *(pyl->func); + const lvvlambda pyProxy = [name, func](const list<value>& args) -> const value { + debug(name, "python::proxy::name"); + const value result = func(cons<value>(name, args)); + debug(result, "python::proxy::result"); + return result; + }; + + PyObject* const pyr = mkPyLambda(pyProxy, pyl->py); return pyr; } @@ -464,10 +469,10 @@ PyObject* pyLambda_getattr(PyObject *self, PyObject *attrname) { /** * Create a new python object representing a lambda expression. */ -PyObject *mkPyLambda(const lambda<value(const list<value>&)>& l, PythonRuntime* py) { - pyLambda* pyl = PyObject_New(pyLambda, &pyLambda_type); +PyObject* const mkPyLambda(const lvvlambda& l, PythonRuntime* const py) { + pyLambda* const pyl = PyObject_New(pyLambda, &pyLambda_type); if (pyl != NULL) { - pyl->func = new (gc_new<lambda<value(const list<value>&)> >()) lambda<value(const list<value>&)>(l); + pyl->func = new (gc_new<lvvlambda >()) lvvlambda(l); pyl->py = py; } //debug(pyl, "python::mkpylambda"); @@ -477,18 +482,18 @@ PyObject *mkPyLambda(const lambda<value(const list<value>&)>& l, PythonRuntime* /** * Convert a list of values to a python list. */ -PyObject* valuesToPyListHelper(PyObject* l, const list<value>& v, PythonRuntime* py) { +PyObject* const valuesToPyListHelper(PyObject* const l, const list<value>& v, PythonRuntime* const py) { if (isNil(v)) return l; - PyObject* pyv = valueToPyObject(car(v), py); + PyObject* const pyv = valueToPyObject(car(v), py); PyList_Append(l, pyv); pyDecRef(pyv, py); return valuesToPyListHelper(l, cdr(v), py); } -PyObject* valuesToPyTuple(const list<value>& v, PythonRuntime* py) { - PyObject* pyl = valuesToPyListHelper(PyList_New(0), v, py); - PyObject* pyt = PyList_AsTuple(pyl); +PyObject* const valuesToPyTuple(const list<value>& v, PythonRuntime* const py) { + PyObject* const pyl = valuesToPyListHelper(PyList_New(0), v, py); + PyObject* const pyt = PyList_AsTuple(pyl); pyDecRef(pyl, py); return pyt; } @@ -496,14 +501,14 @@ PyObject* valuesToPyTuple(const list<value>& v, PythonRuntime* py) { /** * Convert a value to a python object. */ -PyObject* valueToPyObject(const value& v, PythonRuntime* py) { +PyObject* const valueToPyObject(const value& v, PythonRuntime* const py) { switch (type(v)) { case value::List: return valuesToPyTuple(v, py); case value::Lambda: return mkPyLambda(v, py); case value::Symbol: - return PyString_FromString(c_str(string("'") + v)); + return PyString_FromString(c_str(string("'") + (string)v)); case value::String: { const string s = (string)v; return PyString_FromStringAndSize(c_str(s), length(s)); @@ -511,12 +516,12 @@ PyObject* valueToPyObject(const value& v, PythonRuntime* py) { case value::Number: return PyFloat_FromDouble((double)v); case value::Bool: { - PyObject* b = (bool)v? Py_True : Py_False; + PyObject* const b = (bool)v? Py_True : Py_False; pyIncRef(b); return b; } default: { - PyObject* n = Py_None; + PyObject* const n = Py_None; pyIncRef(n); return n; } @@ -526,39 +531,29 @@ PyObject* valueToPyObject(const value& v, PythonRuntime* py) { /** * Convert a python tuple to a list of values. */ -const list<value> pyTupleToValuesHelper(PyObject* o, const size_t i, const size_t size, PythonRuntime* py) { +const list<value> pyTupleToValuesHelper(PyObject* const o, const size_t i, const size_t size, PythonRuntime* const py) { if (i == size) - return list<value>(); + return nilListValue; return cons(pyObjectToValue(PyTuple_GetItem(o, i), py), pyTupleToValuesHelper(o, i + 1, size, py)); } -const list<value> pyTupleToValues(PyObject* o, PythonRuntime* py) { +const list<value> pyTupleToValues(PyObject* const o, PythonRuntime* const py) { return pyTupleToValuesHelper(o, 0, PyTuple_Size(o), py); } /** * Lambda function used to represent a python callable object. */ -struct pyCallable { - PyObject* func; - PythonRuntime* py; - bool owner; - - pyCallable(PyObject* func, PythonRuntime* py) : func(func), py(py), owner(true) { +class pyCallable { +public: + pyCallable(PyObject* const func, PythonRuntime* const py) : func(func), py(py), owner(true) { pyIncRef(func); } pyCallable(const pyCallable& c) : func(c.func), py(c.py), owner(false) { } - const pyCallable& operator=(const pyCallable& c) { - if(this == &c) - return *this; - func = c.func; - py = c.py; - owner = false; - return *this; - } + pyCallable& operator=(const pyCallable& c) = delete; ~pyCallable() { if (!owner) @@ -569,16 +564,16 @@ struct pyCallable { const value operator()(const list<value>& args) const { PythonThreadIn pyin(py); if (debug_islogging()) { - PyObject* rfunc = PyObject_Repr(func); + PyObject* const rfunc = PyObject_Repr(func); char* s = NULL; Py_ssize_t l = 0; PyString_AsStringAndSize(rfunc, &s, &l); debug(string(s, l), "python::operator()::func"); pyDecRef(rfunc, py); } - PyObject* pyargs = valuesToPyTuple(args, py); + PyObject* const pyargs = valuesToPyTuple(args, py); if (debug_islogging()) { - PyObject* rargs = PyObject_Repr(pyargs); + PyObject* const rargs = PyObject_Repr(pyargs); char* s = NULL; Py_ssize_t l = 0; PyString_AsStringAndSize(rargs, &s, &l); @@ -586,21 +581,26 @@ struct pyCallable { pyDecRef(rargs, py); } - PyObject* result = PyObject_CallObject(func, pyargs); + PyObject* const result = PyObject_CallObject(func, pyargs); pyDecRef(pyargs, py); const value v = pyObjectToValue(result, py); pyDecRef(result, py); return v; } + +private: + PyObject* const func; + PythonRuntime* const py; + const bool owner; }; /** * Convert a python object to a value. */ -const value pyObjectToValue(PyObject *o, PythonRuntime* py) { +const value pyObjectToValue(PyObject* const o, PythonRuntime* const py) { if (o == NULL) - return value(); + return nilValue; if (PyString_Check(o)) { char* s = NULL; Py_ssize_t l = 0; @@ -622,8 +622,8 @@ const value pyObjectToValue(PyObject *o, PythonRuntime* py) { if (PyObject_TypeCheck(o, &pyLambda_type)) return *(((pyLambda*)o)->func); if (PyCallable_Check(o)) - return lambda<value(const list<value>&)>(pyCallable(o, py)); - return value(); + return lvvlambda(pyCallable(o, py)); + return nilValue; } /** @@ -636,37 +636,37 @@ const string moduleName(const string& path) { /** * Evaluate an expression against a script provided as a python object. */ -const failable<value> evalScript(const value& expr, PyObject* script, PythonRuntime& py) { +const failable<value> evalScript(const value& expr, PyObject* const script, PythonRuntime& py) { PythonThreadIn pyin(&py); // Get the requested function - PyObject* func = PyObject_GetAttrString(script, c_str(car<value>(expr))); + PyObject* const func = PyObject_GetAttrString(script, c_str(car<value>(expr))); if (func == NULL) { // The start, stop, and restart functions are optional const value fn = car<value>(expr); if (fn == "start" || fn == "stop") { PyErr_Clear(); - return value(lambda<value(const list<value>&)>()); + return value(lvvlambda()); } - return mkfailure<value>(string("Couldn't find function: ") + car<value>(expr) + " : " + lastError(&py)); + return mkfailure<value>(string("Couldn't find function: ") + (string)car<value>(expr) + " : " + lastError(&py)); } if (!PyCallable_Check(func)) { pyDecRef(func, &py); - return mkfailure<value>(string("Couldn't find callable function: ") + car<value>(expr)); + return mkfailure<value>(string("Couldn't find callable function: ") + (string)car<value>(expr)); } // Convert args to python objects - PyObject* args = valuesToPyTuple(cdr<value>(expr), &py); + PyObject* const args = valuesToPyTuple(cdr<value>(expr), &py); // Call the function - PyObject* result = PyObject_CallObject(func, args); + PyObject* const result = PyObject_CallObject(func, args); if (result == NULL) { const string msg = lastError(&py); pyDecRef(func, &py); pyDecRef(args, &py); - return mkfailure<value>(string("Function call failed: ") + car<value>(expr) + " : " + msg); + return mkfailure<value>(string("Function call failed: ") + (string)car<value>(expr) + " : " + msg); } pyDecRef(func, &py); pyDecRef(args, &py); @@ -684,8 +684,8 @@ const failable<PyObject*> readScript(const string& name, const string& path, ist PythonThreadIn pyin(&py); // Lookup already loaded module - PyObject *mods = PyImport_GetModuleDict(); - PyObject *emod = PyDict_GetItemString(mods, const_cast<char*>(c_str(name))); + PyObject* const mods = PyImport_GetModuleDict(); + PyObject* const emod = PyDict_GetItemString(mods, const_cast<char*>(c_str(name))); if (emod != NULL) return emod; @@ -695,10 +695,10 @@ const failable<PyObject*> readScript(const string& name, const string& path, ist 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); + PyObject* const 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(&py)); - PyObject* mod = PyImport_ExecCodeModuleEx(const_cast<char*>(c_str(name)), code, const_cast<char*>(c_str(path))); + PyObject* const mod = PyImport_ExecCodeModuleEx(const_cast<char*>(c_str(name)), code, const_cast<char*>(c_str(path))); if (mod == NULL) { const string msg = lastError(&py); pyDecRef(code, &py); @@ -708,7 +708,7 @@ const failable<PyObject*> readScript(const string& name, const string& path, ist pyDecRef(mod, &py); // Lookup the loaded module - PyObject *lmod = PyDict_GetItemString(mods, const_cast<char*>(c_str(name))); + PyObject* const lmod = PyDict_GetItemString(mods, const_cast<char*>(c_str(name))); if (lmod != NULL) return lmod; @@ -718,7 +718,7 @@ const failable<PyObject*> readScript(const string& name, const string& path, ist /** * Release a python script. */ -const failable<bool> releaseScript(unused PyObject* script, PythonRuntime& py) { +const failable<bool> releaseScript(unused PyObject* const script, PythonRuntime& py) { PythonThreadIn pyin(&py); // No need to decref the script here, as it's referenced only once from sys.modules return true; |