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];
if (!Yap_findFile( inpf, YAP_STARTUP, YapLibDir, fname, true, YAP_SAVED_STATE, true, true))
return false;
return FAIL_RESTORE;
if (fname[0] &&
(mode = try_open(fname,Astate,ATrail,AStack,AHeap,streamp)) != FAIL_RESTORE) {
return mode;

View File

@ -23,7 +23,7 @@
#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);
@ -39,7 +39,6 @@ INLINE_ONLY EXTERN inline Term Deref(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) {

View File

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

View File

@ -4,36 +4,31 @@
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];
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;
}
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, file, line, exp);
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,
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;
if (eval)
{
if (eval) {
o = PythonLookup(s, p0);
/* if (!o)
return o;
*/
}
else
{
} else {
o = PythonLookupSpecial(s);
}
if (o)
{
if (o) {
Py_INCREF(o);
return CHECKNULL(YAP_MkStringTerm(s), o);
} else {
@ -50,8 +45,7 @@ static PyObject *s_to_python( const char *s, bool eval, PyObject *p0)
*
* @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;
while ((child = strchr(s, '.')) != NULL) {
size_t len = child - s;
@ -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
*/
PyObject *term_to_python(term_t t, bool eval, PyObject *o)
{
PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
// o≈
YAP_Term yt = YAP_GetFromSlot(t);
// Yap_DebugPlWriteln(yt);
switch (PL_term_type(t))
{
case PL_VARIABLE:
{
if (t == 0)
{
switch (PL_term_type(t)) {
case PL_VARIABLE: {
if (t == 0) {
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, yt, "in term_to_python");
}
PyObject *out = PyTuple_New(1);
PyTuple_SET_ITEM(out, 0, PyLong_FromLong(t));
return term_to_nametuple("v", 1, out);
};
case PL_ATOM:
{
case PL_ATOM: {
YAP_Atom at = YAP_AtomOfTerm(yt);
const char *s;
s = YAP_AtomName(at);
if (eval)
{
if (eval) {
o = PythonLookup(s, o);
/* if (!o)
return o;
*/
}
else
{
} else {
o = PythonLookupSpecial(s);
}
if (o)
{
if (o) {
Py_INCREF(o);
return CHECKNULL(t, o);
}
}
case PL_STRING:
{
case PL_STRING: {
const char *s = NULL;
if (YAP_IsAtomTerm(yt))
{
if (YAP_IsAtomTerm(yt)) {
s = YAP_AtomName(YAP_AtomOfTerm(yt));
}
else if (YAP_IsStringTerm(yt))
{
} else if (YAP_IsStringTerm(yt)) {
s = YAP_StringOfTerm(yt);
}
else
{
} else {
return CHECKNULL(t, NULL);
}
#if PY_MAJOR_VERSION < 3
if (proper_ascii_string(s))
{
if (proper_ascii_string(s)) {
PyObject *o = PyString_FromStringAndSize(s, strlen(s));
return CHECKNULL(t, o);
}
else
} else
#endif
{
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
return CHECKNULL(t, pobj);
}
}
break;
case PL_INTEGER:
{
} break;
case PL_INTEGER: {
int64_t j;
if (!PL_get_int64_ex(t, &j))
return CHECKNULL(t, NULL);
@ -154,8 +129,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o)
#endif
}
case PL_FLOAT:
{
case PL_FLOAT: {
PyObject *out;
double 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);
return CHECKNULL(t, out);
}
default:
{
default: {
term_t tail = PL_new_term_ref(), arg;
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;
arg = tail;
out = PyList_New(len);
if (!out)
if (!out) {
PL_reset_term_refs(tail);
return CHECKNULL(t, Py_None);
}
for (i = 0; i < len; i++)
{
if (!PL_get_list(t, arg, t))
{
for (i = 0; i < len; i++) {
if (!PL_get_list(t, arg, t)) {
PL_reset_term_refs(tail);
return Py_None;
}
a = term_to_python(arg, eval, o);
if (a )
{
if (PyList_SetItem(out, i, a) < 0)
{
if (a) {
if (PyList_SetItem(out, i, a) < 0) {
PL_reset_term_refs(tail);
return Py_None;
}
}
}
PL_reset_term_refs(tail);
return CHECKNULL(t, out);
}
else
{
} else {
functor_t fun;
PyObject *rc;
if (!PL_get_functor(t, &fun))
{
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
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);
}
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)
return NULL;
term_t yt = YAP_InitSlot(t);
@ -227,13 +194,11 @@ PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *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
// am\n");
YAP_Term yt = YAP_GetFromSlot(t);
if (YAP_IsVarTerm(yt))
{
if (YAP_IsVarTerm(yt)) {
char s[32];
char *o = YAP_WriteBuffer(yt, s, 31, 0);
PyObject *p = PyUnicode_FromString(o);

View File

@ -25,11 +25,14 @@ foreign_t assign_to_symbol(term_t t, PyObject *e) {
foreign_t python_to_term(PyObject *pVal, term_t t) {
bool rc = true;
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) {
//fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" >>***\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);
// fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// >>***\n",stderr);
} else if (PyBool_Check(pVal)) {
rc = rc && PL_unify_bool(t, PyObject_IsTrue(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)) {
rc = rc && PL_unify_float(t, PyFloat_AsDouble(pVal));
} else if (PyComplex_Check(pVal)) {
term_t t1 = PL_new_term_ref(),
t2 = PL_new_term_ref();
term_t 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)) {
@ -113,7 +115,8 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
} else {
rc = false;
}
//fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" ||***\n",stderr);
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// ||***\n",stderr);
}
} else if (PyList_Check(pVal)) {
Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
@ -131,7 +134,6 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
obj = Py_None;
}
rc = rc && python_to_term(obj, to);
}
rc = rc && PL_unify_nil(t);
}
@ -168,7 +170,8 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
rc = false;
}
if (!PL_unify(ti, tint)) {
rc = false; }
rc = false;
}
ti = tnew;
PL_reset_term_refs(tkey);
}
@ -183,9 +186,10 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
X_API YAP_Term pythonToYAP(PyObject *pVal) {
term_t t = PL_new_term_ref();
if (pVal == NULL ||
!python_to_term(pVal, t))
if (pVal == NULL || !python_to_term(pVal, t)) {
PL_reset_term_refs(t);
return 0;
}
YAP_Term tt = YAP_GetFromSlot(t);
PL_reset_term_refs(t);
Py_DECREF(pVal);

View File

@ -105,7 +105,6 @@ PyObject *find_obj(PyObject *ob, term_t l, bool eval) {
return Py_None;
}
yt = YAP_TailOfTerm(yt);
}
YAP_PutInSlot(l, yt);
return ob;
@ -456,8 +455,8 @@ static PyObject *bip_sum(term_t t) {
}
#if PY_MAJOR_VERSION < 3
if (PyInt_CheckExact(item)) {
764PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
f_result += (double)PyInt_AS_LONG(item);
764PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter);
return 0)f_result += (double)PyInt_AS_LONG(item);
PyFPE_END_PROTECT(f_result) Py_DECREF(item);
continue;
}
@ -561,15 +560,14 @@ static long get_len_of_range(long lo, long hi, long step) {
#if PY_MAJOR_VERSION >= 3
static PyStructSequence_Field pnull[] = {
{"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL},
{"A5", NULL}, {"A6", NULL}, {"A7", NULL}, {"A8", NULL},
{"A9", NULL}, {"A9", NULL}, {"A10", NULL}, {"A11", NULL},
{"A12", NULL}, {"A13", NULL}, {"A14", NULL}, {"A15", NULL},
{"A16", NULL}, {"A17", NULL}, {"A18", NULL}, {"A19", NULL},
{"A19", NULL}, {"A20", NULL}, {"A21", NULL}, {"A22", NULL},
{"A23", NULL}, {"A24", NULL}, {"A25", NULL}, {"A26", NULL},
{"A27", NULL}, {"A28", NULL}, {"A29", NULL}, {"A29", NULL},
{"A30", NULL}, {"A31", NULL}, {"A32", NULL}, {NULL,NULL}};
{"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL}, {"A5", NULL},
{"A6", NULL}, {"A7", NULL}, {"A8", NULL}, {"A9", NULL}, {"A9", NULL},
{"A10", NULL}, {"A11", NULL}, {"A12", NULL}, {"A13", NULL}, {"A14", NULL},
{"A15", NULL}, {"A16", NULL}, {"A17", NULL}, {"A18", NULL}, {"A19", NULL},
{"A19", NULL}, {"A20", NULL}, {"A21", NULL}, {"A22", NULL}, {"A23", NULL},
{"A24", NULL}, {"A25", NULL}, {"A26", NULL}, {"A27", NULL}, {"A28", NULL},
{"A29", NULL}, {"A29", NULL}, {"A30", NULL}, {"A31", NULL}, {"A32", NULL},
{NULL, NULL}};
static PyObject *structseq_str(PyObject *iobj) {
@ -698,7 +696,6 @@ static PyObject *structseq_repr(PyObject *iobj) {
}
#endif
PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
PyObject *o;
#if PY_MAJOR_VERSION >= 3
@ -784,7 +781,8 @@ static PyObject *bip_range(term_t t) {
else
bign = get_len_of_range(ihigh, ilow, -istep);
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);
if (v == NULL)
@ -842,7 +840,6 @@ static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
// Py_DECREF(rhs);
}
PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
atom_t name;
int len;
@ -1214,7 +1211,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
pArg = Py_None;
} else {
pArg = term_to_python(tleft, true, NULL);
PyObject_Print(pArg,fdopen(2,"w"),0);
// PyObject_Print(pArg,fdopen(2,"w"),0);
if (pArg == NULL) {
pArg = Py_None;
}

View File

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