jupyter
This commit is contained in:
@@ -119,17 +119,17 @@ static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tle
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if (PL_skip_list(plist, targ, &sz) <0 || ! PL_get_nil(targ)) {
|
||||
pyErrorAndReturn( false, false);
|
||||
pyErrorAndReturn( false);
|
||||
}
|
||||
if (!PyList_Check(pyl))
|
||||
{
|
||||
pyErrorAndReturn( false, false);
|
||||
pyErrorAndReturn( false);
|
||||
}
|
||||
if (sz > PyList_GET_SIZE(pyl))
|
||||
pyErrorAndReturn( false, false);
|
||||
pyErrorAndReturn( false);
|
||||
for (i=0; i < sz; i++) {
|
||||
if (!PL_get_list(plist, targ, plist)) {
|
||||
pyErrorAndReturn( false, false);
|
||||
pyErrorAndReturn( false);
|
||||
}
|
||||
PyObject *t = term_to_python(targ, true, NULL, true);
|
||||
PyList_SET_ITEM(pyl, i, t);
|
||||
@@ -139,7 +139,7 @@ static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tle
|
||||
} else {
|
||||
python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL);
|
||||
}
|
||||
pyErrorAndReturn( true, false);
|
||||
pyErrorAndReturn( true);
|
||||
}
|
||||
|
||||
install_t install_pl2pl(void) {
|
||||
|
@@ -207,6 +207,7 @@ 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)) {
|
||||
PL_reset_term_refs(t);
|
||||
|
@@ -168,14 +168,12 @@ extern void pyErrorHandler__(int line, const char *file, const char *code);
|
||||
} \
|
||||
}
|
||||
|
||||
#define pyErrorAndReturn(x, y) \
|
||||
#define pyErrorAndReturn(x) \
|
||||
{ \
|
||||
if (PyErr_Occurred()) { \
|
||||
pyErrorHandler__(__LINE__, __FILE__, __FUNCTION__); \
|
||||
return (x); \
|
||||
} else { \
|
||||
return (x); \
|
||||
} \
|
||||
return (x); \
|
||||
}
|
||||
// #define pyErrorAndReturn( x, y ) return x
|
||||
|
||||
|
@@ -43,7 +43,7 @@ static int py_put(int sno, int ch)
|
||||
}
|
||||
|
||||
VFS_t pystream;
|
||||
static void *py_open(VFS_t *me, int sno, const char *name, const char *io_mode) {
|
||||
static void *py_open(VFS_t *me, const char *name, const char *io_mode, int sno) {
|
||||
#if HAVE_STRCASESTR
|
||||
if (strcasestr(name, "/python/") == name)
|
||||
name += strlen("/python/");
|
||||
@@ -51,29 +51,28 @@ VFS_t pystream;
|
||||
if (strstr(name, "/python/") == name)
|
||||
name += strlen("/python/");
|
||||
#endif
|
||||
PyObject *pystream = string_to_python(name, true, NULL);
|
||||
if (pystream == NULL || pystream == Py_None) {
|
||||
return NULL;
|
||||
}
|
||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||
if (strcmp(name,"sys.output") == 0) {
|
||||
st->name = YAP_LookupAtom(name);
|
||||
if (0&&strcmp(name,"sys.stdout") == 0) {
|
||||
st->user_name = TermOutStream;
|
||||
} else if(strcmp(name,"sys.error") == 0) {
|
||||
} else if(0&&strcmp(name,"sys.stderr") == 0) {
|
||||
st->user_name = TermErrStream;
|
||||
} else {
|
||||
// we assume object is already open, so there is no need to open it.
|
||||
PyObject *pystream = string_to_python(name, true, NULL);
|
||||
if (pystream == Py_None) {
|
||||
return NULL;
|
||||
} else {
|
||||
st->u.private_data = pystream;
|
||||
st->vfs = me;
|
||||
st->name = YAP_LookupAtom(name);
|
||||
st->user_name = YAP_MkAtomTerm(st->name);
|
||||
}
|
||||
st->user_name = YAP_MkAtomTerm(st->name);
|
||||
}
|
||||
// we assume object is already open, so there is no need to open it.
|
||||
st->u.private_data = pystream;
|
||||
st->vfs = me;
|
||||
return st;
|
||||
}
|
||||
|
||||
static bool py_close(int sno) {
|
||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||
Py_DECREF(st->u.private_data);
|
||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||
Py_XDECREF(st->u.private_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -153,7 +152,7 @@ return PyLong_AsLong(pyr);
|
||||
|
||||
static void py_flush(int sno) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
YAP_Term tg = python_acquire_GIL();
|
||||
PyGILState_STATE tg = python_acquire_GIL();
|
||||
PyObject *flush = PyObject_GetAttrString(s->u.private_data, "flush");
|
||||
PyObject_CallFunction(flush, NULL);
|
||||
python_release_GIL(tg);
|
||||
|
@@ -14,10 +14,10 @@ static foreign_t python_len(term_t tobj, term_t tf) {
|
||||
|
||||
o = term_to_python(tobj, true, NULL, true);
|
||||
if (o == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
len = PyObject_Length(o);
|
||||
pyErrorAndReturn(PL_unify_int64(tf, len), false);
|
||||
pyErrorAndReturn(PL_unify_int64(tf), false);
|
||||
}
|
||||
|
||||
static foreign_t python_dir(term_t tobj, term_t tf) {
|
||||
@@ -26,13 +26,13 @@ static foreign_t python_dir(term_t tobj, term_t tf) {
|
||||
|
||||
o = term_to_python(tobj, true, NULL, true);
|
||||
if (o == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
dir = PyObject_Dir(o);
|
||||
{
|
||||
foreign_t rc = address_to_term(dir, tf);
|
||||
;
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,14 +43,14 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
|
||||
|
||||
o = term_to_python(tobj, true, NULL, true);
|
||||
if (o == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if (!PySequence_Check(o)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
i = term_to_python(tindex, true, NULL, true);
|
||||
if (i == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
f = PyObject_CallMethodObjArgs(o, PyString_FromString("getitem"), i);
|
||||
@@ -60,7 +60,7 @@ static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
|
||||
{
|
||||
foreign_t rc = address_to_term(f, val);
|
||||
;
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,13 +72,13 @@ static foreign_t python_is(term_t tobj, term_t tf) {
|
||||
o = term_to_python(tobj, true, NULL, true);
|
||||
if (!o) {
|
||||
python_release_GIL(lim);
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
foreign_t rc = python_to_term(o, tf);
|
||||
if (rc)
|
||||
PyErr_Clear();
|
||||
python_release_GIL(lim);
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
|
||||
static foreign_t python_proc(term_t tobj) {
|
||||
@@ -89,7 +89,7 @@ static foreign_t python_proc(term_t tobj) {
|
||||
o = term_to_python(tobj, true, NULL, true);
|
||||
python_release_GIL(lim);
|
||||
bool rc = o != NULL;
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
|
||||
static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
|
||||
@@ -104,10 +104,10 @@ 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, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
Py_DecRef(pI);
|
||||
Py_DecRef(p);
|
||||
@@ -115,7 +115,7 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
|
||||
{
|
||||
foreign_t rc;
|
||||
rc = address_to_term(pF, tobj);
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,14 +132,14 @@ 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, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
if (PL_is_atom(targs)) {
|
||||
pArgs = NULL;
|
||||
} else {
|
||||
|
||||
if (!PL_get_name_arity(targs, &aname, &arity)) {
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) {
|
||||
/* ignore (_) */
|
||||
@@ -150,16 +150,16 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
DebugPrintf("Tuple %p\n", pArgs);
|
||||
|
||||
if (!pArgs) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, targs, targ)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
pArg = term_to_python(targ, true, NULL, true);
|
||||
if (pArg == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SetItem(pArgs, i, pArg);
|
||||
@@ -188,16 +188,16 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
}
|
||||
} else {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
if (pArgs)
|
||||
Py_DECREF(pArgs);
|
||||
Py_DECREF(pF);
|
||||
if (pValue == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
out = address_to_term(pValue, tf);
|
||||
pyErrorAndReturn(out, false);
|
||||
pyErrorAndReturn(out);
|
||||
}
|
||||
|
||||
static foreign_t assign_python(term_t exp, term_t name) {
|
||||
@@ -206,11 +206,11 @@ static foreign_t assign_python(term_t exp, term_t name) {
|
||||
|
||||
if (e == NULL) {
|
||||
python_release_GIL(stackp);
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
bool b = python_assign(name, e, NULL);
|
||||
python_release_GIL(stackp);
|
||||
pyErrorAndReturn(b, false);
|
||||
pyErrorAndReturn(b);
|
||||
}
|
||||
|
||||
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
@@ -224,27 +224,27 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
|
||||
if ((env = py_Builtin) == NULL) {
|
||||
// no point in even trying
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
if (PL_get_name_arity(caller, &name, &arity)) {
|
||||
if (!(s = PL_atom_chars(name))) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if ((pI = PyObject_GetAttrString(env, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
Py_INCREF(pI);
|
||||
} else {
|
||||
// Prolog should make sure this never happens.
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
pArgs = PyTuple_New(arity);
|
||||
DebugPrintf("Tuple %p\n", pArgs);
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, caller, targ)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
/* ignore (_) */
|
||||
if (i == 0 && PL_is_variable(targ)) {
|
||||
@@ -252,13 +252,13 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
} else {
|
||||
pArg = term_to_python(targ, true, NULL, true);
|
||||
if (pArg == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
if (PyTuple_SetItem(pArgs, i, pArg)) {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
}
|
||||
pOut = PyObject_CallObject(pI, pArgs);
|
||||
@@ -266,12 +266,12 @@ 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, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
{
|
||||
foreign_t rc = address_to_term(pOut, out);
|
||||
;
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,37 +284,37 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if (o == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if (PL_is_atom(f)) {
|
||||
if (!PL_get_atom_chars(f, &s)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if ((pValue = PyObject_GetAttrString(o, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
Py_INCREF(pValue);
|
||||
{ pyErrorAndReturn(python_to_term(pValue, out), false); }
|
||||
{ pyErrorAndReturn(python_to_term(pValue), false); }
|
||||
}
|
||||
if (!PL_get_name_arity(f, &name, &arity)) {
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (!s) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if ((pF = PyObject_GetAttrString(o, s)) == NULL) {
|
||||
DebugPrintf("Function %p\n", pArgs);
|
||||
DebugPrintf("Function %p\n", o);
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
pArgs = PyTuple_New(arity);
|
||||
DebugPrintf("Tuple %p\n", pArgs);
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, f, targ)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
/* ignore (_) */
|
||||
if (i == 0 && PL_is_variable(targ)) {
|
||||
@@ -322,7 +322,7 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
}
|
||||
pArg = term_to_python(targ, true, NULL, true);
|
||||
if (pArg == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SetItem(pArgs, i, pArg);
|
||||
@@ -331,9 +331,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, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
{ pyErrorAndReturn(python_to_term(pValue, out), false); }
|
||||
{ pyErrorAndReturn(python_to_term(pValue), false); }
|
||||
}
|
||||
|
||||
static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
@@ -343,7 +343,7 @@ 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, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
} else {
|
||||
PyObject *p;
|
||||
|
||||
@@ -352,29 +352,29 @@ 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, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (arity == 1 && !strcmp(s, "()")) {
|
||||
if (!PL_get_arg(1, att, att)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
}
|
||||
if (!s || !p) {
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
} else if ((pF = PyObject_GetAttrString(p, s)) == NULL) {
|
||||
PyErr_Clear();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
}
|
||||
{
|
||||
foreign_t rc;
|
||||
rc = address_to_term(pF, tobj);
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,7 +383,7 @@ static foreign_t python_main_module(term_t mod) {
|
||||
foreign_t rc;
|
||||
PyErr_Clear();
|
||||
rc = address_to_term(py_Main, mod);
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,7 +392,7 @@ static foreign_t python_function(term_t tobj) {
|
||||
PyObject *obj = term_to_python(tobj, true, NULL, true);
|
||||
foreign_t rc = PyFunction_Check(obj);
|
||||
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
|
||||
foreign_t python_builtin(term_t out) {
|
||||
@@ -400,7 +400,7 @@ foreign_t python_builtin(term_t out) {
|
||||
foreign_t rc;
|
||||
PyErr_Clear();
|
||||
rc = address_to_term(py_Builtin, out);
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,15 +417,15 @@ static foreign_t python_run_file(term_t file) {
|
||||
#else
|
||||
FILE *f = fopen(s, "r");
|
||||
if (f == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
PyRun_SimpleFileEx(f, s, 1);
|
||||
#endif
|
||||
{
|
||||
{ pyErrorAndReturn(true, false); }
|
||||
{ pyErrorAndReturn(true); }
|
||||
}
|
||||
}
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
|
||||
extern PyThreadState *YAP_save;
|
||||
@@ -442,7 +442,7 @@ static foreign_t python_run_command(term_t cmd) {
|
||||
PyRun_SimpleString(s);
|
||||
rc = true;
|
||||
}
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
|
||||
static foreign_t python_run_script(term_t cmd, term_t fun) {
|
||||
@@ -483,7 +483,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
|
||||
Py_DECREF(pModule);
|
||||
PyErr_Print();
|
||||
fprintf(stderr, "Call failed\n");
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
} else {
|
||||
pyErrorHandler();
|
||||
@@ -495,11 +495,11 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
|
||||
Py_DECREF(pModule);
|
||||
} else {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
{ pyErrorAndReturn(true, false); }
|
||||
{ pyErrorAndReturn(true); }
|
||||
}
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
|
||||
static foreign_t python_export(term_t t, term_t pl) {
|
||||
@@ -510,16 +510,16 @@ static foreign_t python_export(term_t t, term_t pl) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if (!PL_get_arg(1, t, targ)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if (!PL_get_pointer(targ, &ptr)) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
Py_INCREF((PyObject *)ptr);
|
||||
/* pyErrorAndReturn( __main__, false) */
|
||||
/* pyErrorAndReturn( __main__) */
|
||||
rc = python_to_term((PyObject *)ptr, pl);
|
||||
}
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -546,7 +546,7 @@ static int python_import(term_t mname, term_t mod) {
|
||||
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) ||
|
||||
!PL_get_arg(2, mname, mname)) {
|
||||
python_release_GIL(t0);
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
PL_get_atom_chars(arg, &sa);
|
||||
strcpy(s, sa);
|
||||
@@ -556,7 +556,7 @@ static int python_import(term_t mname, term_t mod) {
|
||||
} else if (!PL_get_nchars(mname, &len, &s,
|
||||
CVT_ATOM | CVT_EXCEPTION | REP_UTF8)) {
|
||||
python_release_GIL(t0);
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -568,7 +568,7 @@ static int python_import(term_t mname, term_t mod) {
|
||||
#endif
|
||||
python_release_GIL(t0);
|
||||
if (pName == NULL) {
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
|
||||
PyObject *pModule = PyImport_Import(pName);
|
||||
@@ -580,11 +580,11 @@ static int python_import(term_t mname, term_t mod) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
#endif
|
||||
{ pyErrorAndReturn(false, false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
{
|
||||
foreign_t rc = address_to_term(pModule, mod);
|
||||
pyErrorAndReturn(rc, false);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -593,8 +593,8 @@ static foreign_t python_to_rhs(term_t inp, term_t t) {
|
||||
PyErr_Clear();
|
||||
pVal = term_to_python(inp, true, NULL, true);
|
||||
if (pVal == NULL)
|
||||
pyErrorAndReturn(false, false);
|
||||
pyErrorAndReturn(address_to_term(pVal, t), false);
|
||||
pyErrorAndReturn(false);
|
||||
pyErrorAndReturn(address_to_term(pVal), false);
|
||||
}
|
||||
|
||||
// static PyThreadState *_saveP = NULL;
|
||||
@@ -605,7 +605,7 @@ static YAP_Int
|
||||
p_python_ensure(term_t ptr)
|
||||
{
|
||||
PyGILState_STATE _tState = PyGILState_Ensure();
|
||||
pyErrorAndReturn( PL_unify_int64(ptr, _tState), false);
|
||||
pyErrorAndReturn( PL_unify_int64(ptr), false);
|
||||
}
|
||||
|
||||
static YAP_Int
|
||||
@@ -615,7 +615,7 @@ static YAP_Int
|
||||
PyGILState_STATE _tState;
|
||||
PL_get_int64( ptr, &_tState);
|
||||
PyGILState_Release( _tState );
|
||||
pyErrorAndReturn( true, false);
|
||||
pyErrorAndReturn( true);
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -628,40 +628,50 @@ static YAP_Int p_python_threaded(void) {
|
||||
// PyEval_ReleaseThread(tstate);
|
||||
// _threaded = true;
|
||||
// _locked = 0;
|
||||
pyErrorAndReturn(true, false);
|
||||
pyErrorAndReturn(true);
|
||||
}
|
||||
|
||||
static PyGILState_STATE gstates[64];
|
||||
static int gstatei = 0;
|
||||
|
||||
term_t python_acquire_GIL(void) {
|
||||
static PyGILState_STATE gstate;
|
||||
term_t curSlot = PL_new_term_ref();
|
||||
if (!_threaded)
|
||||
pyErrorAndReturn(curSlot, false);
|
||||
// extern int Yap_do_low_level_trace;
|
||||
// Yap_do_low_level_trace = 1;
|
||||
// f[rintf( stderr, "++%d\n", ++_locked);
|
||||
// fprintf( stderr, "++%d\n", ++_locked);
|
||||
// if (_locked > 0) { _locked++ ; }
|
||||
// else
|
||||
gstate = PyGILState_Ensure();
|
||||
fprintf(stderr, "+%d\n", (int)gstate);
|
||||
PL_put_integer(curSlot, gstate);
|
||||
pyErrorAndReturn(curSlot, false);
|
||||
if (_threaded) {
|
||||
gstates[gstatei] = PyGILState_Ensure();
|
||||
}
|
||||
fprintf(stderr, "+%d\n", (int)gstatei);
|
||||
PL_put_integer(curSlot, gstatei++);
|
||||
return curSlot;
|
||||
}
|
||||
|
||||
bool python_release_GIL(term_t curBlock) {
|
||||
PyGILState_STATE gstate;
|
||||
int gstateix;
|
||||
|
||||
if (_threaded) {
|
||||
PL_get_integer(curBlock, &gstate);
|
||||
PyGILState_Release(gstate);
|
||||
fprintf(stderr, "-%d\n", (int)gstate);
|
||||
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) {
|
||||
PyGILState_Release(gstates[gstatei--]);
|
||||
}
|
||||
PL_reset_term_refs(curBlock);
|
||||
pyErrorAndReturn(true, false);
|
||||
pyErrorAndReturn(true);
|
||||
}
|
||||
|
||||
//: prolog: release python
|
||||
|
||||
/
|
||||
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);
|
||||
|
@@ -1,12 +1,21 @@
|
||||
|
||||
import os.path
|
||||
import sys
|
||||
import keyword
|
||||
# debugging support.
|
||||
# import pdb
|
||||
from collections import namedtuple
|
||||
import readline
|
||||
from .yap import *
|
||||
from yap4py.yap import YAPEngine, YAPEngineArgs, YAPPredicate, YAPQuery, YAPPrologPredicate, YAPVarTerm
|
||||
from os.path import join, dirname
|
||||
from collections import namedtuple
|
||||
import sys
|
||||
|
||||
yap_lib_path = dirname(__file__)
|
||||
|
||||
compile = namedtuple('compile', 'file')
|
||||
bindvars = namedtuple('bindvars', 'list')
|
||||
library = namedtuple('library', 'list')
|
||||
v0 = namedtuple('v', 'slot')
|
||||
yap_query = namedtuple('yap_query', 'query owner')
|
||||
jupyter_query = namedtuple('jupyter_query', 'vars dict')
|
||||
python_query = namedtuple('python_query', 'vars dict')
|
||||
yapi_query = namedtuple('yapi_query', 'vars dict')
|
||||
show_answer = namedtuple('show_answer', 'vars dict')
|
||||
set_prolog_flag = namedtuple('set_prolog_flag', 'flag new_value')
|
||||
|
||||
|
||||
class Engine( YAPEngine ):
|
||||
@@ -16,10 +25,10 @@ class Engine( YAPEngine ):
|
||||
if not args:
|
||||
args = EngineArgs(**kwargs)
|
||||
if self_contained:
|
||||
yap_lib_path = os.path.dirname(__file__)
|
||||
args.setYapShareDir(os.path.join(yap_lib_path, "prolog"))
|
||||
yap_lib_path = dirname(__file__)
|
||||
args.setYapShareDir(join(yap_lib_path, "prolog"))
|
||||
args.setYapPLDIR(yap_lib_path)
|
||||
args.setSavedState(os.path.join(yap_lib_path, "startup.yss"))
|
||||
args.setSavedState(join(yap_lib_path, "startup.yss"))
|
||||
YAPEngine.__init__(self, args)
|
||||
self.goal(set_prolog_flag('verbose', 'silent'))
|
||||
self.goal(compile(library('yapi')))
|
||||
@@ -31,9 +40,6 @@ class Engine( YAPEngine ):
|
||||
else:
|
||||
self.goal(g)
|
||||
|
||||
def f(self, g):
|
||||
self.E.fun(g)
|
||||
|
||||
|
||||
class EngineArgs( YAPEngineArgs ):
|
||||
""" Interface to Engine Options class"""
|
||||
@@ -47,112 +53,79 @@ class Predicate( YAPPredicate ):
|
||||
def __init__(self, t, module=None):
|
||||
super().__init__(t)
|
||||
|
||||
class IQuery(YAPQuery):
|
||||
class Query:
|
||||
"""Goal is a predicate instantiated under a specific environment """
|
||||
def __init__(self, engine, g):
|
||||
self = engine.query(g)
|
||||
self.port = "call"
|
||||
self.bindings = None
|
||||
self.q = engine.query(g)
|
||||
if self.q:
|
||||
self.port = "call"
|
||||
self.bindings = None
|
||||
self.engine = engine
|
||||
self.answer = {}
|
||||
|
||||
def __iter__(self):
|
||||
return PrologTableIter( self )
|
||||
|
||||
class PrologTableIter:
|
||||
|
||||
def __init__(self, q):
|
||||
try:
|
||||
self.q = q
|
||||
except:
|
||||
print('Error')
|
||||
|
||||
def __iter__(self):
|
||||
# Iterators are iterables too.
|
||||
# - # Adding this functions to make them so.
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
print(self)
|
||||
if not self.q:
|
||||
raise StopIteration()
|
||||
if self.q.next():
|
||||
rc = self.q.bindings
|
||||
if self.q.port == "exit":
|
||||
self.q.close()
|
||||
rc = self.answer
|
||||
if self.port == "exit":
|
||||
self.close()
|
||||
return rc
|
||||
else:
|
||||
if self.q:
|
||||
if self:
|
||||
self.close()
|
||||
raise StopIteration()
|
||||
|
||||
def close(self):
|
||||
def close( self ):
|
||||
self.q.close()
|
||||
self.q = None
|
||||
|
||||
f2p = {"fails":{}}
|
||||
for i in range(16):
|
||||
f2p[i] ={}
|
||||
|
||||
|
||||
|
||||
|
||||
global engine, handler
|
||||
|
||||
yap_lib_path = os.path.dirname(__file__)
|
||||
|
||||
compile = namedtuple('compile', 'file')
|
||||
bindvars = namedtuple('bindvars', 'list')
|
||||
library = namedtuple('library', 'list')
|
||||
v = namedtuple( 'v', 'slot')
|
||||
yap_query = namedtuple( 'yap_query', 'query owner')
|
||||
jupyter_query = namedtuple( 'jupyter_query', 'vars dict')
|
||||
python_query = namedtuple( 'python_query', 'vars dict')
|
||||
yapi_query = namedtuple( 'yapi_query', 'vars dict')
|
||||
show_answer = namedtuple( 'show_answer', 'vars dict')
|
||||
set_prolog_flag = namedtuple('set_prolog_flag', 'flag new_value')
|
||||
|
||||
|
||||
def named( name, arity):
|
||||
def name( name, arity):
|
||||
try:
|
||||
if arity > 0 and name.isidentifier() and not keyword.iskeyword(name):
|
||||
if arity > 0 and name.isidentifier(): # and not keyword.iskeyword(name):
|
||||
s = []
|
||||
for i in range(arity):
|
||||
s += ["A" + str(i)]
|
||||
f2p[arity][name] = namedtuple(name, s)
|
||||
return namedtuple(name, s)
|
||||
except:
|
||||
f2p[fails][name] = True
|
||||
return None
|
||||
|
||||
class PrologPredicate( YAPPrologPredicate ):
|
||||
""" Interface to Prolog Predicate"""
|
||||
|
||||
class v(YAPVarTerm):
|
||||
class v(YAPVarTerm,v0):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
YAPVarTerm.__init__()
|
||||
|
||||
def binding(self):
|
||||
return self.term()
|
||||
|
||||
def numbervars( q ):
|
||||
Dict = {}
|
||||
if True:
|
||||
engine.goal(show_answer( q.namedVars(), Dict))
|
||||
return Dict
|
||||
rc = q.namedVarsVector()
|
||||
q.r = q.goal().numbervars()
|
||||
o = []
|
||||
for i in rc:
|
||||
if len(i) == 2:
|
||||
do = str(i[0]) + " = " + str( i[1] ) + "\n"
|
||||
o += do
|
||||
else:
|
||||
do = str(i[0]) + " = " + str( i[1] ) + "\n"
|
||||
o += do
|
||||
return o
|
||||
|
||||
class YAPShell:
|
||||
|
||||
def numbervars( self ):
|
||||
Dict = {}
|
||||
self.engine.goal(show_answer( self, Dict))
|
||||
return Dict
|
||||
# rc = self.q.namedVarsVector()
|
||||
# self.q.r = self.q.goal().numbervars()
|
||||
# o = []
|
||||
# for i in rc:
|
||||
# if len(i) == 2:
|
||||
# do = str(i[0]) + " = " + str( i[1] ) + "\n"
|
||||
# o += do
|
||||
# else:
|
||||
# do = str(i[0]) + " = " + str( i[1] ) + "\n"
|
||||
# o += do
|
||||
# return o
|
||||
|
||||
|
||||
|
||||
def query_prolog(self, engine, query):
|
||||
def query_prolog(self, query):
|
||||
#import pdb; pdb.set_trace()
|
||||
#
|
||||
# construct a query from a one-line string
|
||||
@@ -172,12 +145,12 @@ class YAPShell:
|
||||
# print( "Error: Variable Name matches a Python Symbol")
|
||||
# return
|
||||
do_ask = True
|
||||
self.e = engine
|
||||
engine = self.engine
|
||||
bindings = []
|
||||
g = python_query(self, query)
|
||||
if not self.q:
|
||||
self.it = IQuery( engine, g )
|
||||
for bind in self.it:
|
||||
self.q = Query( engine, g )
|
||||
for bind in self.q:
|
||||
bindings += [bind]
|
||||
if do_ask:
|
||||
print(bindings)
|
||||
@@ -214,7 +187,7 @@ class YAPShell:
|
||||
if not s:
|
||||
loop = False
|
||||
else:
|
||||
self.query_prolog(engine, s)
|
||||
self.query_prolog(s)
|
||||
except SyntaxError as err:
|
||||
print("Syntax Error error: {0}".format(err))
|
||||
except EOFError:
|
||||
@@ -233,13 +206,13 @@ class YAPShell:
|
||||
# engine = yap.YAPEngine(yap.YAPParams());
|
||||
#
|
||||
def __init__(self, engine, **kwargs):
|
||||
self.live(engine)
|
||||
self.engine = engine
|
||||
self.live(engine)
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
engine = Engine()
|
||||
handler = numbervars
|
||||
YAPShell(engine)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@@ -2664,9 +2664,22 @@ class InteractiveShell(SingletonConfigurable):
|
||||
print("go")
|
||||
result = None
|
||||
try:
|
||||
result = self._yrun_cell(
|
||||
raw_cell, store_history, silent, shell_futures)
|
||||
import trace
|
||||
tracer = trace.Trace(
|
||||
#ignoredirs=[sys.prefix, sys.exec_prefix],
|
||||
trace=1,
|
||||
count=0)
|
||||
|
||||
# run the new command using the given tracer
|
||||
#
|
||||
result =tracer.runfunc(self._yrun_cell,
|
||||
raw_cell, store_history,
|
||||
silent, shell_futures)
|
||||
|
||||
# result = self._yrun_cell(
|
||||
# raw_cell, store_history, silent, shell_futures)
|
||||
finally:
|
||||
print("ugh", self.events)
|
||||
self.events.trigger('post_execute')
|
||||
if not silent:
|
||||
self.events.trigger('post_run_cell', result)
|
||||
|
@@ -62,9 +62,11 @@ blankc('\t').
|
||||
|
||||
|
||||
streams(false) :-
|
||||
% close( user_input),
|
||||
close( user_error ),
|
||||
close( user_output ).
|
||||
flush_output,
|
||||
forall(
|
||||
stream_property( S, mode(_) ),
|
||||
close(S)
|
||||
).
|
||||
streams(true) :-
|
||||
% open('/python/input', read, _Input, [alias(user_input),bom(false)]),
|
||||
open('/python/sys.stdout', append, _Output, [alias(user_output)]),
|
||||
|
@@ -39,6 +39,7 @@ completions = namedtuple('completions', 'txt self' )
|
||||
errors = namedtuple('errors', 'self text' )
|
||||
streams = namedtuple('streams', ' text' )
|
||||
|
||||
global engine
|
||||
|
||||
class YAPInputSplitter(InputSplitter):
|
||||
"""An input splitter that recognizes all of iyap's special syntax."""
|
||||
@@ -106,11 +107,10 @@ class YAPInputSplitter(InputSplitter):
|
||||
def validQuery(self, text, engine, shell, line=None):
|
||||
"""Return whether a legal query
|
||||
"""
|
||||
if text == shell.os:
|
||||
if shell and text == shell.os:
|
||||
return True
|
||||
if not line:
|
||||
line = text.rstrip()
|
||||
(line, _, _, _)=self.shell.clean_end(line)
|
||||
self.errors = []
|
||||
engine.mgoal(errors(self, line),"user")
|
||||
return self.errors != []
|
||||
@@ -181,7 +181,7 @@ class YAPInputSplitter(InputSplitter):
|
||||
if self.transformer_accumulating:
|
||||
return True
|
||||
else:
|
||||
return self.validQuery(self.source, self.yapeng, self)
|
||||
return self.validQuery(self.source, engine, self.shell)
|
||||
|
||||
def transform_cell(self, cell):
|
||||
"""Process and translate a cell of input.
|
||||
@@ -509,6 +509,8 @@ class YAPRun:
|
||||
def __init__(self, shell):
|
||||
self.shell = shell
|
||||
self.yapeng = Engine()
|
||||
global engine
|
||||
engine = self.yapeng
|
||||
self.yapeng.goal(use_module(library("jupyter")))
|
||||
self.query = None
|
||||
self.os = None
|
||||
@@ -542,6 +544,7 @@ class YAPRun:
|
||||
pg = jupyter_query( self, program, query)
|
||||
self.query = self.yapeng.query( pg)
|
||||
self.query.port = "call"
|
||||
self.query.answer = {}
|
||||
else:
|
||||
self.query.port = "retry"
|
||||
self.os = s
|
||||
|
Reference in New Issue
Block a user