This commit is contained in:
Vitor Santos Costa
2018-07-27 11:11:04 +01:00
parent 436f1c205a
commit bf712034a9
18 changed files with 333 additions and 249 deletions

View File

@@ -14,7 +14,22 @@ PyObject *YE(term_t t, int line, const char *file, const char *code) {
return NULL;
}
void YEM(const char *exp, int line, const char *file, const char *code) {
PyObject *YEC(PyObject *f, PyObject *a, PyObject *d, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code,
file, line);
if (f)
PyObject_Print(f, stderr, 0);
else
fprintf(stderr,"<null>");
if (a)
PyObject_Print(a, stderr, 0);
if (a)
PyObject_Print(a, stderr, 0);
fprintf(stderr,"\n");
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);
}
@@ -188,7 +203,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
Term t0 = Yap_GetFromHandle(t);
Term *tail;
size_t len, i;
if ((len = Yap_SkipList(&t0, &tail)) >= 0 && *tail == TermNil) {
if ((len = Yap_SkipList(&t0, &tail)) > 0 && *tail == TermNil) {
PyObject *out, *a;
out = PyList_New(len);
@@ -205,10 +220,10 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
}
return out;
} else {
PyObject *no = find_obj(o, t, false);
PyObject *no = find_term_obj(o, &t0, false);
if (no == o)
return NULL;
return term_to_python(t, eval, no, cvt);
return yap_to_python(t0, eval, no, cvt);
}
} else {
{

View File

@@ -5,7 +5,6 @@
#ifndef PY4YAP_H
#define PY4YAP_H 1
#define PASS_REGS
#define USES_REGSg
@@ -42,7 +41,7 @@
PyObject *find_obj(PyObject *ob, term_t lhs, bool eval);
#if DEBUG_MEMORY||1
#if DEBUG_MEMORY || 1
#define DebugPrintf(s, op) fprintf(stderr, "%s:%d: " s, __FILE__, __LINE__, op)
#else
#define DebugPrintf(s, op)
@@ -63,15 +62,14 @@ extern X_API PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o,
bool cvt);
extern X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0);
typedef YAP_Arity arity_t;
extern bool init_python_vfs(void);
extern bool init_python_vfs(void);
extern atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
extern atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
ATOM_comma, ATOM_builtin, ATOM_V, ATOM_A, ATOM_self, ATOM_nil,
ATOM_brackets, ATOM_curly_brackets;
extern functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1, FUNCTOR_as2,
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
extern functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
FUNCTOR_as2, FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
FUNCTOR_float1, FUNCTOR_int1, FUNCTOR_iter1, FUNCTOR_iter2, FUNCTOR_long1,
FUNCTOR_len1, FUNCTOR_curly1, FUNCTOR_ord1, FUNCTOR_range1, FUNCTOR_range2,
FUNCTOR_range3, FUNCTOR_sum1, FUNCTOR_pointer1, FUNCTOR_complex2,
@@ -147,11 +145,11 @@ static inline PyObject *atom_to_python_string(term_t t) {
}
}
#define CHECK_CALL(rc, t, call) \
#define CHECK_CALL(ys, pArgs, pyDict) \
PyErr_Clear(); \
rc = call; \
rc = PyObject_Call(ys, pArgs, pyDict); \
if (rc == NULL || PyErr_Occurred()) { \
YE(t, __LINE__, __FILE__, __FUNCTION__); \
YEC(ys, pArgs, pyDict, __LINE__, __FILE__, __FUNCTION__); \
PyErr_Print(); \
PyErr_Clear(); \
}
@@ -164,7 +162,8 @@ static inline PyObject *atom_to_python_string(term_t t) {
YEM(#rc, __LINE__, __FILE__, __FUNCTION__); \
}
extern PyObject *YE(term_t t, int line, const char *file, const char *code);
extern PyObject *YE(term_t , int line, const char *file, const char *code);
extern PyObject *YEC(PyObject *c,PyObject *a ,PyObject *d , int line, const char *file, const char *code);
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);
@@ -209,6 +208,7 @@ X_API extern bool init_python(void);
X_API extern bool loadt_python(void);
X_API extern bool do_init_python(void);
extern PyObject *find_term_obj(PyObject *ob, YAP_Term *yt, bool eval);
extern PyObject PyInit_yap(void);
extern PyObject *PythonLookup(const char *s, PyObject *o);

View File

@@ -50,6 +50,11 @@ PyObject *PythonLookupSpecial(const char *s) {
return NULL;
}
static PyObject *builtin(const char *sp) {
PyObject *py_Builtin = PyEval_GetBuiltins();
return PyDict_GetItemString(py_Builtin, sp);
}
PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
PyObject *out = NULL;
if (!sp)
@@ -113,6 +118,22 @@ find_obj(PyObject *ob, term_t l, bool eval) {
return ob;
}
PyObject *find_term_obj(PyObject *ob, YAP_Term *yt, bool eval) {
YAP_Term hd;
py_Context = NULL;
// Yap_DebugPlWriteln(yt);
while (YAP_IsPairTerm(*yt)) {
hd = YAP_HeadOfTerm(*yt);
*yt = YAP_TailOfTerm(*yt);
ob = yap_to_python(hd, true, ob, false);
if (!ob) {
return Py_None;
}
}
return ob;
}
/**
* Python abs
*
@@ -596,7 +617,7 @@ static PyObject *structseq_str(PyObject *iobj) {
for (i = 0; i < ((PyStructSequence *)obj)->ob_base.ob_size; i++) {
PyObject *val, *repr;
char *crepr;
const char *crepr;
val = PyStructSequence_GET_ITEM(obj, i);
repr = PyObject_Str(val);
@@ -659,7 +680,7 @@ static PyObject *structseq_repr(PyObject *iobj) {
for (i = 0; i < ((PyStructSequence *)obj)->ob_base.ob_size; i++) {
PyObject *val, *repr;
char *crepr;
const char *crepr;
val = PyStructSequence_GET_ITEM(obj, i);
repr = PyObject_Repr(val);
@@ -710,18 +731,29 @@ static bool legal_symbol(const char *s) {
}
PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
if (legal_symbol(s)) {
PyTypeObject *typp;
PyObject *key = PyUnicode_FromString(s), *d;
if (Py_f2p && (d = PyList_GetItem(Py_f2p, arity)) &&
PyDict_Contains(d, key)) {
typp = (PyTypeObject *)PyDict_GetItem(d, key);
Py_INCREF(typp);
PyTypeObject *typp;
PyObject *key = PyUnicode_FromString(s), *d;
if (!legal_symbol(s)) {
if (!Py_f2p) {
PyObject *o1;
o1 = PyTuple_New(2);
PyTuple_SET_ITEM(o1, 0, PyUnicode_FromString(s));
PyTuple_SET_ITEM(o1, 1, tuple);
return o1;
}
size_t l = 0;
if ((l = PyList_Size(Py_f2p)) < arity) {
for (; l < arity; l++) {
PyList_Append(Py_f2p, PyDict_New());
}
}
if ((d = PyList_GetItem(Py_f2p, arity - 1)) && PyDict_Contains(d, key)) {
typp = (PyTypeObject *)d;
} else {
typp = calloc(sizeof(PyTypeObject), 1);
PyStructSequence_Desc *desc = calloc(sizeof(PyStructSequence_Desc), 1);
desc->name = PyMem_Malloc(strlen(s) + 1);
strcpy(desc->name, s);
desc->doc = "YAPTerm";
desc->fields = pnull;
desc->n_in_sequence = arity;
@@ -735,9 +767,7 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
// don't do this: we cannot add a type as an atribute.
// PyModule_AddGObject(py_Main, s, (PyObject *)typp);
if (d && !PyDict_Contains(d, key))
PyDict_SetItem(d, key, (PyObject *)typp);
Py_INCREF(key);
Py_INCREF(typp);
PyDict_SetItem(d, key, (PyObject*)typp);
}
PyObject *o = PyStructSequence_New(typp);
Py_INCREF(typp);
@@ -751,14 +781,10 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
}
//((PyStructSequence *)o)->ob_base.ob_size = arity;
// PyObject_Print(o,stderr,0);fputc('\n',stderr);
Py_INCREF(o);
return o;
} else {
PyObject *o1;
o1 = PyTuple_New(2);
PyTuple_SET_ITEM(o1, 0, PyUnicode_FromString(s));
PyTuple_SET_ITEM(o1, 1, tuple);
return o1;
}
return NULL;
}
static PyObject *bip_range(term_t t) {
@@ -959,11 +985,10 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
return PySequence_Concat(lhs, rhs);
}
if (!PyNumber_Check(lhs))
return NULL;
if (!PyNumber_Check(rhs))
return NULL;
return PyNumber_Add(lhs, rhs);
if (PyNumber_Check(lhs) && PyNumber_Check(rhs))
return PyNumber_Add(lhs, rhs);
PyObject_Print(builtin("+"), stderr, 0);
return PyObject_CallFunctionObjArgs(builtin("+"), lhs, rhs, NULL);
} else if (fun == FUNCTOR_sub2) {
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
@@ -971,19 +996,18 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
if (!PL_get_arg(1, t, targ))
return NULL;
lhs = term_to_python(targ, true, NULL, true);
if (!PyNumber_Check(lhs))
return NULL;
if (!PL_get_arg(2, t, targ))
return NULL;
rhs = term_to_python(targ, true, NULL, true);
if (!PyNumber_Check(rhs))
return NULL;
return PyNumber_Subtract(lhs, rhs);
if (PyNumber_Check(rhs) && PyNumber_Check(lhs))
return PyNumber_Subtract(lhs, rhs);
return PyObject_CallFunctionObjArgs(builtin("-"), lhs, rhs, NULL);
} else if (fun == FUNCTOR_mul2) {
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
AOK(PL_get_arg(1, t, targ), NULL);
/* YAP_DebugPlWriteln(YAP_GetTermSlot(arg)); */
(lhs = term_to_python(targ, true, NULL, true));
CHECKNULL(targ, lhs);
AOK(PL_get_arg(2, t, targ), NULL);
@@ -996,9 +1020,9 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
PyLong_Check(rhs))) {
return PySequence_Repeat(lhs, get_p_int(rhs, 0));
}
if (!PyNumber_Check(lhs) + !PyNumber_Check(rhs))
return NULL;
return PyNumber_Multiply(lhs, rhs);
if (PyNumber_Check(lhs) && PyNumber_Check(rhs))
return PyNumber_Multiply(lhs, rhs);
return PyObject_CallFunctionObjArgs(builtin("*"), lhs, rhs, NULL);
}
if (!arity) {
char *s = NULL;
@@ -1006,7 +1030,6 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
AOK(PL_get_atom_chars(t, &s), NULL);
PyObject_Print(o, stderr, 0);
pValue = PyObject_GetAttrString(o, s);
PyObject_Print(pValue, stderr, 0);
if (CHECKNULL(t, pValue) == NULL) {
PyErr_Print();
return NULL;
@@ -1014,12 +1037,32 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
return pValue;
} else {
char *s = PL_atom_chars(name);
if (!strcmp(s,"t")) {
YAP_Term tt = YAP_GetFromSlot(t), tleft;
int i;
PyObject *rc = PyTuple_New(arity);
PyObject *pArg;
for (i=0;i<arity;i++) {
AOK((tleft = YAP_ArgOfTerm(i+1, tt)), NULL);
pArg = yap_to_python(tleft, true, NULL, true);
if (pArg == NULL) {
pArg = Py_None;
}
/* pArg reference stolen here: */
Py_INCREF(pArg);
PyTuple_SetItem(rc, i, pArg);
}
return rc;
}
PyObject *ys = lookupPySymbol(s, o, NULL), *pArgs;
int i;
term_t tleft = PL_new_term_ref();
bool indict = true;
PyObject *pyDict = PyDict_New();
pArgs = Py_None;
for (i = arity; i > 0; i--) {
PyObject *pArg;
AOK(PL_get_arg(i, t, tleft), NULL);
@@ -1037,8 +1080,8 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
pArgs = PyTuple_New(i);
}
}
fprintf(stderr, "Tuple %p: %s\n", pyDict,
PyUnicode_AsUTF8(PyObject_Str(pyDict)));
// fprintf(stderr, "Tuple %p: %s\n", pyDict,
// PyUnicode_AsUTF8(PyObject_Str(pyDict)));
if (!indict) {
if (PL_is_variable(tleft)) {
pArg = Py_None;
@@ -1056,23 +1099,23 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
}
}
if (indict) {
if (pArgs == Py_None) {
pArgs = PyTuple_New(0);
}
PyObject *rc;
if (ys && PyCallable_Check(ys)) {
PyObject_Print(ys, stderr, 0);
PyObject_Print(pArgs, stderr, 0);
PyObject_Print(pyDict, stderr, 0);
// PyObject_Print(ys, stderr, 0);
// PyObject_Print(pArgs, stderr, 0);
// PyObject_Print(pyDict, stderr, 0);
// PyObject_Print(pArgs, stderr, 0);
// PyObject_Print(o, stderr, 0);
CHECK_CALL(rc, t, PyObject_Call(ys, pArgs, pyDict));
CHECK_CALL(ys, pArgs, pyDict);
Py_DECREF(pArgs);
Py_DECREF(ys);
PyObject_Print(rc, stderr, 0);
DebugPrintf("CallObject %p\n", rc);
// PyObject_Print(rc, stderr, 0);
// DebugPrintf("CallObject %p\n", rc);
} else {
rc = term_to_nametuple(s, arity, pArgs);
}

View File

@@ -127,28 +127,26 @@ static bool py_close(int sno) {
return true;
}
static bool getLine(StreamDesc *rl_iostream, int sno) {
char *myrl_line = NULL;
// term_t ctk = python_acquire_GIL();
PyObject *readl =
PythonLookup("input", Py_None);
PyObject_Print(readl, stderr, 0);fprintf(stderr," input\n");
PyObject *user_inp = PyObject_CallFunction(readl,NULL);
PyObject_Print(user_inp, stderr, 0);fprintf(stderr," input\n");
myrl_line = PyUnicode_AsUTF8(user_inp);
//PyObject_Print(myrl_line, stderr, 0);fprintf(stderr," myrl_line\n");
// python_release_GIL(ctk);
static bool pygetLine(StreamDesc *rl_iostream, int sno) {
// term_t ctk = python_acquire_GIL();
const char *myrl_line;
StreamDesc *s = YAP_GetStreamFromId(sno);
//term_t tg = python_acquire_GIL();
PyObject *readl = PyObject_GetAttrString(s->u.private_data, "readline");
PyObject *user_inp = PyObject_CallFunction(readl, NULL);
myrl_line = PyUnicode_AsUTF8(user_inp);
if (myrl_line == NULL)
return NULL;
PyObject *err;
if ((err = PyErr_Occurred())) {
if (PyErr_GivenExceptionMatches(err, PyExc_EOFError))
return NULL;
PyErr_SetString(err, "Error in getLine\n");
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
size_t size = strlen(myrl_line) + 1;
rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf =
(const unsigned char *)malloc(size);
memmove((void *)rl_iostream->u.irl.buf, myrl_line, size);
rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf = myrl_line;
return true;
}
@@ -158,16 +156,14 @@ static int py_getc(int sno) {
bool fetch = (s->u.irl.buf == NULL);
if (fetch) {
if (!getLine(s, sno)) {
if (!pygetLine(s, sno)) {
return EOF;
}
}
const unsigned char *ttyptr = s->u.irl.ptr++, *myrl_line = s->u.irl.buf;
const unsigned char *ttyptr = s->u.irl.ptr++;
ch = *ttyptr;
if (ch == '\0') {
ch = '\n';
free((void *)myrl_line);
s->u.irl.ptr = s->u.irl.buf = NULL;
ch = 10;
}
return ch;
}
@@ -192,7 +188,7 @@ static int py_peek(int sno) {
}
return ch;
}
if (getLine(s, sno)) {
if (pygetLine(s, sno)) {
ch = s->u.irl.ptr[0];
if (ch == '\0') {
ch = '\n';

View File

@@ -7,12 +7,14 @@ PyObject *py_Main;
void pyErrorHandler__(int line, const char *file, const char *code) {
// this code is called if a Python error is found.
//int lvl = push_text_stack();
// int lvl = push_text_stack();
PyObject *type, *val;
// PyErr_Fetch(&type, &val, NULL);
// PyErr_Print();
// Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python Error %s: %s",PyUnicode_AsUTF8(PyObject_Str(type)), PyUnicode_AsUTF8(PyObject_Str(val)));
};
// Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python
// Error %s: %s",PyUnicode_AsUTF8(PyObject_Str(type)),
// PyUnicode_AsUTF8(PyObject_Str(val)));
};
static foreign_t python_len(term_t tobj, term_t tf) {
Py_ssize_t len;
@@ -30,7 +32,6 @@ static foreign_t python_clear_errors(void) {
return true;
}
static foreign_t python_dir(term_t tobj, term_t tf) {
PyObject *dir;
PyObject *o;
@@ -115,7 +116,9 @@ 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); }
@@ -143,14 +146,18 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
pF = term_to_python(tin, true, NULL, true);
PyErr_Clear();
if (pF == NULL) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
}
if (PL_is_atom(targs)) {
pArgs = NULL;
} else {
if (!PL_get_name_arity(targs, &aname, &arity)) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
}
if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) {
/* ignore (_) */
@@ -277,7 +284,7 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
Py_DECREF(pI);
if (pOut == NULL) {
PyErr_Print();
{ pyErrorAndReturn(false); }
{ pyErrorAndReturn(false); }
}
{
foreign_t rc = address_to_term(pOut, out);
@@ -306,10 +313,12 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
{ pyErrorAndReturn(false); }
}
Py_INCREF(pValue);
{ pyErrorAndReturn(python_to_term(pValue, out) ); }
{ pyErrorAndReturn(python_to_term(pValue, out)); }
}
if (!PL_get_name_arity(f, &name, &arity)) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
}
s = PL_atom_chars(name);
if (!s) {
@@ -342,7 +351,9 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
Py_DECREF(pArgs);
Py_DECREF(pF);
if (pValue == NULL) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
}
{ pyErrorAndReturn(python_to_term(pValue, out)); }
}
@@ -354,7 +365,9 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
int arity;
if (!PL_get_name_arity(att, &name, &arity)) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
} else {
PyObject *p;
@@ -363,7 +376,9 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
p = term_to_python(parent, true, NULL, true);
// Exp
if (!PL_get_name_arity(att, &name, &arity)) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
}
s = PL_atom_chars(name);
if (arity == 1 && !strcmp(s, "()")) {
@@ -371,12 +386,16 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
pyErrorAndReturn(false);
}
if (!PL_get_name_arity(att, &name, &arity)) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
}
s = PL_atom_chars(name);
}
if (!s || !p) {
{ pyErrorAndReturn(false); }
{
pyErrorAndReturn(false);
}
} else if ((pF = PyObject_GetAttrString(p, s)) == NULL) {
PyErr_Clear();
{ pyErrorAndReturn(false); }
@@ -548,43 +567,43 @@ static int python_import(term_t mname, term_t mod) {
char s0[MAXPATHLEN], *s = s0;
s[0] = '\0';
const char*sn, *as;
const char *sn, *as = NULL;
Term t = Deref(ARG1), sm;
if (IsApplTerm(t)) {
Functor f = FunctorOfTerm(t);
if (f != Yap_MkFunctor(Yap_LookupAtom("as"),2))
if (f != FunctorAs)
return false;
do_as = true;
sm = ArgOfTerm(2,t);
sm = ArgOfTerm(2, t);
if (IsAtomTerm(sm))
as = RepAtom(AtomOfTerm(sm))->StrOfAE;
else if (IsStringTerm(sm))
as = StringOfTerm(sm);
else
return false;
t = ArgOfTerm(1,t);
t = ArgOfTerm(1, t);
}
while (IsPairTerm(t)) {
Term ti = HeadOfTerm(t);
Term ti = HeadOfTerm(t);
t = TailOfTerm(t);
if (IsAtomTerm(ti))
if (IsAtomTerm(ti))
sn = RepAtom(AtomOfTerm(ti))->StrOfAE;
else if (IsStringTerm(ti))
sn = StringOfTerm(ti);
else
return false;
strcat(s,sn);
strcat(s,".");
strcat(s, sn);
strcat(s, ".");
}
sm = t;
if (IsAtomTerm(sm))
sn = RepAtom(AtomOfTerm(sm))->StrOfAE;
else if (IsStringTerm(sm))
sn = StringOfTerm(sm);
else
return false;
strcat(s,sn);
term_t t0 = python_acquire_GIL();
if (IsAtomTerm(sm))
sn = RepAtom(AtomOfTerm(sm))->StrOfAE;
else if (IsStringTerm(sm))
sn = StringOfTerm(sm);
else
return false;
strcat(s, sn);
term_t t0 = python_acquire_GIL();
#if PY_MAJOR_VERSION < 3
pName = PyString_FromString(s0);
#else
@@ -599,16 +618,17 @@ static int python_import(term_t mname, term_t mod) {
Py_XDECREF(pName);
if (pModule == NULL) {
python_release_GIL(t0);
python_release_GIL(t0);
pyErrorAndReturn(false);
}
{
foreign_t rc = address_to_term(pModule, mod);
if (do_as && PyObject_SetAttrString(py_Main, sn, pModule) <0)
return false;
python_release_GIL(t0);
if (do_as) {
PyObject_SetAttrString(py_Main, as, pModule);
}
python_release_GIL(t0);
pyErrorAndReturn(rc);
}
}
@@ -674,26 +694,25 @@ term_t python_acquire_GIL(void) {
}
bool python_release_GIL(term_t curBlock) {
int gstateix;
gstatei--;
PL_get_integer(curBlock, &gstateix);
PL_reset_term_refs(curBlock);
if (gstatei != gstateix) {
if (gstateix > gstatei) {
fprintf(stderr, "gstateix(%d) > gstatei(%d)\n", gstateix, gstatei);
return false;
} else {
fprintf(stderr, "gstateix(%d) < gstatei(%d)\n", gstateix, gstatei);
return false;
}
int gstateix;
gstatei--;
PL_get_integer(curBlock, &gstateix);
PL_reset_term_refs(curBlock);
if (gstatei != gstateix) {
if (gstateix > gstatei) {
fprintf(stderr, "gstateix(%d) > gstatei(%d)\n", gstateix, gstatei);
return false;
} else {
fprintf(stderr, "gstateix(%d) < gstatei(%d)\n", gstateix, gstatei);
return false;
}
if (_threaded) {
}
if (_threaded) {
PyGILState_Release(gstates[gstatei]);
}
pyErrorAndReturn(true);
}
install_t install_pypreds(void) {
PL_register_foreign("python_builtin_eval", 3, python_builtin_eval, 0);
PL_register_foreign("python_builtin", 1, python_builtin, 0);

View File

@@ -47,8 +47,9 @@ static void add_modules(void) {
if (py_Yapex)
Py_INCREF(py_Yapex);
Py_f2p = PythonLookup("f2p", NULL);
if (Py_f2p)
Py_INCREF(Py_f2p);
if (!Py_f2p)
Py_f2p = PyList_New(0);
Py_INCREF(Py_f2p);
init_python_vfs();
}

View File

@@ -117,7 +117,7 @@ Data types are
user:(:=)/1,
% user:(<-)/1,
% user:(<-)/2,
user:'()'/1, user:'{}'/1, user:dot_qualified_goal/2, user:import_arg/1.
user:'()'/1, user:'{}'/1, user:dot_qualified_goal/1, user:import_arg/1.
import( F ) :- catch( python:python_import(F), _, fail ).

View File

@@ -413,11 +413,23 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/kerne
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/kernel.js
)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/meta.js
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/misc/editors/meta.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/
DEPENDS ${CMAKE_SOURCE_DIR}/misc/editors/meta.js
)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/prolog.js
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/misc/editors/yap.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/prolog.js
DEPENDS ${CMAKE_SOURCE_DIR}/misc/editors/yap.js
)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/mode.js
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/misc/editors/mode.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/
DEPENDS ${CMAKE_SOURCE_DIR}/misc/editors/mode.js
)
foreach(f ${FILES})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${f}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${f} ${CMAKE_CURRENT_BINARY_DIR}/${f}
@@ -431,7 +443,7 @@ endforeach()
add_custom_target(YAP_KERNEL ALL
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} build sdist bdist
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/logo-32x32.png ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/logo-64x64.png ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/kernel.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/prolog.js ${OUTS} YAP4PY
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/logo-32x32.png ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/logo-64x64.png ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/kernel.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/meta.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/prolog.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/mode.js ${OUTS} YAP4PY
)

View File

@@ -92,9 +92,9 @@ blank(Text) :-
close(user_output),
close(user_error).
streams(true) :-
open('/python/input', read, Input, [alias(user_input),bom(false),script(false)]),
open('/python/sys.stdout', append, Output, [alias(user_output)]),
open('/python/sys.stderr', append, Error, [alias(user_error)]).
open('/python/sys.stdin', read, _Input, [alias(user_input),bom(false),script(false)]),
open('/python/sys.stdout', append, _Output, [alias(user_output)]),
open('/python/sys.stderr', append, _Error, [alias(user_error)]).
:- if( current_prolog_flag(apple, true) ).

View File

@@ -514,6 +514,7 @@ class YAPRun:
self.yapeng = JupyterEngine()
global engine
engine = self.yapeng
self.errors = []
self.query = None
self.os = None
self.it = None
@@ -577,7 +578,7 @@ class YAPRun:
self.iterations += 1
if self.port == "exit":
self.os = None
sys.stderr.write('Done, with'+str(self.answers)+'\n')
#sys.stderr.write('Done, with'+str(self.answers)+'\n')
self.result.result = True,self.bindings
return self.result
if stop or howmany == self.iterations:
@@ -658,7 +659,7 @@ class YAPRun:
if store_history:
self.result.execution_count = self.shell.execution_count+1
def error_before_exec(value):
def error_before_exec(self, value):
self.result.error_before_exec = value
self.shell.last_execution_succeeded = False
return self.result
@@ -677,10 +678,10 @@ class YAPRun:
# except SyntaxError:
# preprocessing_exc_tuple = self.shell.syntax_error() # sys.exc_info()
cell = raw_cell # cell has to exist so it can be stored/logged
for i in self.syntaxErrors(raw_cell):
for i in self.errors:
try:
(what,lin,_,text) = i
e = SyntaxError(what, ("<string>", lin, 1, text+'\n'))
(_,lin,pos,text) = i
e = SyntaxError(what, (self.cell_name, lin, pos, text+'\n'))
raise e
except SyntaxError:
self.shell.showsyntaxerror( )
@@ -696,13 +697,13 @@ class YAPRun:
self.showtraceback(preprocessing_exc_tuple)
if store_history:
self.shell.execution_count += 1
return error_before_exec(preprocessing_exc_tuple[2])
return self.error_before_exec(preprocessing_exc_tuple[2])
# Our own compiler remembers the __future__ environment. If we want to
# run code with a separate __future__ environment, use the default
# compiler
# compiler = self.shell.compile if shell_futures else CachingCompiler()
cell_name = str( self.shell.execution_count)
self.cell_name = str( self.shell.execution_count)
if cell[0] == '%':
if cell[1] == '%':
linec = False
@@ -802,7 +803,7 @@ class YAPRun:
its = 0
for ch in n:
if not ch.isdigit():
raise SyntaxError()
raise SyntaxError("expected positive number", (self.cellname,s.strip.lines()+1,s.count('\n'),n))
its = its*10+ (ord(ch) - ord('0'))
stop = False
else: