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/py2pl.c

354 lines
9.1 KiB
C
Raw Normal View History

2016-07-31 16:09:21 +01:00
2018-07-21 01:56:48 +01:00
#include "Yap.h"
2017-06-12 18:00:47 +01:00
#include "py4yap.h"
2018-07-21 01:56:48 +01:00
2017-07-03 02:17:52 +01:00
#include <frameobject.h>
void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
2017-08-21 12:36:48 +01:00
yap_error_number type, term_t where, ...) {
va_list ap;
char tmpbuf[MAXPATHLEN];
YAP_Term wheret = YAP_GetFromSlot(where);
PyObject *ptype;
2017-07-03 02:17:52 +01:00
2017-08-21 12:36:48 +01:00
if ((ptype = PyErr_Occurred()) == NULL) {
PyErr_Print();
}
2017-07-03 02:17:52 +01:00
2017-08-21 12:36:48 +01:00
va_start(ap, where);
char *format = va_arg(ap, char *);
if (format != NULL) {
2017-07-03 02:17:52 +01:00
#if HAVE_VSNPRINTF
2017-08-21 12:36:48 +01:00
(void)vsnprintf(tmpbuf, MAXPATHLEN - 1, format, ap);
2017-07-03 02:17:52 +01:00
#else
2017-08-21 12:36:48 +01:00
(void)vsprintf(tnpbuf, format, ap);
2017-07-03 02:17:52 +01:00
#endif
2017-08-21 12:36:48 +01:00
// fprintf(stderr, "warning: ");
Yap_ThrowError__(file, function, lineno, type, wheret, tmpbuf);
} else {
Yap_ThrowError__(file, function, lineno, type, wheret);
}
2017-07-03 02:17:52 +01:00
}
2017-06-18 11:14:55 +01:00
2018-07-21 01:56:48 +01:00
static Term repr_term(PyObject *pVal) {
Term t = MkAddressTerm(pVal);
return Yap_MkApplTerm(FunctorObj, 1, &t);
2016-07-31 16:09:21 +01:00
}
foreign_t assign_to_symbol(term_t t, PyObject *e);
foreign_t assign_to_symbol(term_t t, PyObject *e) {
2017-02-20 14:37:26 +00:00
char *s = NULL;
2016-07-31 16:09:21 +01:00
if (!PL_get_atom_chars(t, &s)) {
2017-02-20 14:37:26 +00:00
return false;
2016-07-31 16:09:21 +01:00
}
2017-02-20 14:37:26 +00:00
PyObject *dic;
if (!lookupPySymbol(s, NULL, &dic))
dic = py_Main;
2018-07-09 00:50:00 +01:00
Py_INCREF(e);
2017-02-20 14:37:26 +00:00
return PyObject_SetAttrString(dic, s, e) == 0;
2016-07-31 16:09:21 +01:00
}
2018-07-21 01:56:48 +01:00
static Term python_to_term__(PyObject *pVal) {
2016-07-31 16:09:21 +01:00
if (pVal == Py_None) {
2017-06-18 11:14:55 +01:00
// fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// >>***\n",stderr);
2018-09-18 19:27:10 +01:00
//return YAP_MkVarTerm();
2017-06-18 11:14:55 +01:00
// fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
2018-09-18 19:27:10 +01:00
return MkAtomTerm(Yap_LookupAtom("none"));
2017-06-18 11:14:55 +01:00
// >>***\n",stderr);
2017-06-15 22:40:55 +01:00
} else if (PyBool_Check(pVal)) {
2018-07-21 01:56:48 +01:00
if(PyObject_IsTrue(pVal)) return TermTrue;
return TermFalse;
2016-07-31 16:09:21 +01:00
} else if (PyLong_Check(pVal)) {
2018-07-21 01:56:48 +01:00
return MkIntegerTerm(PyLong_AsLong(pVal));
2016-07-31 16:09:21 +01:00
#if PY_MAJOR_VERSION < 3
} else if (PyInt_Check(pVal)) {
2018-07-21 01:56:48 +01:00
return MkIntegerTerm(PyInt_AsLong(pVal));
2016-07-31 16:09:21 +01:00
#endif
} else if (PyFloat_Check(pVal)) {
2018-07-21 01:56:48 +01:00
return MkFloatTerm(PyFloat_AsDouble(pVal));
2016-07-31 16:09:21 +01:00
} else if (PyComplex_Check(pVal)) {
2018-07-21 01:56:48 +01:00
Term t[2];
t[0] = MkFloatTerm(PyComplex_RealAsDouble(pVal));
t[1] = MkFloatTerm(PyComplex_ImagAsDouble(pVal));
return Yap_MkApplTerm(FunctorI, 2, t);
}
else if (PyUnicode_Check(pVal)) {
2016-07-31 16:09:21 +01:00
#if PY_MAJOR_VERSION < 3
2018-07-21 01:56:48 +01:00
size_t sz = PyUnicode_GetSize(pVal) + 1;
wchar_t *s = malloc(sizeof(wchar_t) * sz);
sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, a, sz - 1);
free(ptr);
2016-07-31 16:09:21 +01:00
#else
2018-07-21 01:56:48 +01:00
const char *s = PyUnicode_AsUTF8(pVal);
2016-07-31 16:09:21 +01:00
#endif
2018-07-21 01:56:48 +01:00
#if 0
if (false && Yap_AtomInUse(s))
2018-07-10 23:21:19 +01:00
rc = rc && PL_unify_atom_chars(t, s);
else
2018-07-21 01:56:48 +01:00
#endif
2018-09-14 11:28:24 +01:00
if (pyStringToString)
2018-07-21 01:56:48 +01:00
return MkStringTerm(s);
2018-09-14 11:28:24 +01:00
else
return MkAtomTerm(Yap_LookupAtom(s));
2018-07-21 01:56:48 +01:00
}
else if (PyByteArray_Check(pVal)) {
return MkStringTerm(PyByteArray_AsString(pVal));
2018-07-09 00:50:00 +01:00
#if PY_MAJOR_VERSION < 3
2018-07-21 01:56:48 +01:00
}
else if (PyString_Check(pVal)) {
return MkStringTerm(PyString_AsString(pVal));
2016-07-31 16:09:21 +01:00
#endif
2018-07-21 01:56:48 +01:00
}
else if (PyTuple_Check(pVal)) {
Py_ssize_t sz = PyTuple_Size(pVal);
const char *s;
s = Py_TYPE(pVal)->tp_name;
if (s == NULL)
s = "t";
if (sz == 0) {
return MkAtomTerm(YAP_LookupAtom(Py_TYPE(pVal)->tp_name));
}
else {
Functor f = Yap_MkFunctor(Yap_LookupAtom(s), sz);
Term t = Yap_MkNewApplTerm(f, sz);
long i;
CELL *ptr = RepAppl(t) + 1;
for (i = 0; i < sz; i++) {
PyObject *p = PyTuple_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
2017-05-08 18:51:29 +01:00
}
2018-07-21 01:56:48 +01:00
*ptr++ = python_to_term__(p);
}
return t;
}
// PL_reset_term_refs(to);
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// ||***\n",stderr);
}
else if (PyList_Check(pVal)) {
Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
if (sz == 0)
2018-07-31 15:18:56 +01:00
return repr_term(pVal);
2018-07-21 01:56:48 +01:00
Term t = TermNil;
2018-08-21 03:01:03 +01:00
for (i = sz; i > 0; ) {
-- i;
PyObject *p = PyList_GetItem(pVal, i);
2018-07-21 01:56:48 +01:00
if (p == NULL) {
PyErr_Clear();
return false;
2017-06-18 11:14:55 +01:00
}
2018-07-21 01:56:48 +01:00
if (!python_to_term__(p))
return false;
t = MkPairTerm(python_to_term__(p), t);
}
return t;
}
else if (PyDict_Check(pVal)) {
2018-08-21 03:01:03 +01:00
Py_ssize_t pos = 0, tot = PyDict_Size(pVal);
2018-07-21 01:56:48 +01:00
PyObject *key, *value;
2018-08-21 03:01:03 +01:00
Term f, *opt = &f, t, to;
while (PyDict_Next(pVal, &pos, &key, &value)) {
Term t0[2];
2018-07-21 01:56:48 +01:00
t0[0] = python_to_term__(key);
t0[1] = python_to_term__(value);
2018-07-21 23:29:01 +01:00
to = Yap_MkApplTerm(FunctorModule, 2, t0);
2018-08-21 03:01:03 +01:00
if (pos < tot) {
2018-07-21 23:29:01 +01:00
t = Yap_MkNewApplTerm(FunctorComma, 2);
2018-07-21 01:56:48 +01:00
CELL *pt = RepAppl(t) + 1;
pt[0] = to;
2018-08-21 03:01:03 +01:00
*opt = t;
opt = pt+1;
2018-07-21 01:56:48 +01:00
} else {
2018-08-21 03:01:03 +01:00
if (pos == 0) {
return repr_term(pVal);
}
*opt = to;
break;
2016-07-31 16:09:21 +01:00
}
}
2018-08-21 03:01:03 +01:00
return Yap_MkApplTerm(FunctorBraces, 1, &f);
2016-07-31 16:09:21 +01:00
}
2018-07-21 01:56:48 +01:00
return repr_term(pVal);
2018-08-21 03:01:03 +01:00
2018-07-21 01:56:48 +01:00
}
2018-07-10 23:21:19 +01:00
2018-07-21 01:56:48 +01:00
foreign_t python_to_term(PyObject *pVal, term_t t) {
2018-07-31 15:18:56 +01:00
Term o = python_to_term__(pVal);
return YAP_Unify(o,YAP_GetFromSlot(t));
2016-07-31 16:09:21 +01:00
}
2016-09-27 18:28:54 +01:00
2018-07-21 01:56:48 +01:00
// extern bool Yap_do_low_level_trace;
2018-07-10 23:21:19 +01:00
2016-09-27 18:28:54 +01:00
X_API YAP_Term pythonToYAP(PyObject *pVal) {
2018-07-21 01:56:48 +01:00
// Yap_do_low_level_trace=1;
/* fputs(" *** ", stderr); */
/* PyObject_Print(pVal, stderr, 0); */
/* fputs("***>>\n", stderr); */
if (pVal == NULL)
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, 0, NULL);
Term t = python_to_term__(pVal);
/* fputs("<< *** ", stderr); */
/* Yap_DebugPlWrite(t); */
/* fputs(" ***\n", stderr); */
2018-07-09 00:50:00 +01:00
// Py_DECREF(pVal);
2018-07-21 01:56:48 +01:00
return t;
2016-09-27 18:28:54 +01:00
}
2017-02-20 14:37:26 +00:00
PyObject *py_Local, *py_Global;
/**
* assigns the Python RHS to a Prolog term LHS, ie LHS = RHS
*
* @param root Python environment
* @param t left hand side, in Prolog, may be
* - a Prolog variable, exports the term to Prolog, A <- RHS
* - Python variable A, A <- RHS
* - Python variable $A, A <- RHS
* - Python string "A", A <- RHS
* - Python array range
* @param e the right-hand side
*
* @return -1 on failure.
*
* Note that this is an auxiliary routine to the Prolog
*python_assign.
*/
bool python_assign(term_t t, PyObject *exp, PyObject *context) {
2018-08-21 03:01:03 +01:00
PyErr_Print();
2017-02-20 14:37:26 +00:00
context = find_obj(context, t, false);
// Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) {
case PL_VARIABLE: {
if (context == NULL) // prevent a.V= N*N[N-1]
return python_to_term(exp, t);
}
2018-08-19 14:39:12 +01:00
case PL_STRING: {
2018-07-31 15:18:56 +01:00
char *s = NULL;
size_t l;
PL_get_string_chars(t, &s,&l);
if (!context)
context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0)
return true;
PyErr_Print();
return false;
}
2017-02-20 14:37:26 +00:00
case PL_ATOM: {
char *s = NULL;
PL_get_atom_chars(t, &s);
if (!context)
context = py_Main;
2017-08-21 12:36:48 +01:00
if (PyObject_SetAttrString(context, s, exp) == 0)
return true;
return false;
2017-02-20 14:37:26 +00:00
}
case PL_INTEGER:
case PL_FLOAT:
// domain or type erro?
return false;
default: {
term_t tail = PL_new_term_ref(), arg = PL_new_term_ref();
size_t len, i;
2017-08-21 12:36:48 +01:00
if (PL_is_pair(t)) {
if (PL_skip_list(t, tail, &len) &&
PL_get_nil(tail)) { // true list
2017-02-20 14:37:26 +00:00
2017-08-21 12:36:48 +01:00
if (PySequence_Check(exp) && PySequence_Length(exp) == len)
for (i = 0; i < len; i++) {
PyObject *p;
if (!PL_get_list(t, arg, t)) {
PL_reset_term_refs(tail);
p = Py_None;
}
if ((p = PySequence_GetItem(exp, i)) == NULL)
p = Py_None;
if (!python_assign(arg, p, context)) {
PL_reset_term_refs(tail);
}
2017-06-18 11:14:55 +01:00
}
2017-08-21 12:36:48 +01:00
}
2017-02-20 14:37:26 +00:00
} else {
functor_t fun;
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
return false;
}
if (fun == FUNCTOR_sqbrackets2) {
2017-08-21 12:36:48 +01:00
// tail is the object o
2017-02-20 14:37:26 +00:00
if (!PL_get_arg(2, t, tail)) {
PL_reset_term_refs(tail);
return false;
}
2017-08-21 12:36:48 +01:00
PyObject *o = term_to_python(tail, true, context, false);
// t now refers to the index
if (!PL_get_arg(1, t, t) || !PL_get_list(t, t, tail) ||
!PL_get_nil(tail)) {
2017-02-20 14:37:26 +00:00
PL_reset_term_refs(tail);
return false;
}
2017-08-21 12:36:48 +01:00
PyObject *i = term_to_python(t, true, NULL, false);
// check numeric
if (PySequence_Check(o) && PyLong_Check(i)) {
2017-02-20 14:37:26 +00:00
long int j;
2017-08-21 12:36:48 +01:00
j = PyLong_AsLong(i);
return PySequence_SetItem(o, j, exp) == 0;
}
2017-02-20 14:37:26 +00:00
#if PY_MAJOR_VERSION < 3
2017-08-21 12:36:48 +01:00
if (PySequence_Check(o) && PyInt_Check(i)) {
long int j;
j = PyInt_AsLong(i);
2018-07-09 00:50:00 +01:00
return PySequence_SetItem(o, i, exp) == 0;
2017-08-21 12:36:48 +01:00
}
2017-02-20 14:37:26 +00:00
#endif
2017-08-21 12:36:48 +01:00
if (PyDict_Check(o)) {
2018-03-12 15:11:59 +00:00
if (PyDict_SetItem(o, i, exp) == 0) {
2017-08-21 12:36:48 +01:00
return true;
2018-03-12 15:11:59 +00:00
}
2017-08-21 12:36:48 +01:00
}
if (PyObject_SetAttr(o, i, exp) == 0) {
return true;
}
} else {
atom_t s;
int n, i;
PL_get_name_arity(t, &s, &n);
PyObject *o = term_to_python(t, true, context, true);
if (PySequence_Check(o) && PySequence_Length(o) == n) {
for (i = 1; i <= n; i++) {
PyObject *p;
if (!PL_get_arg(i, t, arg)) {
PL_reset_term_refs(tail);
o = false;
p = Py_None;
}
if ((p = PySequence_GetItem(exp, i - 1)) == NULL)
p = Py_None;
else if (!python_assign(arg, p, NULL)) {
PL_reset_term_refs(tail);
o = NULL;
}
2017-02-20 14:37:26 +00:00
}
2017-08-21 12:36:48 +01:00
return true;
2017-02-20 14:37:26 +00:00
}
}
}
2017-08-21 12:36:48 +01:00
PL_reset_term_refs(tail);
2017-02-20 14:37:26 +00:00
}
2017-08-21 12:36:48 +01:00
return NULL;
2017-02-20 14:37:26 +00:00
}
}