This commit is contained in:
Vítor Santos Costa
2019-05-15 18:54:04 +01:00
276 changed files with 97746 additions and 4627 deletions

View File

@@ -114,7 +114,8 @@ static foreign_t array_to_python_view(term_t addr, term_t type, term_t szt,
static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tlen) {
size_t sz, i;
PyErr_Clear();
PyErr_Clear();
PyObject *pyl = term_to_python(pyt, true, NULL, true);
term_t targ = PL_new_term_ref();
@@ -135,10 +136,10 @@ static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tle
PyList_SET_ITEM(pyl, i, t);
}
if (PL_is_variable(tlen)) {
PL_unify_int64(tlen, sz);
} else {
python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL);
}
PL_unify_int64(tlen, sz);
} else {
python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL);
}
pyErrorAndReturn( true);
}

View File

@@ -76,15 +76,15 @@ static Term python_to_term__(PyObject *pVal) {
t[1] = MkFloatTerm(PyComplex_ImagAsDouble(pVal));
return Yap_MkApplTerm(FunctorI, 2, t);
}
else if (PyUnicode_Check(pVal)) {
}
else if (PyUnicode_Check(pVal)) {
#if PY_MAJOR_VERSION < 3
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);
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);
#else
const char *s = PyUnicode_AsUTF8(pVal);
const char *s = PyUnicode_AsUTF8(pVal);
#endif
#if 0
if (false && Yap_AtomInUse(s))
@@ -92,72 +92,72 @@ else if (PyUnicode_Check(pVal)) {
else
#endif
if (pyStringToString)
return MkStringTerm(s);
return MkStringTerm(s);
else
return MkAtomTerm(Yap_LookupAtom(s));
}
else if (PyByteArray_Check(pVal)) {
return MkStringTerm(PyByteArray_AsString(pVal));
}
else if (PyByteArray_Check(pVal)) {
return MkStringTerm(PyByteArray_AsString(pVal));
#if PY_MAJOR_VERSION < 3
}
else if (PyString_Check(pVal)) {
return MkStringTerm(PyString_AsString(pVal));
}
else if (PyString_Check(pVal)) {
return MkStringTerm(PyString_AsString(pVal));
#endif
}
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) {
}
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;
}
*ptr++ = python_to_term__(p);
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;
}
*ptr++ = python_to_term__(p);
}
return t;
}
// PL_reset_term_refs(to);
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// ||***\n",stderr);
}
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)
return repr_term(pVal);
Term t = TermNil;
for (i = sz; i > 0; ) {
-- i;
PyObject *p = PyList_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
}
if (!python_to_term__(p))
return false;
else if (PyList_Check(pVal)) {
Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
if (sz == 0)
return repr_term(pVal);
Term t = TermNil;
for (i = sz; i > 0; ) {
-- i;
PyObject *p = PyList_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
return false;
}
if (!python_to_term__(p))
return false;
t = MkPairTerm(python_to_term__(p), t);
t = MkPairTerm(python_to_term__(p), t);
}
return t;
}
return t;
}
else if (PyDict_Check(pVal)) {
Py_ssize_t pos = 0, tot = PyDict_Size(pVal);
PyObject *key, *value;
Term f, *opt = &f, t, to;
if (tot == 0)
return MkAtomTerm( Yap_LookupAtom("{}"));
while (PyDict_Next(pVal, &pos, &key, &value)) {
else if (PyDict_Check(pVal)) {
Py_ssize_t pos = 0, tot = PyDict_Size(pVal);
PyObject *key, *value;
Term f, *opt = &f, t, to;
if (tot == 0)
return MkAtomTerm( Yap_LookupAtom("{}"));
while (PyDict_Next(pVal, &pos, &key, &value)) {
Term t0[2];
t0[0] = python_to_term__(key);
t0[1] = python_to_term__(value);
@@ -169,9 +169,9 @@ else if (PyDict_Check(pVal)) {
*opt = t;
opt = pt+1;
} else {
if (pos == 0) {
return repr_term(pVal);
}
if (pos == 0) {
return repr_term(pVal);
}
*opt = to;
break;
@@ -179,7 +179,7 @@ else if (PyDict_Check(pVal)) {
}
return Yap_MkApplTerm(FunctorBraces, 1, &f);
}
return repr_term(pVal);
return repr_term(pVal);
}
@@ -197,11 +197,13 @@ X_API YAP_Term pythonToYAP(PyObject *pVal) {
/* fputs("***>>\n", stderr); */
if (pVal == NULL)
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, 0, NULL);
yhandle_t h0 = Yap_CurrentHandle();
Term t = python_to_term__(pVal);
/* fputs("<< *** ", stderr); */
/* Yap_DebugPlWrite(t); */
/* fputs(" ***\n", stderr); */
// Py_DECREF(pVal);
Yap_CloseHandles(h0);
return t;
}
@@ -225,13 +227,18 @@ PyObject *py_Local, *py_Global;
*python_assign.
*/
bool python_assign(term_t t, PyObject *exp, PyObject *context) {
PyErr_Print();
bool rc = true;
PyErr_Print();
term_t inp = Yap_CurrentHandle();
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);
PL_reset_term_refs(inp);
rc = python_to_term(exp, t);
break;
}
case PL_STRING: {
@@ -240,24 +247,34 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
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;
if (PyObject_SetAttrString(context, s, exp) == 0) {
PL_reset_term_refs(inp);
;
rc = true;
} else {
PyErr_Print();
PL_reset_term_refs(inp);
rc = false;
}
break;
}
case PL_ATOM: {
char *s = NULL;
PL_get_atom_chars(t, &s);
if (!context)
context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0)
return true;
return false;
if (PyObject_SetAttrString(context, s, exp) == 0){
rc = true;
} else {
rc = false;
}
break;
}
case PL_INTEGER:
case PL_FLOAT:
// domain or type erro?
return false;
rc = false;
break;
default: {
term_t tail = PL_new_term_ref(), arg = PL_new_term_ref();
size_t len, i;
@@ -271,90 +288,96 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
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);
rc = false;
} else {
if ((p = PySequence_GetItem(exp, i)) == NULL)
p = Py_None;
rc = rc && python_assign(arg, p, context);
}
}
} else {
while(PL_is_pair(t)) {
context = find_obj(context, t, false);
}
rc = python_assign(t, exp, context);
}
} else {
functor_t fun;
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
return false;
rc = false;
}
if (fun == FUNCTOR_sqbrackets2) {
// tail is the object o
if (!PL_get_arg(2, t, tail)) {
PL_reset_term_refs(tail);
return false;
}
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)) {
PL_reset_term_refs(tail);
return false;
}
PyObject *i = term_to_python(t, true, NULL, false);
// check numeric
if (PySequence_Check(o) && PyLong_Check(i)) {
long int j;
j = PyLong_AsLong(i);
return PySequence_SetItem(o, j, exp) == 0;
}
rc = false;
} else {
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)) {
rc = false;
} else {
PyObject *i = term_to_python(t, true, NULL, false);
// check numeric
if (PySequence_Check(o) && PyLong_Check(i)) {
long int j;
j = PyLong_AsLong(i);
rc = PySequence_SetItem(o, j, exp) == 0;
}
#if PY_MAJOR_VERSION < 3
if (PySequence_Check(o) && PyInt_Check(i)) {
long int j;
j = PyInt_AsLong(i);
return PySequence_SetItem(o, i, exp) == 0;
}
if (PySequence_Check(o) && PyInt_Check(i)) {
long int j;
j = PyInt_AsLong(i);
szzb rc = PySequence_SetItem(o, i, exp) == 0;
} else
#endif
if (PyDict_Check(o)) {
if (PyDict_SetItem(o, i, exp) == 0) {
return true;
}
}
if (PyObject_SetAttr(o, i, exp) == 0) {
return true;
}
if (PyDict_Check(o)) {
if (PyDict_SetItem(o, i, exp) == 0) {
PL_reset_term_refs(inp);
;
rc = true;
}
}
if (PyObject_SetAttr(o, i, exp) == 0) {
PL_reset_term_refs(inp);
;
rc = true;
}
}
}
} else {
atom_t s;
int n, i;
PL_get_name_arity(t, &s, &n);
PyObject *o = term_to_python(t, true, context, true);
PyErr_Print();
PyErr_Print();
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;
o = NULL;
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;
} else {
PyErr_Print();
}
rc = python_assign(arg, p, NULL);
}
return true;
}
}
}
PL_reset_term_refs(tail);
}
PyErr_Print();
return NULL;
}
PyErr_Print();
PL_reset_term_refs(inp);
return rc;
}

View File

@@ -23,10 +23,14 @@
#undef _XOPEN_SOURCE // python does its own thing
#endif
#undef _POSIX_C_SOURCE
#include <Python.h>
#include <Yap.h>
#include <SWI-Prolog.h>
#ifdef HAVE_STAT
#undef HAVE_STATa
@@ -95,7 +99,7 @@ extern PyObject *py_Sys;
extern X_API bool python_in_python;
extern bool pyStringToString;
extern bool python_release_GIL(term_t gstate);
extern bool python_release_GIL(term_t gstate);
extern term_t python_acquire_GIL(void);
static inline Py_ssize_t get_p_int(PyObject *o, Py_ssize_t def) {
@@ -201,22 +205,21 @@ extern PyObject *YEC(PyObject *c,PyObject *a ,PyObject *d , int line, const char
extern void YEM(const char *ex, int line, const char *file, const char *code);
extern void pyErrorHandler__(int line, const char *file, const char *code);
#define pyErrorHandler() \
{ \
if (PyErr_Occurred()) { \
#define PyStart() PyErr_Clear()
#define pyErrorHandler() if (PyErr_Occurred()) { \
pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \
} \
}
#define pyErrorAndReturn(x) \
{ \
if (PyErr_Occurred()) { \
pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \
} \
return (x); \
}
return (x)
// #define pyErrorAndReturn( x, y ) return x
extern PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt);
extern PyObject *compound_to_pytree(term_t t, PyObject *context, bool cvt);

View File

@@ -112,12 +112,12 @@ find_obj(PyObject *ob, term_t l, bool eval) {
YAP_Term hd, yt;
py_Context = NULL;
yt = YAP_GetFromSlot(l);
// Yap_DebugPlWriteln(yt);
while (YAP_IsPairTerm(yt)) {
if (l == 0)
return Py_None;
while (YAP_IsPairTerm((yt = YAP_GetFromSlot(l)))) {
hd = YAP_HeadOfTerm(yt);
yt = YAP_TailOfTerm(yt);
YAP_PutInSlot(l, yt);
Yap_PutInHandle(l, YAP_TailOfTerm(yt));
ob = yap_to_python(hd, true, ob, false);
if (!ob) {
return Py_None;

View File

@@ -20,6 +20,7 @@ void pyErrorHandler__(int line, const char *file, const char *code) {
static foreign_t python_len(term_t tobj, term_t tf) {
Py_ssize_t len;
PyObject *o;
PyStart();
o = term_to_python(tobj, true, NULL, true);
if (o == NULL) {
@@ -32,13 +33,14 @@ static foreign_t python_len(term_t tobj, term_t tf) {
static foreign_t python_represent( term_t name, term_t tobj) {
term_t stackp = python_acquire_GIL();
PyObject *e;
PyStart();
e = term_to_python(tobj, false, NULL, false);
if (e == NULL) {
python_release_GIL(stackp);
pyErrorAndReturn(false);
}
bool b = python_assign(name, e, NULL);
foreign_t b = python_assign(name, e, NULL);
python_release_GIL(stackp);
pyErrorAndReturn(b);
}
@@ -53,6 +55,7 @@ static foreign_t python_clear_errors(void) {
static foreign_t python_dir(term_t tobj, term_t tf) {
PyObject *dir;
PyObject *o;
PyStart();
o = term_to_python(tobj, true, NULL, true);
if (o == NULL) {
@@ -70,6 +73,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
PyObject *i;
PyObject *o;
PyObject *f;
PyStart();
o = term_to_python(tobj, true, NULL, true);
if (o == NULL) {
@@ -96,6 +100,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
static foreign_t python_is(term_t tobj, term_t tf) {
PyObject *o;
PyStart();
term_t lim = python_acquire_GIL();
@@ -112,13 +117,13 @@ static foreign_t python_is(term_t tobj, term_t tf) {
}
static foreign_t python_proc(term_t tobj) {
PyStart();
PyObject *o;
term_t lim = python_acquire_GIL();
o = term_to_python(tobj, true, NULL, true);
python_release_GIL(lim);
bool rc = o != NULL;
foreign_t rc = o != NULL;
pyErrorAndReturn(rc);
}
@@ -134,9 +139,7 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
p = term_to_python(parent, true, NULL, true);
// Exp
if (!pI || !p) {
{
pyErrorAndReturn(false);
}
pyErrorAndReturn(false);
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
PyErr_Print();
{ pyErrorAndReturn(false); }
@@ -153,6 +156,7 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t tf) {
PyStart();
PyObject *pF;
PyObject *pArgs, *pKeywords;
PyObject *pValue;
@@ -162,7 +166,6 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t targ = PL_new_term_ref();
pF = term_to_python(tin, true, NULL, true);
PyErr_Clear();
if (pF == NULL) {
{
pyErrorAndReturn(false);
@@ -237,6 +240,7 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
}
static foreign_t assign_python(term_t exp, term_t name) {
PyStart();
term_t stackp = python_acquire_GIL();
PyObject *e = term_to_python(exp, true, NULL, true);
@@ -244,7 +248,7 @@ static foreign_t assign_python(term_t exp, term_t name) {
python_release_GIL(stackp);
pyErrorAndReturn(false);
}
bool b = python_assign(name, e, NULL);
foreign_t b = python_assign(name, e, NULL);
python_release_GIL(stackp);
pyErrorAndReturn(b);
}
@@ -269,7 +273,7 @@ static foreign_t python_string_to(term_t f) {
}
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
PyErr_Clear();
PyStart();
PyObject *pI, *pArgs, *pOut;
PyObject *env;
atom_t name;
@@ -331,7 +335,7 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
}
static foreign_t python_access(term_t obj, term_t f, term_t out) {
PyErr_Clear();
PyStart();
PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF;
atom_t name;
char *s = NULL;
@@ -400,7 +404,7 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
atom_t name;
char *s;
int arity;
PyStart();
if (!PL_get_name_arity(att, &name, &arity)) {
{
pyErrorAndReturn(false);
@@ -448,14 +452,14 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
static foreign_t python_main_module(term_t mod) {
{
foreign_t rc;
PyErr_Clear();
PyStart();
rc = address_to_term(py_Main, mod);
pyErrorAndReturn(rc);
}
}
static foreign_t python_function(term_t tobj) {
PyErr_Clear();
PyStart();
PyObject *obj = term_to_python(tobj, true, NULL, true);
foreign_t rc = PyFunction_Check(obj);
@@ -465,7 +469,7 @@ static foreign_t python_function(term_t tobj) {
foreign_t python_builtin(term_t out) {
{
foreign_t rc;
PyErr_Clear();
PyStart();
rc = address_to_term(py_Builtin, out);
pyErrorAndReturn(rc);
}
@@ -476,7 +480,7 @@ static foreign_t python_run_file(term_t file) {
size_t len;
char si[256];
s = si;
PyErr_Clear();
PyStart();
if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
#if PY_MAJOR_VERSION < 3
PyObject *PyFileObject = PyFile_FromString(si, "r");
@@ -499,11 +503,11 @@ extern PyThreadState *YAP_save;
static foreign_t python_run_command(term_t cmd) {
char *s;
bool rc = false;
foreign_t rc = false;
size_t len;
char si[256];
PyErr_Clear();
PyStart();
s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
PyRun_SimpleString(s);
@@ -519,7 +523,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
PyObject *pArgs = NULL, *pValue;
char *s;
PyErr_Clear();
PyStart();
s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) &&
(s = sf) != NULL &&
@@ -571,7 +575,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
static foreign_t python_export(term_t t, term_t pl) {
foreign_t rc = false;
PyErr_Clear();
PyStart();
if (PL_is_functor(t, FUNCTOR_pointer1)) {
void *ptr;
term_t targ = PL_new_term_ref();
@@ -600,7 +604,8 @@ static foreign_t python_export(term_t t, term_t pl) {
*/
static int python_import(term_t mname, term_t mod) {
PyObject *pName;
bool do_as = false;
foreign_t do_as = false;
PyStart();
char s0[MAXPATHLEN], *s = s0;
s[0] = '\0';
@@ -673,7 +678,7 @@ static int python_import(term_t mname, term_t mod) {
static foreign_t python_to_rhs(term_t inp, term_t t) {
PyObject *pVal;
PyErr_Clear();
PyStart();
pVal = term_to_python(inp, true, NULL, true);
if (pVal == NULL)
pyErrorAndReturn(false);
@@ -681,7 +686,7 @@ static foreign_t python_to_rhs(term_t inp, term_t t) {
}
// static PyThreadState *_saveP = NULL;
static bool _threaded = true;
static foreign_t _threaded = true;
/*
static YAP_Int
@@ -707,7 +712,7 @@ PyThreadState *tstate;
static YAP_Int p_python_threaded(void) {
PyErr_Clear();
PyStart();
// PyEval_ReleaseThread(tstate);
// _threaded = true;
// _locked = 0;
@@ -730,7 +735,6 @@ term_t python_acquire_GIL(void) {
PL_put_integer(curSlot, gstatei++);
return curSlot;
}
bool python_release_GIL(term_t curBlock) {
int gstateix;
gstatei--;