This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/python/python.c

1069 lines
25 KiB
C
Raw Normal View History

2012-10-08 23:58:22 +01:00
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
2012-10-17 10:56:44 +01:00
#ifdef HAVE_STAT
#undef HAVE_STAT
#endif
2012-10-08 23:58:22 +01:00
#include <Python.h>
#include <assert.h>
2012-10-26 00:24:07 +01:00
static atom_t ATOM_true,
ATOM_false,
ATOM_t;
2012-10-17 10:56:44 +01:00
2012-10-25 00:33:02 +01:00
static functor_t FUNCTOR_dollar1,
2012-11-25 23:37:28 +00:00
FUNCTOR_abs1,
FUNCTOR_all1,
FUNCTOR_any1,
FUNCTOR_bin1,
2012-11-05 13:49:15 +00:00
FUNCTOR_dir1,
FUNCTOR_iter1,
FUNCTOR_len1,
2012-11-25 23:37:28 +00:00
FUNCTOR_curly1,
FUNCTOR_ord1,
2012-10-25 00:33:02 +01:00
FUNCTOR_pointer1,
2012-11-05 13:49:15 +00:00
FUNCTOR_complex2,
FUNCTOR_plus2,
2012-10-25 00:33:02 +01:00
FUNCTOR_sub2,
FUNCTOR_mul2,
FUNCTOR_div2,
2012-11-05 13:49:15 +00:00
FUNCTOR_hat2,
FUNCTOR_colon2,
2012-11-25 23:37:28 +00:00
FUNCTOR_comma2,
2012-11-05 13:49:15 +00:00
FUNCTOR_equal2;
2012-10-23 10:16:32 +01:00
2012-10-25 00:33:02 +01:00
static PyObject *py_Main;
2012-11-25 23:37:28 +00:00
static PyObject *term_to_python(term_t t);
2012-10-17 10:56:44 +01:00
2012-10-28 18:22:09 +00:00
static inline int
2012-11-25 23:37:28 +00:00
proper_ascii_string(char *s)
2012-10-28 18:22:09 +00:00
{
2012-11-25 23:37:28 +00:00
unsigned char c;
2012-10-28 18:22:09 +00:00
while ((c = *s++)) {
if (c > 127)
return FALSE;
}
return TRUE;
}
2012-11-05 13:49:15 +00:00
static Py_ssize_t
get_p_int(PyObject *o, Py_ssize_t def) {
if (o == NULL)
return def;
if (PyLong_Check(o)) {
return PyLong_AsLong(o);
} else if (PyInt_Check(o)) {
return PyInt_AsLong(o);
}
return def;
}
2012-11-25 23:37:28 +00:00
static int
copy_to_dictionary(PyObject *dict, term_t targ, term_t taux)
{
PyObject *lhs, *rhs;
term_t tleft = taux, tright = tleft;
if (!PL_get_arg(1, targ, tleft)) {
return FALSE;
}
lhs = term_to_python(tleft);
if (!PL_get_arg(2, targ, tright)) {
return FALSE;
}
rhs = term_to_python(tright);
if (PyDict_SetItem(dict, lhs, rhs) < 0 ) {
return FALSE;
}
//PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
Py_DECREF(lhs);
Py_DECREF(rhs);
return TRUE;
}
static PyObject *
bip_abs(term_t t)
{
PyObject *pVal;
if (! PL_get_arg(1, t, t) )
return NULL;
pVal = term_to_python(t);
return PyNumber_Absolute(pVal);
}
static PyObject *
bip_all(term_t t)
{
PyObject *it, *item, *v;
PyObject *(*iternext)(PyObject *);
int cmp;
if (! PL_get_arg(1, t, t) )
return NULL;
v = term_to_python(t);
it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
iternext = *Py_TYPE(it)->tp_iternext;
// PyObject_Print(v, stderr, 0);
for (;;) {
item = iternext(it);
if (item == NULL)
break;
cmp = PyObject_IsTrue(item);
Py_DECREF(item);
if (cmp < 0) {
Py_DECREF(it);
return NULL;
}
if (cmp == 0) {
Py_DECREF(it);
return Py_False;
}
}
Py_DECREF(it);
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
else
return NULL;
}
return Py_True;
}
static PyObject *
bip_any(term_t t)
{
PyObject *it, *item, *v;
PyObject *(*iternext)(PyObject *);
int cmp;
if (! PL_get_arg(1, t, t) )
return NULL;
v = term_to_python(t);
it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
iternext = *Py_TYPE(it)->tp_iternext;
for (;;) {
item = iternext(it);
if (item == NULL)
break;
cmp = PyObject_IsTrue(item);
Py_DECREF(item);
if (cmp < 0) {
Py_DECREF(it);
return NULL;
}
if (cmp == 1) {
Py_DECREF(it);
Py_RETURN_TRUE;
}
}
Py_DECREF(it);
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
else
return NULL;
}
Py_RETURN_FALSE;
}
static PyObject *
bip_bin(term_t t)
{
PyObject *v;
if (! PL_get_arg(1, t, t) )
return NULL;
v = term_to_python(t);
return PyNumber_ToBase(v, 2);
}
static PyObject *
bip_ord(term_t t)
{
PyObject *pVal;
Py_ssize_t size;
if (! PL_get_arg(1, t, t) )
return NULL;
pVal = term_to_python(t);
if (PyUnicode_Check(pVal)) {
size = PyUnicode_GET_SIZE(pVal);
if (size == 1) {
long ord = (long)*PyUnicode_AS_UNICODE(pVal);
return PyInt_FromLong(ord);
}
return NULL;
} else if (PyByteArray_Check(pVal)) {
char *s = PyByteArray_AsString(pVal);
if (s[1])
return NULL;
return PyInt_FromLong(s[0]);
} else if (PyString_Check(pVal)) {
char *s = PyString_AsString(pVal);
if (s[1])
return NULL;
return PyInt_FromLong(s[0]);
} else
return NULL;
}
2012-10-17 10:56:44 +01:00
static PyObject *
term_to_python(term_t t)
{
2012-10-25 00:33:02 +01:00
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I am\n");
2012-10-17 10:56:44 +01:00
switch (PL_term_type(t)) {
case PL_VARIABLE:
return NULL;
case PL_ATOM:
{
char *s;
2012-11-25 23:37:28 +00:00
atom_t at;
2012-10-17 10:56:44 +01:00
2012-10-28 18:22:09 +00:00
if (!PL_get_atom_chars(t, &s)) {
wchar_t *w;
size_t len;
if (!PL_get_atom(t, &at))
return NULL;
if (!(w = PL_atom_wchars(at, &len)))
return NULL;
2012-11-25 23:37:28 +00:00
return PyUnicode_FromWideChar(w, len );
}
if (!PL_get_atom(t, &at)) {
return NULL;
2012-10-28 18:22:09 +00:00
}
2012-11-25 23:37:28 +00:00
if (at == ATOM_true) return Py_True;
if (at == ATOM_false) return Py_False;
2012-10-28 18:22:09 +00:00
if (proper_ascii_string(s))
return PyString_FromStringAndSize(s, strlen(s) );
2012-11-25 23:37:28 +00:00
else {
PyObject *pobj = PyUnicode_DecodeLatin1(s, strlen(s), NULL);
return pobj;
}
2012-10-17 10:56:44 +01:00
}
case PL_INTEGER:
{
int64_t j;
if (!PL_get_int64_ex(t, &j))
return NULL;
return PyInt_FromLong(j);
}
case PL_STRING:
{
char *s;
size_t len;
if (!PL_get_string_chars(t, &s, &len))
return NULL;
2012-10-28 18:22:09 +00:00
return PyByteArray_FromStringAndSize(s, len);
2012-10-17 10:56:44 +01:00
}
case PL_FLOAT:
{
double fl;
if (!PL_get_float(t, &fl))
return NULL;
return PyFloat_FromDouble( fl );
}
case PL_TERM:
2012-10-23 10:16:32 +01:00
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;
2012-10-25 00:33:02 +01:00
} else {
functor_t fun;
if (!PL_get_functor(t, &fun))
return NULL;
if (fun == FUNCTOR_dollar1) {
char *s;
term_t targ = PL_new_term_ref();
if (! PL_get_arg(1, t, targ) )
return NULL;
if (!PL_get_atom_chars(targ, &s))
return NULL;
/* return __main__,s */
return PyObject_GetAttrString(py_Main, s);
} else if (fun == FUNCTOR_pointer1) {
void *ptr;
term_t targ = PL_new_term_ref();
if (! PL_get_arg(1, t, targ) )
return NULL;
if (!PL_get_pointer(targ, &ptr))
return NULL;
/* return __main__,s */
return (PyObject *)ptr;
2012-11-25 23:37:28 +00:00
} else if (fun == FUNCTOR_abs1) {
return bip_abs(t);
} else if (fun == FUNCTOR_all1) {
return bip_all(t);
} else if (fun == FUNCTOR_any1) {
return bip_any(t);
} else if (fun == FUNCTOR_bin1) {
return bip_bin(t);
} else if (fun == FUNCTOR_ord1) {
return bip_ord(t);
2012-11-05 13:49:15 +00:00
} else if (fun == FUNCTOR_len1) {
term_t targ = PL_new_term_ref();
PyObject *ptr;
if (! PL_get_arg(1, t, targ) )
return NULL;
ptr = term_to_python(targ);
return PyLong_FromLong(PyObject_Length(ptr));
} else if (fun == FUNCTOR_dir1) {
term_t targ = PL_new_term_ref();
PyObject *ptr;
if (! PL_get_arg(1, t, targ) )
return NULL;
ptr = term_to_python(targ);
return PyObject_Dir(ptr);
} else if (fun == FUNCTOR_iter1) {
term_t targ = PL_new_term_ref();
PyObject *ptr;
if (! PL_get_arg(1, t, targ) )
return NULL;
ptr = term_to_python(targ);
return PyObject_GetIter(ptr);
} else if (fun == FUNCTOR_complex2) {
2012-10-25 00:33:02 +01:00
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
2012-11-05 13:49:15 +00:00
double d1, d2;
2012-10-25 00:33:02 +01:00
if (! PL_get_arg(1, t, targ) )
return NULL;
lhs = term_to_python(targ);
if (!PyNumber_Check(lhs))
return NULL;
2012-11-05 13:49:15 +00:00
if (PyFloat_Check(lhs)) {
d1 = PyFloat_AsDouble(lhs);
} else if (PyLong_Check(lhs)) {
d1 = PyLong_AsLong(lhs);
} else if (PyInt_Check(lhs)) {
d1 = PyInt_AsLong(lhs);
} else {
return NULL;
}
if (! PL_get_arg(2, t, targ) )
return NULL;
rhs = term_to_python(targ);
if (!PyNumber_Check(rhs))
return NULL;
if (PyFloat_Check(rhs)) {
d2 = PyFloat_AsDouble(rhs);
} else if (PyLong_Check(rhs)) {
d2 = PyLong_AsLong(rhs);
} else if (PyInt_Check(rhs)) {
d2 = PyInt_AsLong(rhs);
} else {
return NULL;
}
return PyComplex_FromDoubles(d1, d2);
2012-11-25 23:37:28 +00:00
} else if (fun == FUNCTOR_curly1) {
term_t targ = PL_new_term_ref(), taux = PL_new_term_ref();
PyObject *dict;
if (! PL_get_arg(1, t, t) )
return NULL;
if (! (dict = PyDict_New() ) )
return NULL;
while (PL_is_functor(t, FUNCTOR_comma2)) {
if (! PL_get_arg(1, t, targ) )
return NULL;
if (PL_is_functor(targ, FUNCTOR_colon2)) {
if ( !copy_to_dictionary(dict, targ, taux) )
return NULL;
if (! PL_get_arg(2, t, t) )
return NULL;
} else {
return NULL;
}
}
if (PL_is_functor(t, FUNCTOR_colon2)) {
if ( !copy_to_dictionary(dict, t, taux) )
return NULL;
} else {
return NULL;
}
return dict;
2012-11-05 13:49:15 +00:00
} else if (fun == FUNCTOR_plus2) {
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
if (! PL_get_arg(1, t, targ) )
return NULL;
lhs = term_to_python(targ);
2012-10-25 00:33:02 +01:00
if (! PL_get_arg(2, t, targ) )
return NULL;
rhs = term_to_python(targ);
2012-11-05 13:49:15 +00:00
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
return PySequence_Concat(lhs, rhs);
}
if (!PyNumber_Check(lhs))
return NULL;
2012-10-25 00:33:02 +01:00
if (!PyNumber_Check(rhs))
return NULL;
return PyNumber_Add(lhs, rhs);
} else if (fun == FUNCTOR_sub2) {
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
if (! PL_get_arg(1, t, targ) )
return NULL;
lhs = term_to_python(targ);
if (!PyNumber_Check(lhs))
return NULL;
if (! PL_get_arg(2, t, targ) )
return NULL;
rhs = term_to_python(targ);
if (!PyNumber_Check(rhs))
return NULL;
return PyNumber_Subtract(lhs, rhs);
} else if (fun == FUNCTOR_mul2) {
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
if (! PL_get_arg(1, t, targ) )
return NULL;
lhs = term_to_python(targ);
if (! PL_get_arg(2, t, targ) )
return NULL;
rhs = term_to_python(targ);
2012-11-05 13:49:15 +00:00
if (PySequence_Check(lhs) && (PyInt_Check(rhs) || PyLong_Check(rhs)) ){
return PySequence_Repeat(lhs, get_p_int(rhs, 0));
}
if (!PyNumber_Check(lhs)+!PyNumber_Check(rhs))
2012-10-25 00:33:02 +01:00
return NULL;
return PyNumber_Multiply(lhs, rhs);
} else if (fun == FUNCTOR_div2) {
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
if (! PL_get_arg(1, t, targ) )
return NULL;
lhs = term_to_python(targ);
if (!PyNumber_Check(lhs))
return NULL;
if (! PL_get_arg(2, t, targ) )
return NULL;
rhs = term_to_python(targ);
if (!PyNumber_Check(rhs))
return NULL;
return PyNumber_Divide(lhs, rhs);
} else if (fun == FUNCTOR_hat2) {
2012-11-02 22:37:27 +00:00
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
2012-10-25 00:33:02 +01:00
PyObject *lhs, *rhs;
2012-11-02 22:37:27 +00:00
if (! PL_get_arg(1, t, targ))
2012-10-25 00:33:02 +01:00
return NULL;
lhs = term_to_python(targ);
2012-11-02 22:37:27 +00:00
if (! PL_get_arg(2, t, targ) || !PL_is_list(targ) || !PL_get_list(targ, trhs, targ) )
2012-10-25 00:33:02 +01:00
return NULL;
2012-11-05 13:49:15 +00:00
if (PL_is_functor(trhs, FUNCTOR_colon2) ) {
Py_ssize_t left, right;
if (!PL_get_arg(1, trhs, targ))
return NULL;
left = get_p_int(term_to_python(targ), 0);
if (!PL_get_arg(2, trhs, targ))
return NULL;
right = get_p_int(term_to_python(targ), PyObject_Size(lhs) );
if (!PySequence_Check(lhs))
return NULL;
return PySequence_GetSlice(lhs, left, right);
} else {
rhs = term_to_python(trhs);
return PyObject_GetItem(lhs, rhs);
}
2012-11-02 22:37:27 +00:00
} else {
atom_t name;
int len;
if (! PL_get_name_arity( t, &name, &len) ) {
return NULL;
}
if (name == ATOM_t) {
term_t targ = PL_new_term_ref();
PyObject *out;
int i;
out = PyTuple_New(len);
if (!out)
return NULL;
for (i=0; i< len; i++) {
if (!PL_get_arg(i+1, t, targ)) {
return NULL;
}
if (PyTuple_SetItem(out, i, term_to_python(targ)) < 0)
return NULL;
}
return out;
}
2012-10-25 00:33:02 +01:00
}
2012-10-23 10:16:32 +01:00
}
2012-10-17 10:56:44 +01:00
return NULL;
}
return NULL;
}
2012-11-02 22:37:27 +00:00
static int
assign_python(PyObject *root, term_t t, PyObject *e)
{
switch (PL_term_type(t)) {
case PL_VARIABLE:
return -1;
case PL_ATOM:
{
char *s;
if (!PL_get_atom_chars(t, &s)) {
wchar_t *w;
atom_t at;
size_t len;
PyObject *wo;
if (!PL_get_atom(t, &at))
return -1;
if (!(w = PL_atom_wchars(at, &len)))
return -1;
wo = PyUnicode_FromWideChar(w, wcslen(w) );
return PyObject_SetAttr(root, wo, e);
}
if (proper_ascii_string(s)) {
return PyObject_SetAttrString(root, s, e);
} else {
PyObject *wo= PyUnicode_DecodeLatin1(s, strlen(s), NULL);
return PyObject_SetAttr(root, wo, e);
}
}
case PL_INTEGER:
case PL_STRING:
case PL_FLOAT:
return -1;
case PL_TERM:
if (PL_is_list(t)) {
return -1;
} else {
functor_t fun;
if (!PL_get_functor(t, &fun))
return -1;
if (fun == FUNCTOR_dollar1) {
char *s;
if (! PL_get_arg(1, t, t) )
return -1;
if (!PL_get_atom_chars(t, &s)) {
wchar_t *w;
atom_t at;
size_t len;
PyObject *attr;
if (!PL_get_atom(t, &at)) {
return -1;
}
if (!(w = PL_atom_wchars(at, &len)))
return -1;
attr = PyUnicode_FromWideChar(w, wcslen(w) );
if (!attr)
return -1;
return PyObject_SetAttr(py_Main, attr, e);
}
if (proper_ascii_string(s)) {
return PyObject_SetAttrString(py_Main, s, e);
} else {
PyObject *attr= PyUnicode_DecodeLatin1(s, strlen(s), NULL);
if (!attr)
return -1;
return PyObject_SetAttr(py_Main, attr, e);
}
} else if (fun == FUNCTOR_pointer1) {
return -1;
} else if (fun == FUNCTOR_hat2) {
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
PyObject *lhs, *rhs;
if (! PL_get_arg(1, t, targ) )
return -1;
lhs = term_to_python(targ);
if (! PL_get_arg(2, t, targ) || !PL_is_list(targ) || !PL_get_list(targ, trhs, targ ) )
return -1;
2012-11-05 13:49:15 +00:00
if (PL_is_functor(trhs, FUNCTOR_colon2) ) {
Py_ssize_t left, right;
if (!PL_get_arg(1, trhs, targ))
return -1;
left = get_p_int(term_to_python(targ), 0);
if (!PL_get_arg(2, trhs, targ))
return -1;
right = get_p_int(term_to_python(targ), PyObject_Size(lhs) );
if (!PySequence_Check(lhs))
return -1;
return PySequence_SetSlice(lhs, left, right, e);
} else {
rhs = term_to_python(trhs);
return PyObject_SetItem(lhs, rhs, e);
}
2012-11-02 22:37:27 +00:00
}
}
return -1;
}
return -1;
}
2012-10-17 10:56:44 +01:00
static foreign_t
2012-10-25 00:33:02 +01:00
python_to_term(PyObject *pVal, term_t t)
2012-10-17 10:56:44 +01:00
{
2012-11-25 23:37:28 +00:00
if (PyBool_Check(pVal)) {
2012-10-25 00:33:02 +01:00
if (PyObject_IsTrue(pVal)) {
2012-10-17 10:56:44 +01:00
return PL_unify_atom(t, ATOM_true);
} else {
return PL_unify_atom(t, ATOM_false);
}
2012-11-25 23:37:28 +00:00
} else if (PyLong_Check(pVal)) {
return PL_unify_int64(t, PyLong_AsLong(pVal));
} else if (PyInt_Check(pVal)) {
return PL_unify_int64(t, PyInt_AsLong(pVal));
2012-10-17 10:56:44 +01:00
} else if (PyFloat_Check(pVal)) {
return PL_unify_float(t, PyFloat_AsDouble(pVal));
2012-11-05 13:49:15 +00:00
} else if (PyComplex_Check(pVal)) {
term_t to = PL_new_term_ref(), t1= PL_new_term_ref(), t2 = PL_new_term_ref();
if (!PL_put_float(t1, PyComplex_RealAsDouble(pVal) ) ||
!PL_put_float(t2, PyComplex_ImagAsDouble(pVal) ) ||
!PL_cons_functor(to, FUNCTOR_complex2, t1, t2) )
return FALSE;
return PL_unify(t, to);
2012-10-26 00:24:07 +01:00
} else if (PyUnicode_Check(pVal)) {
Py_ssize_t sz = PyUnicode_GetSize(pVal)+1;
wchar_t *ptr;
2012-11-25 23:37:28 +00:00
atom_t tmp_atom;
2012-10-26 00:24:07 +01:00
ptr = malloc(sizeof(wchar_t)*sz);
2012-11-25 23:37:28 +00:00
sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, ptr, sz-1);
tmp_atom = PL_new_atom_wchars(sz,ptr);
2012-10-26 00:24:07 +01:00
free(ptr);
return PL_unify_atom(t, tmp_atom);
2012-10-17 10:56:44 +01:00
} else if (PyByteArray_Check(pVal)) {
atom_t tmp_atom = PL_new_atom(PyByteArray_AsString(pVal));
return PL_unify_atom(t, tmp_atom);
2012-10-25 00:33:02 +01:00
} else if (PyString_Check(pVal)) {
atom_t tmp_atom = PL_new_atom(PyString_AsString(pVal));
return PL_unify_atom(t, tmp_atom);
2012-10-26 00:24:07 +01:00
} else if (PyTuple_Check(pVal)) {
2012-11-05 13:49:15 +00:00
Py_ssize_t i, sz = PyTuple_Size(pVal);
functor_t f = PL_new_functor(ATOM_t, sz);
2012-10-26 00:24:07 +01:00
if (!PL_unify_functor(t, f))
return FALSE;
for (i = 0; i < sz; i++) {
term_t to = PL_new_term_ref();
if (!PL_unify_arg(i+1, t, to))
return FALSE;
if ( !python_to_term(PyTuple_GetItem(pVal, i), to) )
return FALSE;
}
return TRUE;
2012-10-23 10:16:32 +01:00
} 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) ||
2012-10-25 00:33:02 +01:00
!python_to_term(PyList_GetItem(pVal, i), to))
2012-10-23 10:16:32 +01:00
return FALSE;
}
return PL_unify_nil(t);
2012-11-25 23:37:28 +00:00
} else if (PyDict_Check(pVal)) {
Py_ssize_t pos = 0;
term_t to = PL_new_term_ref(), ti = to;
int left = PyDict_Size(pVal);
PyObject *key, *value;
while (PyDict_Next(pVal, &pos, &key, &value)) {
term_t tkey = PL_new_term_ref(), tval = PL_new_term_ref(), tint, tnew = PL_new_term_ref();
/* do something interesting with the values... */
if (!python_to_term(key, tkey)) {
return FALSE;
}
if (!python_to_term(value, tval)) {
return FALSE;
}
/* reuse */
tint = tkey;
if (!PL_cons_functor(tint, FUNCTOR_colon2, tkey, tval)) {
return FALSE;
}
if (--left) {
if (!PL_cons_functor(tint, FUNCTOR_comma2, tint, tnew))
return FALSE;
}
if (!PL_unify(ti, tint))
return FALSE;
ti = tnew;
}
PL_cons_functor(to, FUNCTOR_curly1, to);
return PL_unify(t, to);
2012-10-17 10:56:44 +01:00
} else {
2012-10-25 00:33:02 +01:00
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
PL_put_pointer(t1, (void *)pVal);
PL_cons_functor(to, FUNCTOR_pointer1, t1);
2012-10-26 00:24:07 +01:00
Py_INCREF(pVal);
2012-10-25 00:33:02 +01:00
return PL_unify(t, to);
}
}
static int
python_import(term_t mname, term_t mod)
{
char *s;
size_t len;
PyObject *pName, *pModule;
if ( !PL_get_nchars(mname, &len, &s, CVT_ALL|CVT_EXCEPTION) ) {
return FALSE;
}
pName = PyString_FromString(s);
if (pName == NULL) {
2012-10-17 10:56:44 +01:00
return FALSE;
}
2012-10-25 00:33:02 +01:00
pModule = PyImport_Import(pName);
// PyErr_Print();
Py_DECREF(pName);
if (pModule == NULL) {
return FALSE;
}
return python_to_term(pModule, mod);
}
static foreign_t
python_f(term_t tmod, term_t fname, term_t tf)
{
char *s;
size_t len;
PyObject *pF, *pModule;
/* if an atom, fetch again */
if ( PL_is_atom(tmod) ) {
PyObject *pName;
if ( !PL_get_nchars(fname, &len, &s, CVT_ALL|CVT_EXCEPTION) ) {
return FALSE;
}
pName = PyString_FromString(s);
if (pName == NULL) {
return FALSE;
}
pModule = PyImport_Import(pName);
} else if (!(pModule = term_to_python(tmod)))
return FALSE;
if ( !PL_get_nchars(fname, &len, &s, CVT_ALL|CVT_EXCEPTION) ) {
return FALSE;
}
pF = PyObject_GetAttrString(pModule, s);
2012-11-25 23:37:28 +00:00
Py_DECREF(pModule);
2012-10-25 00:33:02 +01:00
if (pF == NULL || ! PyCallable_Check(pF)) {
return FALSE;
}
return python_to_term(pF, tf);
}
static foreign_t
python_o(term_t tmod, term_t fname, term_t tf)
{
char *s;
size_t len;
PyObject *pO, *pModule;
pModule = term_to_python(tmod);
if ( !PL_get_nchars(fname, &len, &s, CVT_ALL|CVT_EXCEPTION) ) {
return FALSE;
}
pO = PyObject_GetAttrString(pModule, s);
if (pO == NULL) {
return FALSE;
}
return python_to_term(pO, tf);
}
static foreign_t
python_len(term_t tobj, term_t tf)
{
Py_ssize_t len;
PyObject *o;
o = term_to_python(tobj);
if (o == NULL)
return FALSE;
len = PyObject_Length(o);
return PL_unify_int64(tf, len);
}
static foreign_t
python_dir(term_t tobj, term_t tf)
{
PyObject *dir;
PyObject *o;
o = term_to_python(tobj);
if (o == NULL)
return FALSE;
dir = PyObject_Dir(o);
return python_to_term(dir, tf);
}
static foreign_t
python_is(term_t tobj, term_t tf)
{
PyObject *o;
o = term_to_python(tobj);
if (!o)
return FALSE;
return python_to_term(o, tf);
2012-10-17 10:56:44 +01:00
}
static foreign_t
python_apply(term_t tin, term_t targs, term_t tf)
{
2012-10-26 00:24:07 +01:00
PyObject *pF, *pValue;
PyObject *pArgs;
2012-11-05 13:49:15 +00:00
int i, arity, j;
2012-10-17 10:56:44 +01:00
atom_t aname;
foreign_t out;
term_t targ = PL_new_term_ref();
2012-10-25 00:33:02 +01:00
pF = term_to_python(tin);
if ( pF == NULL ) {
2012-10-17 10:56:44 +01:00
return FALSE;
}
if (! PL_get_name_arity( targs, &aname, &arity) ) {
return FALSE;
}
2012-11-05 13:49:15 +00:00
if (PyFunction_Check(pF)) {
int tuple_inited = FALSE;
PyObject *pOpt = NULL;
for (j = arity ; j > 0; j--) {
term_t tsubarg = PL_new_term_ref();
PyObject *pArg;
int posx;
Py_ssize_t pos;
if (! PL_get_arg(j, targs, targ) )
return FALSE;
if (! PL_is_functor(targ, FUNCTOR_equal2) )
break;
if (! PL_get_arg(1, targ, tsubarg) || !PL_get_integer(tsubarg, &posx) )
break;
pos = posx;
if (!tuple_inited) {
if ((pOpt = PyFunction_GetDefaults(pF)) == NULL)
break;
tuple_inited = TRUE;
}
if (! PL_get_arg(2, targ, tsubarg) )
break;
pArg = term_to_python(tsubarg);
if (pArg == NULL)
return FALSE;
/* pArg reference stolen here: */
PyTuple_SetItem(pOpt, pos, pArg);
}
if (tuple_inited) {
if (PyFunction_SetDefaults(pF, pOpt) < 0) {
return FALSE;
}
arity = j;
}
}
2012-10-17 10:56:44 +01:00
pArgs = PyTuple_New(arity);
for (i = 0 ; i < arity; i++) {
PyObject *pArg;
if (! PL_get_arg(i+1, targs, targ) )
return FALSE;
pArg = term_to_python(targ);
if (pArg == NULL)
2012-10-23 10:16:32 +01:00
return FALSE;
2012-10-17 10:56:44 +01:00
/* pArg reference stolen here: */
PyTuple_SetItem(pArgs, i, pArg);
}
2012-10-26 00:24:07 +01:00
if (PyCallable_Check(pF)) {
pValue = PyObject_CallObject(pF, pArgs);
} else {
return FALSE;
}
PyErr_Print();
2012-10-17 10:56:44 +01:00
Py_DECREF(pArgs);
2012-10-25 00:33:02 +01:00
if (pValue == NULL)
2012-10-26 00:24:07 +01:00
return FALSE;
2012-11-05 13:49:15 +00:00
out = 0;
2012-10-25 00:33:02 +01:00
out = python_to_term(pValue, tf);
2012-10-17 10:56:44 +01:00
Py_DECREF(pValue);
return out;
}
2012-10-25 00:33:02 +01:00
static foreign_t
python_assign(term_t name, term_t exp)
{
PyObject *e = term_to_python(exp);
if (e == NULL)
return FALSE;
2012-11-02 22:37:27 +00:00
return assign_python(py_Main, name, e) >= 0;
2012-10-25 00:33:02 +01:00
}
static foreign_t
python_access(term_t obj, term_t f, term_t out)
{
PyObject *o = term_to_python(obj), *pValue, *pArgs, *pF;
atom_t name;
char *s;
int i, arity;
term_t targ = PL_new_term_ref();
if (o == NULL)
return FALSE;
if ( PL_is_atom(f) ) {
if (!PL_get_atom_chars(f, &s))
return FALSE;
if ((pValue = PyObject_GetAttrString(o, s)) == NULL)
return FALSE;
2012-10-26 00:24:07 +01:00
if ( PyCallable_Check(pValue) )
pValue = PyObject_CallObject(pValue, NULL);
PyErr_Print();
2012-10-25 00:33:02 +01:00
return python_to_term(pValue, out);
}
if (! PL_get_name_arity( f, &name, &arity) ) {
return FALSE;
}
s = PL_atom_chars(name);
2012-10-26 00:24:07 +01:00
if ((pF = PyObject_GetAttrString(o, s)) == NULL) {
PyErr_Print();
2012-10-25 00:33:02 +01:00
return FALSE;
2012-10-26 00:24:07 +01:00
}
2012-10-25 00:33:02 +01:00
pArgs = PyTuple_New(arity);
for (i = 0 ; i < arity; i++) {
PyObject *pArg;
if (! PL_get_arg(i+1, f, targ) )
return FALSE;
pArg = term_to_python(targ);
if (pArg == NULL)
return FALSE;
/* pArg reference stolen here: */
PyTuple_SetItem(pArgs, i, pArg);
}
pValue = PyObject_CallObject(pF, pArgs);
if (pValue == NULL) {
Py_DECREF(pArgs);
Py_DECREF(pF);
return FALSE;
}
Py_DECREF(pArgs);
Py_DECREF(pF);
2012-10-26 00:24:07 +01:00
return python_to_term(pValue, out);
2012-10-25 00:33:02 +01:00
}
2012-10-08 23:58:22 +01:00
static foreign_t
python_run_command(term_t cmd)
{
char *s;
size_t len;
if ( PL_get_nchars(cmd, &len, &s, CVT_ALL|CVT_EXCEPTION) ) {
PyRun_SimpleString(s);
return TRUE;
}
return FALSE;
}
2012-10-25 00:33:02 +01:00
static foreign_t
init_python(void)
{
Py_Initialize();
py_Main = PyImport_AddModule("__main__");
return TRUE;
}
static foreign_t
end_python(void)
{
Py_Finalize();
return TRUE;
}
2012-10-08 23:58:22 +01:00
install_t install_python(void);
install_t
install_python(void)
{ // FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
// FUNCTOR_boolop1 = PL_new_functor(PL_new_atom("@"), 1);
2012-10-17 10:56:44 +01:00
ATOM_true = PL_new_atom("true");
ATOM_false = PL_new_atom("false");
2012-10-26 00:24:07 +01:00
ATOM_t = PL_new_atom("t");
2012-11-25 23:37:28 +00:00
FUNCTOR_abs1 = PL_new_functor(PL_new_atom("abs"), 1);
FUNCTOR_all1 = PL_new_functor(PL_new_atom("all"), 1);
FUNCTOR_any1 = PL_new_functor(PL_new_atom("any"), 1);
FUNCTOR_bin1 = PL_new_functor(PL_new_atom("bin"), 1);
FUNCTOR_ord1 = PL_new_functor(PL_new_atom("ord"), 1);
FUNCTOR_curly1 = PL_new_functor(PL_new_atom("{}"), 1);
2012-10-25 00:33:02 +01:00
FUNCTOR_dollar1 = PL_new_functor(PL_new_atom("$"), 1);
FUNCTOR_pointer1 = PL_new_functor(PL_new_atom("__obj__"), 1);
2012-11-05 13:49:15 +00:00
FUNCTOR_dir1 = PL_new_functor(PL_new_atom("dir"), 1);
FUNCTOR_iter1 = PL_new_functor(PL_new_atom("iter"), 1);
FUNCTOR_len1 = PL_new_functor(PL_new_atom("len"), 1);
FUNCTOR_complex2 = PL_new_functor(PL_new_atom("complex"), 2);
FUNCTOR_plus2 = PL_new_functor(PL_new_atom("+"), 2);
2012-10-25 00:33:02 +01:00
FUNCTOR_sub2 = PL_new_functor(PL_new_atom("-"), 2);
FUNCTOR_mul2 = PL_new_functor(PL_new_atom("*"), 2);
FUNCTOR_div2 = PL_new_functor(PL_new_atom("/"), 2);
FUNCTOR_hat2 = PL_new_functor(PL_new_atom("^"), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_colon2 = PL_new_functor(PL_new_atom(":"), 2);
2012-11-25 23:37:28 +00:00
FUNCTOR_comma2 = PL_new_functor(PL_new_atom(","), 2);
2012-11-05 13:49:15 +00:00
FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
2012-10-08 23:58:22 +01:00
PL_register_foreign("init_python", 0, init_python, 0);
PL_register_foreign("end_python", 0, end_python, 0);
2012-10-17 10:56:44 +01:00
PL_register_foreign("python_import", 2, python_import, 0);
PL_register_foreign("python_f", 3, python_f, 0);
2012-10-25 00:33:02 +01:00
PL_register_foreign("python_o", 3, python_o, 0);
PL_register_foreign("python_len", 2, python_len, 0);
PL_register_foreign("python_is", 2, python_is, 0);
PL_register_foreign("python_dir", 2, python_dir, 0);
2012-10-17 10:56:44 +01:00
PL_register_foreign("python_apply", 3, python_apply, 0);
2012-10-25 00:33:02 +01:00
PL_register_foreign("python_access", 3, python_access, 0);
PL_register_foreign("python_assign", 2, python_assign, 0);
2012-10-08 23:58:22 +01:00
PL_register_foreign("python_run_command", 1, python_run_command, 0);
}
2012-10-17 10:56:44 +01:00