summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/python/eval.hpp
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-10-25 03:18:16 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-10-25 03:18:16 +0000
commita2a2cb76e9582af32b6803be7fa99af074dc04ae (patch)
treed0ef504321e72fe16afd23f385f20386530f5dfc /sca-cpp/trunk/modules/python/eval.hpp
parent0dd33c3859618f3a385583d7344230f0e1eb1004 (diff)
Support python method invocation style on references, ref.func(...) in addition to ref('func', ...). Minor cleanup of the various samples, renamed gettotal to total and getcatalog to items, for consistency with the python sample.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1026939 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk/modules/python/eval.hpp')
-rw-r--r--sca-cpp/trunk/modules/python/eval.hpp44
1 files changed, 43 insertions, 1 deletions
diff --git a/sca-cpp/trunk/modules/python/eval.hpp b/sca-cpp/trunk/modules/python/eval.hpp
index daccb16ec7..9b75945cf2 100644
--- a/sca-cpp/trunk/modules/python/eval.hpp
+++ b/sca-cpp/trunk/modules/python/eval.hpp
@@ -94,15 +94,54 @@ typedef struct {
lambda<value(const list<value>&)> func;
} pyLambda;
+PyObject *mkPyLambda(const lambda<value(const list<value>&)>& l);
+
void pyLambda_dealloc(PyObject* self) {
PyMem_DEL(self);
}
+const string pyRepr(PyObject * o) {
+ return PyString_AsString(PyObject_Repr(o));
+}
+
PyObject* pyLambda_call(PyObject* self, PyObject* args, unused PyObject* kwds) {
+ debug("python::call");
const pyLambda* pyl = (pyLambda*)self;
const value result = pyl->func(pyTupleToValues(args));
+ debug(result, "python::call::result");
Py_DECREF(args);
PyObject *pyr = valueToPyObject(result);
+ Py_INCREF(pyr);
+ 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) {
+ const string name = PyString_AsString(attrname);
+ if (substr(name, 0, 1) == "_")
+ return PyObject_GenericGetAttr(self, attrname);
+
+ if (name == "eval")
+ return self;
+
+ const pyLambda* pyl = (pyLambda*)self;
+ debug(name, "python::getattr::name");
+ PyObject* pyr = mkPyLambda(pyProxy(name, pyl->func));
+ Py_INCREF(pyr);
return pyr;
}
@@ -115,7 +154,9 @@ PyTypeObject pyLambda_type = {
(destructor)pyLambda_dealloc,
0, 0, 0, 0, 0, 0, 0, 0, 0,
(ternaryfunc)pyLambda_call,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0,
+ (binaryfunc)pyLambda_getattr,
+ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0
@@ -179,6 +220,7 @@ const list<value> pyTupleToValuesHelper(PyObject* o, const int i, const int size
}
const list<value> pyTupleToValues(PyObject* o) {
+ debug(pyRepr(o), "python::pyTupleToValues");
return pyTupleToValuesHelper(o, 0, PyTuple_Size(o));
}