smallbugs

This commit is contained in:
Vitor Santos Costa 2017-06-18 11:14:55 +01:00
parent 31f1c25ee7
commit 6b545c8f71
7 changed files with 338 additions and 373 deletions

View File

@ -1427,7 +1427,7 @@ OpenRestore(const char *inpf, const char *YapLibDir, CELL *Astate, CELL *ATrail,
char fname[YAP_FILENAME_MAX +1]; char fname[YAP_FILENAME_MAX +1];
if (!Yap_findFile( inpf, YAP_STARTUP, YapLibDir, fname, true, YAP_SAVED_STATE, true, true)) if (!Yap_findFile( inpf, YAP_STARTUP, YapLibDir, fname, true, YAP_SAVED_STATE, true, true))
return false; return FAIL_RESTORE;
if (fname[0] && if (fname[0] &&
(mode = try_open(fname,Astate,ATrail,AStack,AHeap,streamp)) != FAIL_RESTORE) { (mode = try_open(fname,Astate,ATrail,AStack,AHeap,streamp)) != FAIL_RESTORE) {
return mode; return mode;

View File

@ -1,17 +1,17 @@
/************************************************************************* /*************************************************************************
* * * *
* YAP Prolog %W% %G% * * YAP Prolog %W% %G% *
* Yap Prolog was developed at NCCUP - Universidade do Porto * * Yap Prolog was developed at NCCUP - Universidade do Porto *
* * * *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-2012 * * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-2012 *
* * * *
************************************************************************** **************************************************************************
* * * *
* File: YapCompounTerm.h * * File: YapCompounTerm.h *
* mods: * * mods: *
* comments: main header file for YAP * * comments: main header file for YAP *
* version: $Id: Yap.h,v 1.38 2008-06-18 10:02:27 vsc Exp $ * * version: $Id: Yap.h,v 1.38 2008-06-18 10:02:27 vsc Exp $ *
*************************************************************************/ *************************************************************************/
/************************************************************************************************* /*************************************************************************************************
High level macros to access arguments High level macros to access arguments
@ -23,7 +23,7 @@
#include "inline-only.h" #include "inline-only.h"
EXTERN Int Yap_unify(Term a, Term b); EXTERN bool Yap_unify(Term a, Term b);
INLINE_ONLY EXTERN inline Term Deref(Term a); INLINE_ONLY EXTERN inline Term Deref(Term a);
@ -39,7 +39,6 @@ INLINE_ONLY EXTERN inline Term Deref(Term a) {
INLINE_ONLY EXTERN inline CELL *GetTermAdress(Term a); INLINE_ONLY EXTERN inline CELL *GetTermAdress(Term a);
INLINE_ONLY EXTERN inline Term Derefa(CELL *b); INLINE_ONLY EXTERN inline Term Derefa(CELL *b);
INLINE_ONLY EXTERN inline Term Derefa(CELL *b) { INLINE_ONLY EXTERN inline Term Derefa(CELL *b) {

View File

@ -374,19 +374,19 @@ close_attvar_chain(CELL *dvarsmin, CELL *dvarsmax) {
} }
INLINE_ONLY EXTERN inline INLINE_ONLY EXTERN inline
Int Yap_unify(Term t0, Term t1); bool Yap_unify(Term t0, Term t1);
INLINE_ONLY EXTERN inline INLINE_ONLY EXTERN inline
Int Yap_unify(Term t0, Term t1) bool Yap_unify(Term t0, Term t1)
{ {
CACHE_REGS CACHE_REGS
tr_fr_ptr TR0 = TR; tr_fr_ptr TR0 = TR;
if (Yap_IUnify(t0,t1)) { if (Yap_IUnify(t0,t1)) {
return TRUE; return true;
} else { } else {
reset_trail(TR0); reset_trail(TR0);
return FALSE; return false;
} }
} }

View File

@ -4,42 +4,37 @@
extern PyObject *py_Local, *py_Global; extern PyObject *py_Local, *py_Global;
PyObject *YE(term_t t, int line, const char *file, const char *code) PyObject *YE(term_t t, int line, const char *file, const char *code) {
{
char buf[1024]; char buf[1024];
YAP_WriteBuffer(YAP_GetFromSlot(t), buf, 1023, 0); YAP_WriteBuffer(YAP_GetFromSlot(t), buf, 1023, 0);
fprintf(stderr, "**** Warning,%s@%s:%d: failed on expression %s\n", code, file, line, buf); fprintf(stderr, "**** Warning,%s@%s:%d: failed on expression %s\n", code,
file, line, buf);
return NULL; return NULL;
} }
void YEM(const char *exp, int line, const char *file, const char *code) void YEM(const char *exp, int line, const char *file, const char *code) {
{ fprintf(stderr, "**** Warning,%s@%s:%d: failed while executing %s\n", code,
fprintf(stderr, "**** Warning,%s@%s:%d: failed while executing %s\n", code, file, line, exp); file, line, exp);
} }
static PyObject *s_to_python( const char *s, bool eval, PyObject *p0) static PyObject *s_to_python(const char *s, bool eval, PyObject *p0) {
{
PyObject *o; PyObject *o;
if (eval) if (eval) {
{ o = PythonLookup(s, p0);
o = PythonLookup(s,p0); /* if (!o)
/* if (!o) return o;
return o; */
*/ } else {
} o = PythonLookupSpecial(s);
else }
{ if (o) {
o = PythonLookupSpecial(s); Py_INCREF(o);
} return CHECKNULL(YAP_MkStringTerm(s), o);
if (o) } else {
{ PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Py_INCREF(o); return pobj;
return CHECKNULL(YAP_MkStringTerm(s), o); }
} else {
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
return pobj;
}
} }
/** /**
@ -50,19 +45,18 @@ static PyObject *s_to_python( const char *s, bool eval, PyObject *p0)
* *
* @return a Python object descriptor or NULL if failed * @return a Python object descriptor or NULL if failed
*/ */
X_API PyObject *string_to_python( const char *s, bool eval, PyObject *p0) X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) {
{ char *buf = malloc(strlen(s) + 1), *child;
char *buf = malloc(strlen(s)+1), *child; while ((child = strchr(s, '.')) != NULL) {
while((child = strchr(s,'.')) != NULL ) {
size_t len = child - s; size_t len = child - s;
strncpy(buf,s,len); strncpy(buf, s, len);
buf[len]='\0'; buf[len] = '\0';
p0 = s_to_python(buf, eval, p0); p0 = s_to_python(buf, eval, p0);
s = child+1; s = child + 1;
} }
return s_to_python(s, eval, p0); return s_to_python(s, eval, p0);
} }
/** /**
* term_to_python translates and evaluates from Prolog to Python * term_to_python translates and evaluates from Prolog to Python
* *
@ -71,77 +65,58 @@ X_API PyObject *string_to_python( const char *s, bool eval, PyObject *p0)
* *
* @return a Python object descriptor or NULL if failed * @return a Python object descriptor or NULL if failed
*/ */
PyObject *term_to_python(term_t t, bool eval, PyObject *o) PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
{
// o≈ // o≈
YAP_Term yt = YAP_GetFromSlot(t); YAP_Term yt = YAP_GetFromSlot(t);
// Yap_DebugPlWriteln(yt); // Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) switch (PL_term_type(t)) {
{ case PL_VARIABLE: {
case PL_VARIABLE: if (t == 0) {
{
if (t == 0)
{
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, yt, "in term_to_python"); Yap_ThrowError(SYSTEM_ERROR_INTERNAL, yt, "in term_to_python");
} }
PyObject *out = PyTuple_New(1); PyObject *out = PyTuple_New(1);
PyTuple_SET_ITEM(out, 0, PyLong_FromLong(t)); PyTuple_SET_ITEM(out, 0, PyLong_FromLong(t));
return term_to_nametuple("v", 1, out); return term_to_nametuple("v", 1, out);
}; };
case PL_ATOM: case PL_ATOM: {
{
YAP_Atom at = YAP_AtomOfTerm(yt); YAP_Atom at = YAP_AtomOfTerm(yt);
const char *s; const char *s;
s = YAP_AtomName(at); s = YAP_AtomName(at);
if (eval) if (eval) {
{
o = PythonLookup(s, o); o = PythonLookup(s, o);
/* if (!o) /* if (!o)
return o; return o;
*/ */
} } else {
else
{
o = PythonLookupSpecial(s); o = PythonLookupSpecial(s);
} }
if (o) if (o) {
{
Py_INCREF(o); Py_INCREF(o);
return CHECKNULL(t, o); return CHECKNULL(t, o);
} }
} }
case PL_STRING: case PL_STRING: {
{
const char *s = NULL; const char *s = NULL;
if (YAP_IsAtomTerm(yt)) if (YAP_IsAtomTerm(yt)) {
{
s = YAP_AtomName(YAP_AtomOfTerm(yt)); s = YAP_AtomName(YAP_AtomOfTerm(yt));
} } else if (YAP_IsStringTerm(yt)) {
else if (YAP_IsStringTerm(yt))
{
s = YAP_StringOfTerm(yt); s = YAP_StringOfTerm(yt);
} } else {
else
{
return CHECKNULL(t, NULL); return CHECKNULL(t, NULL);
} }
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (proper_ascii_string(s)) if (proper_ascii_string(s)) {
{
PyObject *o = PyString_FromStringAndSize(s, strlen(s)); PyObject *o = PyString_FromStringAndSize(s, strlen(s));
return CHECKNULL(t, o); return CHECKNULL(t, o);
} } else
else
#endif #endif
{ {
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL); PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
return CHECKNULL(t, pobj); return CHECKNULL(t, pobj);
} }
} } break;
break; case PL_INTEGER: {
case PL_INTEGER:
{
int64_t j; int64_t j;
if (!PL_get_int64_ex(t, &j)) if (!PL_get_int64_ex(t, &j))
return CHECKNULL(t, NULL); return CHECKNULL(t, NULL);
@ -154,8 +129,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o)
#endif #endif
} }
case PL_FLOAT: case PL_FLOAT: {
{
PyObject *out; PyObject *out;
double fl; double fl;
if (!PL_get_float(t, &fl)) if (!PL_get_float(t, &fl))
@ -163,45 +137,39 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o)
out = PyFloat_FromDouble(fl); out = PyFloat_FromDouble(fl);
return CHECKNULL(t, out); return CHECKNULL(t, out);
} }
default: default: {
{
term_t tail = PL_new_term_ref(), arg; term_t tail = PL_new_term_ref(), arg;
size_t len, i; size_t len, i;
if (PL_skip_list(t, tail, &len) && PL_get_nil(tail)) if (PL_skip_list(t, tail, &len) && PL_get_nil(tail)) {
{
PyObject *out, *a; PyObject *out, *a;
arg = tail; arg = tail;
out = PyList_New(len); out = PyList_New(len);
if (!out) if (!out) {
PL_reset_term_refs(tail);
return CHECKNULL(t, Py_None); return CHECKNULL(t, Py_None);
}
for (i = 0; i < len; i++) for (i = 0; i < len; i++) {
{ if (!PL_get_list(t, arg, t)) {
if (!PL_get_list(t, arg, t))
{
PL_reset_term_refs(tail); PL_reset_term_refs(tail);
return Py_None; return Py_None;
} }
a = term_to_python(arg, eval, o); a = term_to_python(arg, eval, o);
if (a ) if (a) {
{ if (PyList_SetItem(out, i, a) < 0) {
if (PyList_SetItem(out, i, a) < 0) PL_reset_term_refs(tail);
{ return Py_None;
return Py_None;
} }
} }
} }
PL_reset_term_refs(tail); PL_reset_term_refs(tail);
return CHECKNULL(t, out); return CHECKNULL(t, out);
} } else {
else
{
functor_t fun; functor_t fun;
PyObject *rc; PyObject *rc;
if (!PL_get_functor(t, &fun)) if (!PL_get_functor(t, &fun)) {
{
PL_reset_term_refs(tail); PL_reset_term_refs(tail);
return CHECKNULL(t, Py_None); return CHECKNULL(t, Py_None);
} }
@ -217,8 +185,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o)
return CHECKNULL(t, Py_None); return CHECKNULL(t, Py_None);
} }
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
{
if (t == 0) if (t == 0)
return NULL; return NULL;
term_t yt = YAP_InitSlot(t); term_t yt = YAP_InitSlot(t);
@ -227,13 +194,11 @@ PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o)
return o; return o;
} }
PyObject *deref_term_to_python(term_t t) PyObject *deref_term_to_python(term_t t) {
{
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I // Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
// am\n"); // am\n");
YAP_Term yt = YAP_GetFromSlot(t); YAP_Term yt = YAP_GetFromSlot(t);
if (YAP_IsVarTerm(yt)) if (YAP_IsVarTerm(yt)) {
{
char s[32]; char s[32];
char *o = YAP_WriteBuffer(yt, s, 31, 0); char *o = YAP_WriteBuffer(yt, s, 31, 0);
PyObject *p = PyUnicode_FromString(o); PyObject *p = PyUnicode_FromString(o);

View File

@ -1,6 +1,6 @@
#include "py4yap.h" #include "py4yap.h"
static foreign_t repr_term(PyObject *pVal, term_t t) { static foreign_t repr_term(PyObject *pVal, term_t t) {
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref(); term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
PL_put_pointer(t1, pVal); PL_put_pointer(t1, pVal);
@ -25,11 +25,14 @@ foreign_t assign_to_symbol(term_t t, PyObject *e) {
foreign_t python_to_term(PyObject *pVal, term_t t) { foreign_t python_to_term(PyObject *pVal, term_t t) {
bool rc = true; bool rc = true;
term_t to = PL_new_term_ref(); term_t to = PL_new_term_ref();
// fputs(" <<*** ",stderr); PyObject_Print(pVal,stderr,0); fputs("<<***\n",stderr); // fputs(" <<*** ",stderr); PyObject_Print(pVal,stderr,0);
// fputs("<<***\n",stderr);
if (pVal == Py_None) { if (pVal == Py_None) {
//fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" >>***\n",stderr); // fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
rc= PL_unify_atom(t, ATOM_none); // >>***\n",stderr);
//fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" >>***\n",stderr); rc = PL_unify_atom(t, ATOM_none);
// fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// >>***\n",stderr);
} else if (PyBool_Check(pVal)) { } else if (PyBool_Check(pVal)) {
rc = rc && PL_unify_bool(t, PyObject_IsTrue(pVal)); rc = rc && PL_unify_bool(t, PyObject_IsTrue(pVal));
} else if (PyLong_Check(pVal)) { } else if (PyLong_Check(pVal)) {
@ -41,8 +44,7 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
} else if (PyFloat_Check(pVal)) { } else if (PyFloat_Check(pVal)) {
rc = rc && PL_unify_float(t, PyFloat_AsDouble(pVal)); rc = rc && PL_unify_float(t, PyFloat_AsDouble(pVal));
} else if (PyComplex_Check(pVal)) { } else if (PyComplex_Check(pVal)) {
term_t t1 = PL_new_term_ref(), term_t t1 = PL_new_term_ref(), t2 = PL_new_term_ref();
t2 = PL_new_term_ref();
if (!PL_put_float(t1, PyComplex_RealAsDouble(pVal)) || if (!PL_put_float(t1, PyComplex_RealAsDouble(pVal)) ||
!PL_put_float(t2, PyComplex_ImagAsDouble(pVal)) || !PL_put_float(t2, PyComplex_ImagAsDouble(pVal)) ||
!PL_cons_functor(to, FUNCTOR_complex2, t1, t2)) { !PL_cons_functor(to, FUNCTOR_complex2, t1, t2)) {
@ -61,7 +63,7 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
free(ptr); free(ptr);
#else #else
const char *s = PyUnicode_AsUTF8(pVal); const char *s = PyUnicode_AsUTF8(pVal);
tmp_atom = PL_new_atom( s); tmp_atom = PL_new_atom(s);
#endif #endif
rc = rc && PL_unify_atom(t, tmp_atom); rc = rc && PL_unify_atom(t, tmp_atom);
} else if (PyByteArray_Check(pVal)) { } else if (PyByteArray_Check(pVal)) {
@ -80,63 +82,63 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
rc = rc && PL_unify_atom(t, ATOM_brackets); rc = rc && PL_unify_atom(t, ATOM_brackets);
} else { } else {
if ((s = (Py_TYPE(pVal)->tp_name))) { if ((s = (Py_TYPE(pVal)->tp_name))) {
if (!strcmp(s, "H")) { if (!strcmp(s, "H")) {
pVal = PyTuple_GetItem(pVal, 0); pVal = PyTuple_GetItem(pVal, 0);
if (pVal==NULL) { if (pVal == NULL) {
pVal = Py_None; pVal = Py_None;
PyErr_Clear(); PyErr_Clear();
} }
} }
if (s[0] == '$') { if (s[0] == '$') {
char *ns = malloc(strlen(s) + 5); char *ns = malloc(strlen(s) + 5);
strcpy(ns, "__"); strcpy(ns, "__");
strcat(ns, s + 1); strcat(ns, s + 1);
strcat(ns, "__"); strcat(ns, "__");
f = PL_new_functor(PL_new_atom(ns), sz); f = PL_new_functor(PL_new_atom(ns), sz);
} else { } else {
f = PL_new_functor(PL_new_atom(s), sz); f = PL_new_functor(PL_new_atom(s), sz);
} }
} else { } else {
f = PL_new_functor(ATOM_t, sz); f = PL_new_functor(ATOM_t, sz);
} }
if (PL_unify_functor(t, f)) { if (PL_unify_functor(t, f)) {
for (i = 0; i < sz; i++) { for (i = 0; i < sz; i++) {
if (!PL_get_arg(i + 1, t, to)) if (!PL_get_arg(i + 1, t, to))
rc = false; rc = false;
PyObject *p = PyTuple_GetItem(pVal, i); PyObject *p = PyTuple_GetItem(pVal, i);
if (p==NULL) { if (p == NULL) {
PyErr_Clear(); PyErr_Clear();
p = Py_None; p = Py_None;
} }
rc = rc && python_to_term(p, to); rc = rc && python_to_term(p, to);
}
} else {
rc = false;
} }
} else { // fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
rc = false; // ||***\n",stderr);
}
//fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" ||***\n",stderr);
} }
} else if (PyList_Check(pVal)) { } else if (PyList_Check(pVal)) {
Py_ssize_t i, sz = PyList_GET_SIZE(pVal); Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
if (sz == 0) { if (sz == 0) {
rc = rc && PL_unify_nil(t);
} else {
for (i = 0; i < sz; i++) {
PyObject *obj;
if (!PL_unify_list(t, to, t)) {
rc = false;
break;
}
if ((obj = PyList_GetItem(pVal, i)) == NULL) {
obj = Py_None;
}
rc = rc && python_to_term(obj, to);
}
rc = rc && PL_unify_nil(t); rc = rc && PL_unify_nil(t);
} else {
for (i = 0; i < sz; i++) {
PyObject *obj;
if (!PL_unify_list(t, to, t)) {
rc = false;
break;
}
if ((obj = PyList_GetItem(pVal, i)) == NULL) {
obj = Py_None;
}
rc = rc && python_to_term(obj, to);
} }
//fputs("[***] ", stderr); rc = rc && PL_unify_nil(t);
//Yap_DebugPlWrite(yt); fputs("[***]\n", stderr); }
// fputs("[***] ", stderr);
// Yap_DebugPlWrite(yt); fputs("[***]\n", stderr);
} else if (PyDict_Check(pVal)) { } else if (PyDict_Check(pVal)) {
Py_ssize_t pos = 0; Py_ssize_t pos = 0;
term_t to = PL_new_term_ref(), ti = to; term_t to = PL_new_term_ref(), ti = to;
@ -147,30 +149,31 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
rc = rc && PL_unify_atom(t, ATOM_curly_brackets); rc = rc && PL_unify_atom(t, ATOM_curly_brackets);
} else { } else {
while (PyDict_Next(pVal, &pos, &key, &value)) { while (PyDict_Next(pVal, &pos, &key, &value)) {
term_t tkey = PL_new_term_ref(), tval = PL_new_term_ref(), tint, term_t tkey = PL_new_term_ref(), tval = PL_new_term_ref(), tint,
tnew = PL_new_term_ref(); tnew = PL_new_term_ref();
/* do something interesting with the values... */ /* do something interesting with the values... */
if (!python_to_term(key, tkey)) { if (!python_to_term(key, tkey)) {
continue; continue;
} }
if (!python_to_term(value, tval)) { if (!python_to_term(value, tval)) {
continue; continue;
} }
/* reuse */ /* reuse */
tint = tkey; tint = tkey;
if (!PL_cons_functor(tint, FUNCTOR_colon2, tkey, tval)) { if (!PL_cons_functor(tint, FUNCTOR_colon2, tkey, tval)) {
rc = false; rc = false;
continue; continue;
} }
if (--left) { if (--left) {
if (!PL_cons_functor(tint, FUNCTOR_comma2, tint, tnew)) if (!PL_cons_functor(tint, FUNCTOR_comma2, tint, tnew))
PL_reset_term_refs(tkey); PL_reset_term_refs(tkey);
rc = false; rc = false;
} }
if (!PL_unify(ti, tint)) { if (!PL_unify(ti, tint)) {
rc = false; } rc = false;
ti = tnew; }
PL_reset_term_refs(tkey); ti = tnew;
PL_reset_term_refs(tkey);
} }
rc = rc && PL_unify(t, to); rc = rc && PL_unify(t, to);
} }
@ -183,9 +186,10 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
X_API YAP_Term pythonToYAP(PyObject *pVal) { X_API YAP_Term pythonToYAP(PyObject *pVal) {
term_t t = PL_new_term_ref(); term_t t = PL_new_term_ref();
if (pVal == NULL || if (pVal == NULL || !python_to_term(pVal, t)) {
!python_to_term(pVal, t)) PL_reset_term_refs(t);
return 0; return 0;
}
YAP_Term tt = YAP_GetFromSlot(t); YAP_Term tt = YAP_GetFromSlot(t);
PL_reset_term_refs(t); PL_reset_term_refs(t);
Py_DECREF(pVal); Py_DECREF(pVal);
@ -238,25 +242,25 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
if (PL_skip_list(t, tail, &len) && if (PL_skip_list(t, tail, &len) &&
PL_get_nil(tail)) { // true list PL_get_nil(tail)) { // true list
bool o = true; bool o = true;
if (PySequence_Check(exp) && PySequence_Length(exp) == len) if (PySequence_Check(exp) && PySequence_Length(exp) == len)
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyObject *p; PyObject *p;
if (!PL_get_list(t, arg, t)) { if (!PL_get_list(t, arg, t)) {
PL_reset_term_refs(tail); PL_reset_term_refs(tail);
o = false; o = false;
p = Py_None; p = Py_None;
} }
if ((p = PySequence_GetItem(exp, i)) == NULL) if ((p = PySequence_GetItem(exp, i)) == NULL)
p = Py_None; p = Py_None;
if (!python_assign(arg, p, context)) { if (!python_assign(arg, p, context)) {
PL_reset_term_refs(tail); PL_reset_term_refs(tail);
o = false; o = false;
} }
} }
PL_reset_term_refs(tail); PL_reset_term_refs(tail);
return o; return o;
} else { } else {
functor_t fun; functor_t fun;
@ -287,8 +291,8 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
} }
if (PyList_Check(i)) { if (PyList_Check(i)) {
i = PyList_GetItem(i, 0); i = PyList_GetItem(i, 0);
if (i==NULL) if (i == NULL)
i = Py_None; i = Py_None;
long int j; long int j;
if (PyList_Check(o)) { if (PyList_Check(o)) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3

View File

@ -14,7 +14,7 @@ static PyObject *finalLookup(PyObject *i, const char *s) {
PyObject *rc; PyObject *rc;
if (i == NULL) if (i == NULL)
return NULL; return NULL;
if (strcmp(s,"none") == 0) if (strcmp(s, "none") == 0)
return Py_None; return Py_None;
if (PyDict_Check(i)) { if (PyDict_Check(i)) {
if ((rc = PyDict_GetItemString(i, s))) if ((rc = PyDict_GetItemString(i, s)))
@ -68,14 +68,14 @@ PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
return out; return out;
} }
PyObject *py_Global = PyEval_GetGlobals(); PyObject *py_Global = PyEval_GetGlobals();
if ((out = finalLookup(py_Global, sp)) ) { if ((out = finalLookup(py_Global, sp))) {
return out; return out;
} }
if ((out = finalLookup(py_ModDict, sp))) { if ((out = finalLookup(py_ModDict, sp))) {
return out; return out;
} }
if ((out = finalLookup(py_Main, sp)) ) { if ((out = finalLookup(py_Main, sp))) {
return out; return out;
} }
return NULL; return NULL;
} }
@ -101,12 +101,11 @@ PyObject *find_obj(PyObject *ob, term_t l, bool eval) {
hd = YAP_HeadOfTerm(yt); hd = YAP_HeadOfTerm(yt);
ob = yap_to_python(hd, true, ob); ob = yap_to_python(hd, true, ob);
ob = CHECKNULL(yt, ob); ob = CHECKNULL(yt, ob);
if (!ob){ if (!ob) {
return Py_None; return Py_None;
} }
yt = YAP_TailOfTerm(yt); yt = YAP_TailOfTerm(yt);
}
}
YAP_PutInSlot(l, yt); YAP_PutInSlot(l, yt);
return ob; return ob;
} }
@ -122,9 +121,9 @@ PyObject *find_obj(PyObject *ob, term_t l, bool eval) {
static PyObject *bip_abs(term_t t) { static PyObject *bip_abs(term_t t) {
PyObject *pVal, *nVal; PyObject *pVal, *nVal;
AOK( PL_get_arg(1, t, t), NULL); AOK(PL_get_arg(1, t, t), NULL);
pVal = term_to_python(t, true, NULL); pVal = term_to_python(t, true, NULL);
pVal = CHECKNULL( t, pVal ); pVal = CHECKNULL(t, pVal);
nVal = PyNumber_Absolute(pVal); nVal = PyNumber_Absolute(pVal);
Py_DecRef(pVal); Py_DecRef(pVal);
return nVal; return nVal;
@ -143,11 +142,11 @@ static PyObject *bip_all(term_t t) {
PyObject *(*iternext)(PyObject *); PyObject *(*iternext)(PyObject *);
int cmp; int cmp;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
(v = term_to_python(t, true, NULL) ); (v = term_to_python(t, true, NULL));
v = CHECKNULL( t, v); v = CHECKNULL(t, v);
it = PyObject_GetIter(v); it = PyObject_GetIter(v);
if (CHECKNULL( t, it) == NULL) if (CHECKNULL(t, it) == NULL)
return Py_None; return Py_None;
iternext = *Py_TYPE(it)->tp_iternext; iternext = *Py_TYPE(it)->tp_iternext;
@ -160,7 +159,7 @@ static PyObject *bip_all(term_t t) {
// PyObject_Print(v, stderr, 0); // PyObject_Print(v, stderr, 0);
for (;;) { for (;;) {
item = iternext(it); item = iternext(it);
if (CHECKNULL(t,item) == NULL) if (CHECKNULL(t, item) == NULL)
break; break;
cmp = PyObject_IsTrue(item); cmp = PyObject_IsTrue(item);
Py_DECREF(item); Py_DECREF(item);
@ -190,16 +189,16 @@ static PyObject *bip_any(term_t t) {
PyObject *(*iternext)(PyObject *); PyObject *(*iternext)(PyObject *);
int cmp; int cmp;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
v = term_to_python(t, true, NULL); v = term_to_python(t, true, NULL);
it = PyObject_GetIter(v); it = PyObject_GetIter(v);
if (CHECKNULL(t,it) == NULL) if (CHECKNULL(t, it) == NULL)
return Py_None; return Py_None;
iternext = *Py_TYPE(it)->tp_iternext; iternext = *Py_TYPE(it)->tp_iternext;
for (;;) { for (;;) {
item = iternext(it); item = iternext(it);
if (CHECKNULL(t,item) == NULL) if (CHECKNULL(t, item) == NULL)
break; break;
cmp = PyObject_IsTrue(item); cmp = PyObject_IsTrue(item);
Py_DECREF(item); Py_DECREF(item);
@ -217,7 +216,7 @@ static PyObject *bip_any(term_t t) {
if (PyErr_ExceptionMatches(PyExc_StopIteration)) if (PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear(); PyErr_Clear();
else else
return CHECKNULL(t,NULL); return CHECKNULL(t, NULL);
} }
Py_RETURN_FALSE; Py_RETURN_FALSE;
} }
@ -225,7 +224,7 @@ static PyObject *bip_any(term_t t) {
static PyObject *bip_bin(term_t t) { static PyObject *bip_bin(term_t t) {
PyObject *v; PyObject *v;
AOK ( PL_get_arg(1, t, t), NULL); AOK(PL_get_arg(1, t, t), NULL);
v = term_to_python(t, true, NULL); v = term_to_python(t, true, NULL);
return PyNumber_ToBase(v, 2); return PyNumber_ToBase(v, 2);
} }
@ -233,7 +232,7 @@ static PyObject *bip_bin(term_t t) {
static PyObject *bip_float(term_t t, bool eval) { static PyObject *bip_float(term_t t, bool eval) {
PyObject *pVal, *o; PyObject *pVal, *o;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
pVal = term_to_python(t, eval, NULL); pVal = term_to_python(t, eval, NULL);
if (PyLong_Check(pVal)) { if (PyLong_Check(pVal)) {
o = PyFloat_FromDouble(PyLong_AsLong(pVal)); o = PyFloat_FromDouble(PyLong_AsLong(pVal));
@ -252,7 +251,7 @@ static PyObject *bip_float(term_t t, bool eval) {
static PyObject *bip_int(term_t t) { static PyObject *bip_int(term_t t) {
PyObject *pVal, *o; PyObject *pVal, *o;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
pVal = term_to_python(t, true, NULL); pVal = term_to_python(t, true, NULL);
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (PyLong_Check(pVal)) { if (PyLong_Check(pVal)) {
@ -278,7 +277,7 @@ static PyObject *bip_int(term_t t) {
static PyObject *bip_long(term_t t) { static PyObject *bip_long(term_t t) {
PyObject *pVal, *o; PyObject *pVal, *o;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
pVal = term_to_python(t, true, NULL); pVal = term_to_python(t, true, NULL);
if (PyLong_Check(pVal)) { if (PyLong_Check(pVal)) {
return pVal; return pVal;
@ -297,8 +296,8 @@ static PyObject *bip_long(term_t t) {
static PyObject *bip_iter(term_t t) { static PyObject *bip_iter(term_t t) {
PyObject *v; PyObject *v;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
v = term_to_python(t, true, NULL); v = term_to_python(t, true, NULL);
return PyObject_GetIter(v); return PyObject_GetIter(v);
} }
@ -306,8 +305,8 @@ static PyObject *bip_ord(term_t t) {
PyObject *pVal; PyObject *pVal;
Py_ssize_t size; Py_ssize_t size;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
pVal = term_to_python(t, true, NULL); pVal = term_to_python(t, true, NULL);
if (PyUnicode_Check(pVal)) { if (PyUnicode_Check(pVal)) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
size = PyUnicode_GET_SIZE(pVal); size = PyUnicode_GET_SIZE(pVal);
@ -349,7 +348,7 @@ static PyObject *bip_sum(term_t t) {
PyObject *result = NULL; PyObject *result = NULL;
PyObject *temp, *item, *iter; PyObject *temp, *item, *iter;
AOK( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
seq = term_to_python(t, true, NULL); seq = term_to_python(t, true, NULL);
iter = PyObject_GetIter(seq); iter = PyObject_GetIter(seq);
if (iter == NULL) if (iter == NULL)
@ -456,8 +455,8 @@ static PyObject *bip_sum(term_t t) {
} }
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (PyInt_CheckExact(item)) { if (PyInt_CheckExact(item)) {
764PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) 764PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter);
f_result += (double)PyInt_AS_LONG(item); return 0)f_result += (double)PyInt_AS_LONG(item);
PyFPE_END_PROTECT(f_result) Py_DECREF(item); PyFPE_END_PROTECT(f_result) Py_DECREF(item);
continue; continue;
} }
@ -561,15 +560,14 @@ static long get_len_of_range(long lo, long hi, long step) {
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
static PyStructSequence_Field pnull[] = { static PyStructSequence_Field pnull[] = {
{"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL}, {"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL}, {"A5", NULL},
{"A5", NULL}, {"A6", NULL}, {"A7", NULL}, {"A8", NULL}, {"A6", NULL}, {"A7", NULL}, {"A8", NULL}, {"A9", NULL}, {"A9", NULL},
{"A9", NULL}, {"A9", NULL}, {"A10", NULL}, {"A11", NULL}, {"A10", NULL}, {"A11", NULL}, {"A12", NULL}, {"A13", NULL}, {"A14", NULL},
{"A12", NULL}, {"A13", NULL}, {"A14", NULL}, {"A15", NULL}, {"A15", NULL}, {"A16", NULL}, {"A17", NULL}, {"A18", NULL}, {"A19", NULL},
{"A16", NULL}, {"A17", NULL}, {"A18", NULL}, {"A19", NULL}, {"A19", NULL}, {"A20", NULL}, {"A21", NULL}, {"A22", NULL}, {"A23", NULL},
{"A19", NULL}, {"A20", NULL}, {"A21", NULL}, {"A22", NULL}, {"A24", NULL}, {"A25", NULL}, {"A26", NULL}, {"A27", NULL}, {"A28", NULL},
{"A23", NULL}, {"A24", NULL}, {"A25", NULL}, {"A26", NULL}, {"A29", NULL}, {"A29", NULL}, {"A30", NULL}, {"A31", NULL}, {"A32", NULL},
{"A27", NULL}, {"A28", NULL}, {"A29", NULL}, {"A29", NULL}, {NULL, NULL}};
{"A30", NULL}, {"A31", NULL}, {"A32", NULL}, {NULL,NULL}};
static PyObject *structseq_str(PyObject *iobj) { static PyObject *structseq_str(PyObject *iobj) {
@ -698,10 +696,9 @@ static PyObject *structseq_repr(PyObject *iobj) {
} }
#endif #endif
PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) { PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
PyObject *o; PyObject *o;
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
PyTypeObject *typp; PyTypeObject *typp;
PyObject *key = PyUnicode_FromString(s); PyObject *key = PyUnicode_FromString(s);
if (py_F2P && PyDict_Contains(py_F2P, key)) { if (py_F2P && PyDict_Contains(py_F2P, key)) {
@ -711,7 +708,7 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
typp = calloc(sizeof(PyTypeObject), 1); typp = calloc(sizeof(PyTypeObject), 1);
PyStructSequence_Desc *desc = calloc(sizeof(PyStructSequence_Desc), 1); PyStructSequence_Desc *desc = calloc(sizeof(PyStructSequence_Desc), 1);
desc->name = PyMem_Malloc(strlen(s)+1); desc->name = PyMem_Malloc(strlen(s) + 1);
strcpy(desc->name, s); strcpy(desc->name, s);
Py_DECREF(key); Py_DECREF(key);
desc->doc = "YAPTerm"; desc->doc = "YAPTerm";
@ -724,22 +721,22 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
typp->tp_repr = structseq_repr; typp->tp_repr = structseq_repr;
// typp = PyStructSequence_NewType(desc); // typp = PyStructSequence_NewType(desc);
// don't do this: we cannot add a type as an atribute. // don't do this: we cannot add a type as an atribute.
//PyModule_AddObject(py_Main, s, (PyObject *)typp); // PyModule_AddObject(py_Main, s, (PyObject *)typp);
if (py_F2P) if (py_F2P)
PyDict_SetItem(py_F2P, key, (PyObject *)typp); PyDict_SetItem(py_F2P, key, (PyObject *)typp);
Py_INCREF(typp); Py_INCREF(typp);
} }
o = PyStructSequence_New(typp); o = PyStructSequence_New(typp);
arity_t i; arity_t i;
for (i=0; i < arity; i++) { for (i = 0; i < arity; i++) {
PyObject *pArg = PyTuple_GET_ITEM(tuple, i); PyObject *pArg = PyTuple_GET_ITEM(tuple, i);
Py_INCREF(pArg); Py_INCREF(pArg);
if (pArg) if (pArg)
PyStructSequence_SET_ITEM(o, i, pArg); PyStructSequence_SET_ITEM(o, i, pArg);
//PyObject_Print(pArg,stderr,0);fputc('\n',stderr); // PyObject_Print(pArg,stderr,0);fputc('\n',stderr);
} }
//((PyStructSequence *)o)->ob_base.ob_size = arity; //((PyStructSequence *)o)->ob_base.ob_size = arity;
//PyObject_Print(o,stderr,0);fputc('\n',stderr); // PyObject_Print(o,stderr,0);fputc('\n',stderr);
return o; return o;
#else #else
PyObject *o1; PyObject *o1;
@ -762,16 +759,16 @@ static PyObject *bip_range(term_t t) {
if (!PL_get_name_arity(t, &name, &arity)) if (!PL_get_name_arity(t, &name, &arity))
return NULL; return NULL;
AOK( PL_get_arg(1, t, arg), NULL); AOK(PL_get_arg(1, t, arg), NULL);
ilow = get_int(arg, true); ilow = get_int(arg, true);
if (arity == 1) { if (arity == 1) {
ihigh = ilow; ihigh = ilow;
ilow = 0; ilow = 0;
} else { } else {
AOK( PL_get_arg(2, t, arg), NULL ); AOK(PL_get_arg(2, t, arg), NULL);
ihigh = get_int(arg, true); ihigh = get_int(arg, true);
if (arity == 3) { if (arity == 3) {
AOK( PL_get_arg(3, t, arg), NULL ); AOK(PL_get_arg(3, t, arg), NULL);
istep = get_int(arg, true); istep = get_int(arg, true);
} }
} }
@ -784,7 +781,8 @@ static PyObject *bip_range(term_t t) {
else else
bign = get_len_of_range(ihigh, ilow, -istep); bign = get_len_of_range(ihigh, ilow, -istep);
n = (Py_ssize_t)bign; n = (Py_ssize_t)bign;
AOK ( ( (bign >= 0 && (long)n == bign) || "range() result has too many items" ), NULL ); AOK(((bign >= 0 && (long)n == bign) || "range() result has too many items"),
NULL);
v = PyList_New(n); v = PyList_New(n);
if (v == NULL) if (v == NULL)
@ -807,53 +805,52 @@ static PyObject *bip_range(term_t t) {
} }
static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux, static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
bool eval) { bool eval) {
PyObject *lhs, *rhs; PyObject *lhs, *rhs;
term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref(); term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref();
functor_t fun; functor_t fun;
AOK (PL_get_functor(targ, &fun), false);
while (fun == FUNCTOR_comma2) {
AOK( PL_get_arg(1, targ, tleft), false);
if (! copy_to_dictionary(dict, tleft, taux, eval) )
return false;
AOK ( PL_get_arg(2, targ, targ), false);
return copy_to_dictionary(dict, tright, taux, eval);
}
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
// Py_DECREF(lhs);
// Py_DECREF(rhs);
AOK(PL_get_functor(targ, &fun), false);
while (fun == FUNCTOR_comma2) {
AOK(PL_get_arg(1, targ, tleft), false); AOK(PL_get_arg(1, targ, tleft), false);
lhs = atom_to_python_string(tleft); if (!copy_to_dictionary(dict, tleft, taux, eval))
if (lhs == NULL) {
return FALSE;
}
AOK(PL_get_arg(2, targ, tright), false);
rhs = term_to_python(tright, eval, NULL);
if (rhs == NULL) {
PyErr_Print();
return false; return false;
} AOK(PL_get_arg(2, targ, targ), false);
return PyDict_SetItem(dict, lhs, rhs) >= 0; return copy_to_dictionary(dict, tright, taux, eval);
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
// Py_DECREF(lhs);
// Py_DECREF(rhs);
} }
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
// Py_DECREF(lhs);
// Py_DECREF(rhs);
AOK(PL_get_arg(1, targ, tleft), false);
lhs = atom_to_python_string(tleft);
if (lhs == NULL) {
return FALSE;
}
AOK(PL_get_arg(2, targ, tright), false);
rhs = term_to_python(tright, eval, NULL);
if (rhs == NULL) {
PyErr_Print();
return false;
}
return PyDict_SetItem(dict, lhs, rhs) >= 0;
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
// Py_DECREF(lhs);
// Py_DECREF(rhs);
}
PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) { PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
atom_t name; atom_t name;
int len; int len;
AOK (PL_get_name_arity(t, &name, &len), o); AOK(PL_get_name_arity(t, &name, &len), o);
if (fun == FUNCTOR_pointer1) { if (fun == FUNCTOR_pointer1) {
void *ptr; void *ptr;
AOK (PL_get_arg(1, t, t), NULL); AOK(PL_get_arg(1, t, t), NULL);
AOK (PL_get_pointer(t, &ptr), NULL) AOK(PL_get_pointer(t, &ptr), NULL)
/* return __main__,s */ /* return __main__,s */
return (PyObject *)ptr; return (PyObject *)ptr;
} }
@ -868,7 +865,7 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
if (!out) if (!out)
return NULL; return NULL;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
AOK ( PL_get_arg(i + 1, t, targ), NULL); AOK(PL_get_arg(i + 1, t, targ), NULL);
PyErr_Clear(); PyErr_Clear();
PyObject *oa = term_to_python(targ, true, o); PyObject *oa = term_to_python(targ, true, o);
bool rc = PyTuple_SET_ITEM(out, i, oa) == 0; bool rc = PyTuple_SET_ITEM(out, i, oa) == 0;
@ -882,11 +879,11 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs; PyObject *lhs, *rhs;
AOK ( PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
lhs = term_to_python(targ, true, NULL); lhs = term_to_python(targ, true, NULL);
if (!PyNumber_Check(lhs)) if (!PyNumber_Check(lhs))
return NULL; return NULL;
AOK ( PL_get_arg(2, t, targ), NULL ); AOK(PL_get_arg(2, t, targ), NULL);
rhs = term_to_python(targ, true, NULL); rhs = term_to_python(targ, true, NULL);
if (!PyNumber_Check(rhs)) if (!PyNumber_Check(rhs))
return NULL; return NULL;
@ -900,16 +897,16 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref(); term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
PyObject *v; PyObject *v;
Py_ssize_t min, max; Py_ssize_t min, max;
AOK (PL_get_arg(2, t, targ), NULL); AOK(PL_get_arg(2, t, targ), NULL);
v = term_to_python(targ, true, o); v = term_to_python(targ, true, o);
AOK (PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
AOK (PL_get_list(targ, trhs, targ) , NULL); AOK(PL_get_list(targ, trhs, targ), NULL);
if (PL_is_functor(trhs, FUNCTOR_colon2)) { if (PL_is_functor(trhs, FUNCTOR_colon2)) {
if (!PySequence_Check(v)) if (!PySequence_Check(v))
return NULL; return NULL;
min = get_p_int(term_to_python(targ, true, NULL), 0); min = get_p_int(term_to_python(targ, true, NULL), 0);
AOK (PL_get_arg(1, trhs, targ), NULL); AOK(PL_get_arg(1, trhs, targ), NULL);
if (PL_is_functor(targ, FUNCTOR_colon2)) { if (PL_is_functor(targ, FUNCTOR_colon2)) {
return NULL; return NULL;
} }
@ -927,18 +924,18 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
#else #else
if (PyLong_Check(ip)) { if (PyLong_Check(ip)) {
PyObject *o = PySequence_GetItem(v, PyLong_AsLong(ip)); PyObject *o = PySequence_GetItem(v, PyLong_AsLong(ip));
if (o == NULL) if (o == NULL)
o = Py_None; o = Py_None;
if (CHECKNULL(t,o) == NULL) if (CHECKNULL(t, o) == NULL)
return NULL; return NULL;
Py_INCREF(o); Py_INCREF(o);
return o; return o;
} }
#endif #endif
} else { } else {
o = PyObject_GetItem(v, ip); o = PyObject_GetItem(v, ip);
if (o == NULL) if (o == NULL)
o = Py_None; o = Py_None;
Py_INCREF(o); Py_INCREF(o);
return o; return o;
} }
@ -947,14 +944,14 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
if (fun == FUNCTOR_dollar1) { if (fun == FUNCTOR_dollar1) {
char *s = NULL; char *s = NULL;
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
AOK ( PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
AOK ( PL_get_atom_chars(targ, &s), NULL ); AOK(PL_get_atom_chars(targ, &s), NULL);
/* return __main__,s */ /* return __main__,s */
PyObject *o = PyObject_GetAttrString(py_Main, s); PyObject *o = PyObject_GetAttrString(py_Main, s);
return o; return o;
} }
if (fun == FUNCTOR_brackets1) { if (fun == FUNCTOR_brackets1) {
AOK ( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
return term_to_python(t, true, NULL); return term_to_python(t, true, NULL);
} }
if (fun == FUNCTOR_complex2) { if (fun == FUNCTOR_complex2) {
@ -962,9 +959,9 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
PyObject *lhs, *rhs; PyObject *lhs, *rhs;
double d1, d2; double d1, d2;
AOK ( PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
lhs = term_to_python(targ, true, NULL); lhs = term_to_python(targ, true, NULL);
AOK ( PyNumber_Check(lhs), NULL ); AOK(PyNumber_Check(lhs), NULL);
if (PyFloat_Check(lhs)) { if (PyFloat_Check(lhs)) {
d1 = PyFloat_AsDouble(lhs); d1 = PyFloat_AsDouble(lhs);
} else if (PyLong_Check(lhs)) { } else if (PyLong_Check(lhs)) {
@ -976,9 +973,9 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
} else { } else {
return NULL; return NULL;
} }
AOK (PL_get_arg(2, t, targ), NULL ); AOK(PL_get_arg(2, t, targ), NULL);
rhs = term_to_python(targ, true, NULL); rhs = term_to_python(targ, true, NULL);
AOK (PyNumber_Check(rhs), NULL); AOK(PyNumber_Check(rhs), NULL);
if (PyFloat_Check(rhs)) { if (PyFloat_Check(rhs)) {
d2 = PyFloat_AsDouble(rhs); d2 = PyFloat_AsDouble(rhs);
} else if (PyLong_Check(rhs)) { } else if (PyLong_Check(rhs)) {
@ -997,21 +994,21 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
term_t targ = PL_new_term_ref(), taux = PL_new_term_ref(); term_t targ = PL_new_term_ref(), taux = PL_new_term_ref();
PyObject *dict; PyObject *dict;
AOK ( PL_get_arg(1, t, t), NULL ); AOK(PL_get_arg(1, t, t), NULL);
if (!(dict = PyDict_New())) if (!(dict = PyDict_New()))
return NULL; return NULL;
DebugPrintf("Dict %p\n", dict); DebugPrintf("Dict %p\n", dict);
while (PL_is_functor(t, FUNCTOR_comma2)) { while (PL_is_functor(t, FUNCTOR_comma2)) {
AOK ( PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
AOK ( PL_is_functor(targ, FUNCTOR_colon2), NULL); AOK(PL_is_functor(targ, FUNCTOR_colon2), NULL);
AOK( copy_to_dictionary(dict, targ, taux, true), NULL); AOK(copy_to_dictionary(dict, targ, taux, true), NULL);
AOK( PL_get_arg(2, t, t) , NULL ); AOK(PL_get_arg(2, t, t), NULL);
} }
if (PL_is_functor(t, FUNCTOR_colon2)) { if (PL_is_functor(t, FUNCTOR_colon2)) {
AOK ( copy_to_dictionary(dict, t, taux, true), NULL); AOK(copy_to_dictionary(dict, t, taux, true), NULL);
} }
return dict; return dict;
} }
@ -1025,17 +1022,17 @@ PyObject *compound_to_pytree(term_t t, PyObject *context) {
int arity; int arity;
o = find_obj(context, t, false); o = find_obj(context, t, false);
AOK( PL_get_name_arity(t, &name, &arity), NULL ); AOK(PL_get_name_arity(t, &name, &arity), NULL);
if (arity == 0) if (arity == 0)
return term_to_python(t, false, o); return term_to_python(t, false, o);
AOK( PL_get_functor(t, &fun), NULL); AOK(PL_get_functor(t, &fun), NULL);
if ((no = compound_to_data(t, o, fun, false)) != o && no) { if ((no = compound_to_data(t, o, fun, false)) != o && no) {
return no; return no;
} }
if (!arity) { if (!arity) {
char *s = NULL; char *s = NULL;
AOK (!PL_get_atom_chars(t, &s), NULL ); AOK(!PL_get_atom_chars(t, &s), NULL);
// this should never happen // this should never happen
return term_to_python(t, false, o); return term_to_python(t, false, o);
} else { } else {
@ -1050,12 +1047,12 @@ PyObject *compound_to_pytree(term_t t, PyObject *context) {
tleft = PL_new_term_ref(); tleft = PL_new_term_ref();
for (i = 0; i < arity; i++) { for (i = 0; i < arity; i++) {
PyObject *pArg; PyObject *pArg;
AOK (PL_get_arg(i + 1, t, tleft), NULL ); AOK(PL_get_arg(i + 1, t, tleft), NULL);
pArg = term_to_python(tleft, false, NULL); pArg = term_to_python(tleft, false, NULL);
if (pArg) { if (pArg) {
/* pArg reference stolen here: */ /* pArg reference stolen here: */
PyTuple_SET_ITEM(out, i, pArg); PyTuple_SET_ITEM(out, i, pArg);
Py_INCREF(pArg); Py_INCREF(pArg);
} }
} }
if (CHECKNULL(t, out) == NULL) { if (CHECKNULL(t, out) == NULL) {
@ -1064,11 +1061,11 @@ PyObject *compound_to_pytree(term_t t, PyObject *context) {
} }
PyObject *c = lookupPySymbol(s, o, NULL); PyObject *c = lookupPySymbol(s, o, NULL);
if ( c && PyCallable_Check(c)) { if (c && PyCallable_Check(c)) {
PyObject *n = PyTuple_New(arity); PyObject *n = PyTuple_New(arity);
PyTuple_SET_ITEM(n, 0, c); PyTuple_SET_ITEM(n, 0, c);
PyTuple_SET_ITEM(n, 1, out); PyTuple_SET_ITEM(n, 1, out);
return n; return n;
} }
return term_to_nametuple(s, arity, out); return term_to_nametuple(s, arity, out);
} }
@ -1081,7 +1078,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
functor_t fun; functor_t fun;
o = find_obj(context, t, true); o = find_obj(context, t, true);
AOK (PL_get_name_arity(t, &name, &arity), NULL ); AOK(PL_get_name_arity(t, &name, &arity), NULL);
if (arity == 0) if (arity == 0)
return term_to_python(t, true, o); return term_to_python(t, true, o);
if (!PL_get_functor(t, &fun)) if (!PL_get_functor(t, &fun))
@ -1117,7 +1114,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
PyObject *ptr; PyObject *ptr;
AOK (PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
ptr = term_to_python(targ, true, NULL); ptr = term_to_python(targ, true, NULL);
return PyLong_FromLong(PyObject_Length(ptr)); return PyLong_FromLong(PyObject_Length(ptr));
} }
@ -1125,7 +1122,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
PyObject *ptr; PyObject *ptr;
AOK ( PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
ptr = term_to_python(targ, true, NULL); ptr = term_to_python(targ, true, NULL);
return PyObject_Dir(ptr); return PyObject_Dir(ptr);
@ -1138,7 +1135,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
if (!PL_get_arg(1, t, targ)) if (!PL_get_arg(1, t, targ))
return NULL; return NULL;
lhs = term_to_python(targ, true, NULL); lhs = term_to_python(targ, true, NULL);
AOK( PL_get_arg(2, t, targ), NULL ); AOK(PL_get_arg(2, t, targ), NULL);
rhs = term_to_python(targ, true, NULL); rhs = term_to_python(targ, true, NULL);
if (PySequence_Check(lhs) && PySequence_Check(rhs)) { if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
return PySequence_Concat(lhs, rhs); return PySequence_Concat(lhs, rhs);
@ -1167,12 +1164,12 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs; PyObject *lhs, *rhs;
AOK (PL_get_arg(1, t, targ), NULL ); AOK(PL_get_arg(1, t, targ), NULL);
(lhs = term_to_python(targ, true, NULL)); (lhs = term_to_python(targ, true, NULL));
CHECKNULL( targ, lhs); CHECKNULL(targ, lhs);
AOK (PL_get_arg(2, t, targ), NULL ); AOK(PL_get_arg(2, t, targ), NULL);
(rhs = term_to_python(targ, true, NULL)); (rhs = term_to_python(targ, true, NULL));
CHECKNULL( targ, rhs); CHECKNULL(targ, rhs);
if (PySequence_Check(lhs) && ( if (PySequence_Check(lhs) && (
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
PyInt_Check(rhs) || PyInt_Check(rhs) ||
@ -1188,9 +1185,9 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
char *s = NULL; char *s = NULL;
PyObject *pValue; PyObject *pValue;
AOK (PL_get_atom_chars(t, &s), NULL ); AOK(PL_get_atom_chars(t, &s), NULL);
pValue = PyObject_GetAttrString(o, s); pValue = PyObject_GetAttrString(o, s);
if (CHECKNULL(t,pValue) == NULL) { if (CHECKNULL(t, pValue) == NULL) {
PyErr_Print(); PyErr_Print();
return NULL; return NULL;
} }
@ -1198,7 +1195,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
} else { } else {
char *s = PL_atom_chars(name); char *s = PL_atom_chars(name);
o = lookupPySymbol(s, o, NULL); o = lookupPySymbol(s, o, NULL);
if (CHECKNULL(t,o) == NULL) { if (CHECKNULL(t, o) == NULL) {
PyErr_Print(); PyErr_Print();
return NULL; return NULL;
} }
@ -1208,30 +1205,30 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
term_t tleft = PL_new_term_ref(); term_t tleft = PL_new_term_ref();
for (i = 0; i < arity; i++) { for (i = 0; i < arity; i++) {
PyObject *pArg; PyObject *pArg;
AOK (PL_get_arg(i + 1, t, tleft), NULL ); AOK(PL_get_arg(i + 1, t, tleft), NULL);
/* ignore (_) */ /* ignore (_) */
if (i == 0 && PL_is_variable(tleft)) { if (i == 0 && PL_is_variable(tleft)) {
pArg = Py_None; pArg = Py_None;
} else { } else {
pArg = term_to_python(tleft, true, NULL); pArg = term_to_python(tleft, true, NULL);
PyObject_Print(pArg,fdopen(2,"w"),0); // PyObject_Print(pArg,fdopen(2,"w"),0);
if (pArg == NULL) { if (pArg == NULL) {
pArg = Py_None; pArg = Py_None;
} }
/* pArg reference stolen here: */ /* pArg reference stolen here: */
Py_INCREF(pArg); Py_INCREF(pArg);
} }
PyTuple_SetItem(pArgs, i, pArg); PyTuple_SetItem(pArgs, i, pArg);
} }
if ( !PyCallable_Check(o)) { if (!PyCallable_Check(o)) {
return term_to_nametuple(s, arity, pArgs); return term_to_nametuple(s, arity, pArgs);
} }
PyObject *rc; PyObject *rc;
//PyObject_Print(pArgs, stderr, 0); // PyObject_Print(pArgs, stderr, 0);
//PyObject_Print(o, stderr, 0); // PyObject_Print(o, stderr, 0);
CHECK_CALL(rc ,t, PyObject_CallObject(o, pArgs)); CHECK_CALL(rc, t, PyObject_CallObject(o, pArgs));
Py_DECREF(pArgs); Py_DECREF(pArgs);
Py_DECREF(o); Py_DECREF(o);
DebugPrintf("CallObject %p\n", rc); DebugPrintf("CallObject %p\n", rc);

View File

@ -638,7 +638,7 @@ static YAP_Int p_python_threaded(void) {
static PyGILState_STATE gstate; static PyGILState_STATE gstate;
term_t python_acquire_GIL(void) { term_t python_acquire_GIL(void) {
term_t curSlot = 1; //PL_new_term_ref(); term_t curSlot = PL_new_term_ref();
if (!_threaded) if (!_threaded)
pyErrorAndReturn(curSlot, false); pyErrorAndReturn(curSlot, false);
// extern int Yap_do_low_level_trace; // extern int Yap_do_low_level_trace;
@ -652,7 +652,7 @@ term_t python_acquire_GIL(void) {
bool python_release_GIL(term_t curBlock) { bool python_release_GIL(term_t curBlock) {
PyErr_Clear(); PyErr_Clear();
// PL_reset_term_refs(curBlock); PL_reset_term_refs(curBlock);
if (_threaded) { if (_threaded) {
PyGILState_Release(gstate); PyGILState_Release(gstate);
} }