From 0caac30c11f0cf14557a11469b1efe8b97cfb061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Tue, 23 Oct 2012 10:16:32 +0100 Subject: [PATCH] support lists --- packages/pyswip/pl2py_examples/multiply.py | 17 +++++++++ packages/pyswip/python.c | 42 +++++++++++++++++++--- 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 packages/pyswip/pl2py_examples/multiply.py diff --git a/packages/pyswip/pl2py_examples/multiply.py b/packages/pyswip/pl2py_examples/multiply.py new file mode 100644 index 000000000..1111beb96 --- /dev/null +++ b/packages/pyswip/pl2py_examples/multiply.py @@ -0,0 +1,17 @@ +def multiply(a,b): + print "Will compute", a, "times", b + c = 0 + for i in range(0, a): + c = c + b + return c + +def square(a,b): + return [a*a,b*b] + +def lsquare(a): + print a + b = [] + for i in a: + b.append(i*i) + return b + diff --git a/packages/pyswip/python.c b/packages/pyswip/python.c index d6d9747d7..7bf2d2664 100644 --- a/packages/pyswip/python.c +++ b/packages/pyswip/python.c @@ -12,6 +12,7 @@ static foreign_t init_python(void) { Py_Initialize(); + return TRUE; } @@ -37,9 +38,10 @@ python_import(term_t mname, term_t mod) if (pName == NULL) { return FALSE; } -PyRun_SimpleString("import sys"); -PyRun_SimpleString("sys.path.append(\"/Users/vsc/Yap/bins/osx/packages/pyswip\")"); -//PyRun_SimpleString("import multiply"); + + PyRun_SimpleString("import sys"); + PyRun_SimpleString("sys.path.append(\"/Users/vsc/Yap/bins/osx/packages/pyswip\")"); + //PyRun_SimpleString("import multiply"); pModule = PyImport_Import(pName); PyErr_Print(); Py_DECREF(pName); @@ -117,6 +119,28 @@ term_to_python(term_t t) return PyFloat_FromDouble( fl ); } case PL_TERM: + if (PL_is_list(t)) { + size_t len, i; + term_t tail = PL_new_term_ref(), arg; + PyObject *out; + + PL_skip_list(t, tail, &len); + if (!PL_get_nil(tail)) + return NULL; + arg = tail; + out = PyList_New(len); + if (!out) + return NULL; + + for (i=0; i< len; i++) { + if (!PL_get_list(t, arg, t)) { + return NULL; + } + if (PyList_SetItem(out, i, term_to_python(arg)) < 0) + return NULL; + } + return out; + } return NULL; } return NULL; @@ -140,6 +164,16 @@ output_python_term(PyObject *pVal, term_t t) } else if (PyByteArray_Check(pVal)) { atom_t tmp_atom = PL_new_atom(PyByteArray_AsString(pVal)); return PL_unify_atom(t, tmp_atom); + } else if (PyList_Check(pVal)) { + term_t to = PL_new_term_ref(); + Py_ssize_t i, sz = PyList_GET_SIZE(pVal); + + for (i = 0; i < sz; i++) { + if (!PL_unify_list(t, to, t) || + !output_python_term(PyList_GetItem(pVal, i), to)) + return FALSE; + } + return PL_unify_nil(t); } else { return FALSE; } @@ -168,7 +202,7 @@ python_apply(term_t tin, term_t targs, term_t tf) return FALSE; pArg = term_to_python(targ); if (pArg == NULL) - return NULL; + return FALSE; /* pArg reference stolen here: */ PyTuple_SetItem(pArgs, i, pArg); }