diff --git a/packages/python/py4yap.h b/packages/python/py4yap.h index 0c799427e..cb925e731 100644 --- a/packages/python/py4yap.h +++ b/packages/python/py4yap.h @@ -63,7 +63,7 @@ extern bool init_python_vfs(void); 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, +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, diff --git a/packages/python/pyio.c b/packages/python/pyio.c index ff2881228..ceac10525 100644 --- a/packages/python/pyio.c +++ b/packages/python/pyio.c @@ -14,13 +14,13 @@ static int py_put(int sno, int ch) // PyObject *pyw_data; StreamDesc *st = YAP_GetStreamFromId(sno); if (st->user_name == TermOutStream) { - PyGILState_STATE tg = python_acquire_GIL(); + term_t tg = python_acquire_GIL(); PySys_WriteStdout("%C", ch); python_release_GIL(tg); return ch; } if (st->user_name == TermErrStream) { - PyGILState_STATE tg = python_acquire_GIL(); + term_t tg = python_acquire_GIL(); PySys_WriteStderr("%C", ch); python_release_GIL(tg); return ch; @@ -29,7 +29,7 @@ static int py_put(int sno, int ch) PyObject *err; s[0] = ch; s[1] = '\0'; - PyGILState_STATE g0 = python_acquire_GIL(); + term_t g0 = python_acquire_GIL(); PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"), PyUnicode_FromString(s), NULL); python_release_GIL(g0); @@ -88,10 +88,12 @@ static bool py_close(int sno) { static bool getLine(int inp) { char *myrl_line = NULL; StreamDesc *rl_instream = YAP_RepStreamFromId(inp); - PyObject*prompt = PyUnicode_FromString( "?- "), + term_t ctk = python_acquire_GIL(); + PyObject*prompt = PyUnicode_FromString( "?- "), *msg = PyUnicode_FromString(""); /* window of vulnerability opened */ myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(rl_instream->u.private_data,msg,prompt,NULL)); + python_release_GIL(ctk); rl_instream->u.irl.ptr = rl_instream->u.irl.buf = (const unsigned char*)myrl_line; myrl_line = NULL; return true; diff --git a/packages/python/pypreds.c b/packages/python/pypreds.c index c29c2effe..d03e1ac34 100644 --- a/packages/python/pypreds.c +++ b/packages/python/pypreds.c @@ -1 +1,703 @@ - #include "py4yap.h" PyObject *py_Main; void pyErrorHandler__(int line, const char *file, const char *code) { // this code is called if a Python error is found. fprintf(stderr, " Python error detected at %s %s:%d\n\n", code, file, line); PyErr_Print(); } static foreign_t python_len(term_t tobj, term_t tf) { Py_ssize_t len; PyObject *o; o = term_to_python(tobj, true, NULL, true); if (o == NULL) { pyErrorAndReturn(false); } len = PyObject_Length(o); pyErrorAndReturn(PL_unify_int64(tf, len)); } static foreign_t python_dir(term_t tobj, term_t tf) { PyObject *dir; PyObject *o; o = term_to_python(tobj, true, NULL, true); if (o == NULL) { pyErrorAndReturn(false); } dir = PyObject_Dir(o); { foreign_t rc = address_to_term(dir, tf); ; pyErrorAndReturn(rc); } } static foreign_t python_index(term_t tobj, term_t tindex, term_t val) { PyObject *i; PyObject *o; PyObject *f; o = term_to_python(tobj, true, NULL, true); if (o == NULL) { pyErrorAndReturn(false); } if (!PySequence_Check(o)) { pyErrorAndReturn(false); } i = term_to_python(tindex, true, NULL, true); if (i == NULL) { pyErrorAndReturn(false); } #if PY_MAJOR_VERSION < 3 f = PyObject_CallMethodObjArgs(o, PyString_FromString("getitem"), i); #else f = PyObject_CallMethodObjArgs(o, PyUnicode_FromString("getitem"), i); #endif { foreign_t rc = address_to_term(f, val); ; pyErrorAndReturn(rc); } } static foreign_t python_is(term_t tobj, term_t tf) { PyObject *o; term_t lim = python_acquire_GIL(); o = term_to_python(tobj, true, NULL, true); if (!o) { python_release_GIL(lim); pyErrorAndReturn(false); } foreign_t rc = python_to_term(o, tf); if (rc) PyErr_Clear(); python_release_GIL(lim); pyErrorAndReturn(rc); } static foreign_t python_proc(term_t tobj) { PyObject *o; term_t lim = python_acquire_GIL(); o = term_to_python(tobj, true, NULL, true); python_release_GIL(lim); bool rc = o != NULL; pyErrorAndReturn(rc); } static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) { PyObject *pF, *pI; PyObject *p; // get Scope ... pI = term_to_python(indx, true, NULL, true); // got Scope.Exp // get Scope ... p = term_to_python(parent, true, NULL, true); // Exp if (!pI || !p) { { pyErrorAndReturn(false); } } else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) { PyErr_Print(); { pyErrorAndReturn(false); } } Py_DecRef(pI); Py_DecRef(p); Py_INCREF(pF); { foreign_t rc; rc = address_to_term(pF, tobj); pyErrorAndReturn(rc); } } static foreign_t python_apply(term_t tin, term_t targs, term_t keywds, term_t tf) { PyObject *pF; PyObject *pArgs, *pKeywords; PyObject *pValue; int i, arity; atom_t aname; foreign_t out; term_t targ = PL_new_term_ref(); pF = term_to_python(tin, true, NULL, true); PyErr_Clear(); if (pF == NULL) { { pyErrorAndReturn(false); } } if (PL_is_atom(targs)) { pArgs = NULL; } else { if (!PL_get_name_arity(targs, &aname, &arity)) { { pyErrorAndReturn(false); } } if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) { /* ignore (_) */ pArgs = NULL; } else { pArgs = PyTuple_New(arity); DebugPrintf("Tuple %p\n", pArgs); if (!pArgs) { pyErrorAndReturn(false); } for (i = 0; i < arity; i++) { PyObject *pArg; if (!PL_get_arg(i + 1, targs, targ)) { pyErrorAndReturn(false); } pArg = term_to_python(targ, true, NULL, true); if (pArg == NULL) { pyErrorAndReturn(false); } /* pArg reference stolen here: */ PyTuple_SetItem(pArgs, i, pArg); } } } if (PL_is_atom(keywds)) { pKeywords = NULL; } else { pKeywords = term_to_python(keywds, true, NULL, true); } if (PyCallable_Check(pF)) { pValue = PyEval_CallObjectWithKeywords(pF, pArgs, pKeywords); // PyObject_Print(pF,stderr,0);fprintf(stderr, "\n"); // PyObject_Print(pArgs,stderr,0);fprintf(stderr, " "); // PyObject_Print(pKeywords,stderr,0);fprintf(stderr, "\n"); if (!pValue) PyErr_Print(); else Py_IncRef(pValue); } else if (pArgs == NULL) { pValue = pF; if (pF) { Py_IncRef(pValue); } } else { PyErr_Print(); { pyErrorAndReturn(false); } } if (pArgs) Py_DECREF(pArgs); Py_DECREF(pF); if (pValue == NULL) { pyErrorAndReturn(false); } out = address_to_term(pValue, tf); pyErrorAndReturn(out); } static foreign_t assign_python(term_t exp, term_t name) { term_t stackp = python_acquire_GIL(); PyObject *e = term_to_python(exp, true, NULL, true); if (e == NULL) { python_release_GIL(stackp); pyErrorAndReturn(false); } bool b = python_assign(name, e, NULL); python_release_GIL(stackp); pyErrorAndReturn(b); } static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) { PyErr_Clear(); PyObject *pI, *pArgs, *pOut; PyObject *env; atom_t name; char *s; int i, arity; term_t targ = PL_new_term_ref(); if ((env = py_Builtin) == NULL) { // no point in even trying { pyErrorAndReturn(false); } } if (PL_get_name_arity(caller, &name, &arity)) { if (!(s = PL_atom_chars(name))) { pyErrorAndReturn(false); } if ((pI = PyObject_GetAttrString(env, s)) == NULL) { PyErr_Print(); { pyErrorAndReturn(false); } } Py_INCREF(pI); } else { // Prolog should make sure this never happens. { 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); } /* ignore (_) */ if (i == 0 && PL_is_variable(targ)) { pArg = Py_None; } else { pArg = term_to_python(targ, true, NULL, true); if (pArg == NULL) { pyErrorAndReturn(false); } } /* pArg reference stolen here: */ if (PyTuple_SetItem(pArgs, i, pArg)) { PyErr_Print(); { pyErrorAndReturn(false); } } } pOut = PyObject_CallObject(pI, pArgs); Py_DECREF(pArgs); Py_DECREF(pI); if (pOut == NULL) { PyErr_Print(); { pyErrorAndReturn(false); } } { foreign_t rc = address_to_term(pOut, out); ; pyErrorAndReturn(rc); } } static foreign_t python_access(term_t obj, term_t f, term_t out) { PyErr_Clear(); PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF; atom_t name; char *s = NULL; int i, arity; term_t targ = PL_new_term_ref(); if (o == NULL) { pyErrorAndReturn(false); } if (PL_is_atom(f)) { if (!PL_get_atom_chars(f, &s)) { pyErrorAndReturn(false); } if ((pValue = PyObject_GetAttrString(o, s)) == NULL) { PyErr_Print(); { pyErrorAndReturn(false); } } Py_INCREF(pValue); { pyErrorAndReturn(python_to_term(pValue, out) ); } } if (!PL_get_name_arity(f, &name, &arity)) { { pyErrorAndReturn(false); } } s = PL_atom_chars(name); if (!s) { pyErrorAndReturn(false); } if ((pF = PyObject_GetAttrString(o, s)) == NULL) { DebugPrintf("Function %p\n", o); PyErr_Print(); { 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); } /* ignore (_) */ if (i == 0 && PL_is_variable(targ)) { pArgs = Py_None; } pArg = term_to_python(targ, true, NULL, true); if (pArg == NULL) { pyErrorAndReturn(false); } /* pArg reference stolen here: */ PyTuple_SetItem(pArgs, i, pArg); } pValue = PyObject_CallObject(pF, pArgs); Py_DECREF(pArgs); Py_DECREF(pF); if (pValue == NULL) { { pyErrorAndReturn(false); } } { pyErrorAndReturn(python_to_term(pValue, out)); } } static foreign_t python_field(term_t parent, term_t att, term_t tobj) { PyObject *pF; atom_t name; char *s; int arity; if (!PL_get_name_arity(att, &name, &arity)) { { pyErrorAndReturn(false); } } else { PyObject *p; // got Scope.Exp // get Scope ... p = term_to_python(parent, true, NULL, true); // Exp if (!PL_get_name_arity(att, &name, &arity)) { { pyErrorAndReturn(false); } } s = PL_atom_chars(name); if (arity == 1 && !strcmp(s, "()")) { if (!PL_get_arg(1, att, att)) { pyErrorAndReturn(false); } if (!PL_get_name_arity(att, &name, &arity)) { { pyErrorAndReturn(false); } } s = PL_atom_chars(name); } if (!s || !p) { { pyErrorAndReturn(false); } } else if ((pF = PyObject_GetAttrString(p, s)) == NULL) { PyErr_Clear(); { pyErrorAndReturn(false); } } } { foreign_t rc; rc = address_to_term(pF, tobj); pyErrorAndReturn(rc); } } static foreign_t python_main_module(term_t mod) { { foreign_t rc; PyErr_Clear(); rc = address_to_term(py_Main, mod); pyErrorAndReturn(rc); } } static foreign_t python_function(term_t tobj) { PyErr_Clear(); PyObject *obj = term_to_python(tobj, true, NULL, true); foreign_t rc = PyFunction_Check(obj); pyErrorAndReturn(rc); } foreign_t python_builtin(term_t out) { { foreign_t rc; PyErr_Clear(); rc = address_to_term(py_Builtin, out); pyErrorAndReturn(rc); } } static foreign_t python_run_file(term_t file) { char *s; size_t len; char si[256]; s = si; PyErr_Clear(); if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) { #if PY_MAJOR_VERSION < 3 PyObject *PyFileObject = PyFile_FromString(si, "r"); PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "test.py", 1); #else FILE *f = fopen(s, "r"); if (f == NULL) { pyErrorAndReturn(false); } PyRun_SimpleFileEx(f, s, 1); #endif { { pyErrorAndReturn(true); } } } { pyErrorAndReturn(false); } } extern PyThreadState *YAP_save; static foreign_t python_run_command(term_t cmd) { char *s; bool rc = false; size_t len; char si[256]; PyErr_Clear(); s = si; if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) { PyRun_SimpleString(s); rc = true; } pyErrorAndReturn(rc); } static foreign_t python_run_script(term_t cmd, term_t fun) { char si[256], sf[256]; size_t len = 255, len1 = 255; PyObject *pName, *pModule, *pFunc; PyObject *pArgs = NULL, *pValue; char *s; PyErr_Clear(); s = si; if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) && (s = sf) != NULL && PL_get_nchars(fun, &len1, &s, CVT_ALL | CVT_EXCEPTION)) { #if PY_MAJOR_VERSION < 3 pName = PyString_FromString("rbm"); #else // asssumes UTF-8 pName = PyUnicode_FromString("rbm"); #endif /* Error checking of pName left out */ pModule = PyImport_Import(pName); PyErr_Clear(); Py_DECREF(pName); if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, sf); /* pFunc is a new reference */ if (pFunc && PyCallable_Check(pFunc)) { pValue = PyObject_CallObject(pFunc, pArgs); if (pValue != NULL) { Py_DECREF(pValue); } else { Py_DECREF(pFunc); Py_DECREF(pModule); PyErr_Print(); fprintf(stderr, "Call failed\n"); { pyErrorAndReturn(false); } } } else { pyErrorHandler(); if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", sf); } Py_XDECREF(pFunc); Py_DECREF(pModule); } else { PyErr_Print(); { pyErrorAndReturn(false); } } { pyErrorAndReturn(true); } } { pyErrorAndReturn(false); } } static foreign_t python_export(term_t t, term_t pl) { foreign_t rc = false; PyErr_Clear(); if (PL_is_functor(t, FUNCTOR_pointer1)) { void *ptr; term_t targ = PL_new_term_ref(); if (!PL_get_arg(1, t, targ)) { pyErrorAndReturn(false); } if (!PL_get_pointer(targ, &ptr)) { pyErrorAndReturn(false); } Py_INCREF((PyObject *)ptr); /* pyErrorAndReturn( __main__) */ rc = python_to_term((PyObject *)ptr, pl); } pyErrorAndReturn(rc); } /** * @pred python_import(MName, Mod) * Import a python module to the YAP environment. * * @param mname module name, either an atom or a sequence of atoms, * eg os.sys * @param mod the pointer to the Python object * @return success? */ static int python_import(term_t mname, term_t mod) { PyObject *pName; term_t arg = PL_new_term_ref(); char s0[MAXPATHLEN], *s = s0; while (true) { size_t len; //PyErr_Clear(); len = (MAXPATHLEN - 1) - (s - s0); if (PL_is_pair(mname)) { char *sa = NULL; if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) || !PL_get_arg(2, mname, mname)) { pyErrorAndReturn(false); } PL_get_atom_chars(arg, &sa); strcpy(s, sa); s += strlen(s); *s++ = '.'; s[0] = '\0'; } else if (!PL_get_nchars(mname, &len, &s, CVT_ATOM | CVT_EXCEPTION | REP_UTF8)) { pyErrorAndReturn(false); } else { break; } } term_t t0 = python_acquire_GIL(); #if PY_MAJOR_VERSION < 3 pName = PyString_FromString(s0); #else pName = PyUnicode_FromString(s0); #endif if (pName == NULL) { python_release_GIL(t0); pyErrorAndReturn(false); } PyObject *pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule == NULL) { #if EXTRA_MESSSAGES if (PyErr_Occurred()) PyErr_Print(); PyErr_Clear(); #endif python_release_GIL(t0); pyErrorAndReturn(false); } { foreign_t rc = address_to_term(pModule, mod); python_release_GIL(t0); pyErrorAndReturn(rc); } } static foreign_t python_to_rhs(term_t inp, term_t t) { PyObject *pVal; PyErr_Clear(); pVal = term_to_python(inp, true, NULL, true); if (pVal == NULL) pyErrorAndReturn(false); pyErrorAndReturn(address_to_term(pVal, t)); } // static PyThreadState *_saveP = NULL; static bool _threaded = true; /* static YAP_Int p_python_ensure(term_t ptr) { PyGILState_STATE _tState = PyGILState_Ensure(); pyErrorAndReturn( PL_unify_int64(ptr), false); } static YAP_Int p_python_release(term_t ptr) { PyGILState_STATE _tState; PL_get_int64( ptr, &_tState); PyGILState_Release( _tState ); pyErrorAndReturn( true); } */ int _locked = 0; PyThreadState *tstate; static YAP_Int p_python_threaded(void) { PyErr_Clear(); // PyEval_ReleaseThread(tstate); // _threaded = true; // _locked = 0; pyErrorAndReturn(true); } static PyGILState_STATE gstates[64]; static int gstatei = 0; term_t python_acquire_GIL(void) { term_t curSlot = PL_new_term_ref(); // extern int Yap_do_low_level_trace; // Yap_do_low_level_trace = 1; // fprintf( stderr, "++%d\n", ++_locked); // if (_locked > 0) { _locked++ ; } // else 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) { 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) { 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); PL_register_foreign("python_import", 2, python_import, 0); PL_register_foreign("python_to_rhs", 2, python_to_rhs, 0); PL_register_foreign("python_len", 2, python_len, 0); PL_register_foreign("python_is", 2, python_is, 0); PL_register_foreign("python_dir", 2, python_dir, 0); PL_register_foreign("python_apply", 4, python_apply, 0); PL_register_foreign("python_index", 3, python_index, 0); PL_register_foreign("python_field", 3, python_field, 0); PL_register_foreign("python_assign", 2, assign_python, 0); PL_register_foreign("python_export", 2, python_export, 0); PL_register_foreign("python_function", 1, python_function, 0); PL_register_foreign("python_slice", 4, python_slice, 0); PL_register_foreign("python_run_file", 1, python_run_file, 0); PL_register_foreign("python_proc", 1, python_proc, 0); PL_register_foreign("python_run_command", 1, python_run_command, 0); PL_register_foreign("python_run_script", 2, python_run_script, 0); PL_register_foreign("python_main_module", 1, python_main_module, 0); PL_register_foreign("python_import", 2, python_import, 0); PL_register_foreign("python_access", 3, python_access, 0); PL_register_foreign("python_threaded", 0, p_python_threaded, 0); init_python_vfs(); } \ No newline at end of file + +#include "py4yap.h" + +PyObject *py_Main; + +void pyErrorHandler__(int line, const char *file, const char *code) { + // this code is called if a Python error is found. + fprintf(stderr, " Python error detected at %s %s:%d\n\n", code, file, line); + PyErr_Print(); +} +static foreign_t python_len(term_t tobj, term_t tf) { + Py_ssize_t len; + PyObject *o; + + o = term_to_python(tobj, true, NULL, true); + if (o == NULL) { + pyErrorAndReturn(false); + } + len = PyObject_Length(o); + pyErrorAndReturn(PL_unify_int64(tf, len)); +} + +static foreign_t python_dir(term_t tobj, term_t tf) { + PyObject *dir; + PyObject *o; + + o = term_to_python(tobj, true, NULL, true); + if (o == NULL) { + pyErrorAndReturn(false); + } + dir = PyObject_Dir(o); + { + foreign_t rc = address_to_term(dir, tf); + ; + pyErrorAndReturn(rc); + } +} + +static foreign_t python_index(term_t tobj, term_t tindex, term_t val) { + PyObject *i; + PyObject *o; + PyObject *f; + + o = term_to_python(tobj, true, NULL, true); + if (o == NULL) { + pyErrorAndReturn(false); + } + if (!PySequence_Check(o)) { + pyErrorAndReturn(false); + } + i = term_to_python(tindex, true, NULL, true); + if (i == NULL) { + pyErrorAndReturn(false); + } +#if PY_MAJOR_VERSION < 3 + f = PyObject_CallMethodObjArgs(o, PyString_FromString("getitem"), i); +#else + f = PyObject_CallMethodObjArgs(o, PyUnicode_FromString("getitem"), i); +#endif + { + foreign_t rc = address_to_term(f, val); + ; + pyErrorAndReturn(rc); + } +} + +static foreign_t python_is(term_t tobj, term_t tf) { + PyObject *o; + + term_t lim = python_acquire_GIL(); + + o = term_to_python(tobj, true, NULL, true); + if (!o) { + python_release_GIL(lim); + pyErrorAndReturn(false); + } + foreign_t rc = python_to_term(o, tf); + if (rc) + PyErr_Clear(); + python_release_GIL(lim); + pyErrorAndReturn(rc); +} + +static foreign_t python_proc(term_t tobj) { + PyObject *o; + + term_t lim = python_acquire_GIL(); + + o = term_to_python(tobj, true, NULL, true); + python_release_GIL(lim); + bool rc = o != NULL; + pyErrorAndReturn(rc); +} + +static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) { + PyObject *pF, *pI; + + PyObject *p; + + // get Scope ... + pI = term_to_python(indx, true, NULL, true); + // got Scope.Exp + // get Scope ... + p = term_to_python(parent, true, NULL, true); + // Exp + if (!pI || !p) { + { pyErrorAndReturn(false); } + } else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) { + PyErr_Print(); + { pyErrorAndReturn(false); } + } + Py_DecRef(pI); + Py_DecRef(p); + Py_INCREF(pF); + { + foreign_t rc; + rc = address_to_term(pF, tobj); + pyErrorAndReturn(rc); + } +} + +static foreign_t python_apply(term_t tin, term_t targs, term_t keywds, + term_t tf) { + PyObject *pF; + PyObject *pArgs, *pKeywords; + PyObject *pValue; + int i, arity; + atom_t aname; + foreign_t out; + term_t targ = PL_new_term_ref(); + + pF = term_to_python(tin, true, NULL, true); + PyErr_Clear(); + if (pF == NULL) { + { pyErrorAndReturn(false); } + } + if (PL_is_atom(targs)) { + pArgs = NULL; + } else { + + if (!PL_get_name_arity(targs, &aname, &arity)) { + { pyErrorAndReturn(false); } + } + if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) { + /* ignore (_) */ + pArgs = NULL; + } else { + + pArgs = PyTuple_New(arity); + DebugPrintf("Tuple %p\n", pArgs); + + if (!pArgs) { + pyErrorAndReturn(false); + } + for (i = 0; i < arity; i++) { + PyObject *pArg; + if (!PL_get_arg(i + 1, targs, targ)) { + pyErrorAndReturn(false); + } + pArg = term_to_python(targ, true, NULL, true); + if (pArg == NULL) { + pyErrorAndReturn(false); + } + /* pArg reference stolen here: */ + PyTuple_SetItem(pArgs, i, pArg); + } + } + } + if (PL_is_atom(keywds)) { + pKeywords = NULL; + } else { + pKeywords = term_to_python(keywds, true, NULL, true); + } + if (PyCallable_Check(pF)) { + pValue = PyEval_CallObjectWithKeywords(pF, pArgs, pKeywords); + // PyObject_Print(pF,stderr,0);fprintf(stderr, "\n"); + // PyObject_Print(pArgs,stderr,0);fprintf(stderr, " "); + // PyObject_Print(pKeywords,stderr,0);fprintf(stderr, "\n"); + if (!pValue) + PyErr_Print(); + else + Py_IncRef(pValue); + } else if (pArgs == NULL) { + pValue = pF; + + if (pF) { + Py_IncRef(pValue); + } + } else { + PyErr_Print(); + { pyErrorAndReturn(false); } + } + if (pArgs) + Py_DECREF(pArgs); + Py_DECREF(pF); + if (pValue == NULL) { + pyErrorAndReturn(false); + } + out = address_to_term(pValue, tf); + pyErrorAndReturn(out); +} + +static foreign_t assign_python(term_t exp, term_t name) { + term_t stackp = python_acquire_GIL(); + PyObject *e = term_to_python(exp, true, NULL, true); + + if (e == NULL) { + python_release_GIL(stackp); + pyErrorAndReturn(false); + } + bool b = python_assign(name, e, NULL); + python_release_GIL(stackp); + pyErrorAndReturn(b); +} + +static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) { + PyErr_Clear(); + PyObject *pI, *pArgs, *pOut; + PyObject *env; + atom_t name; + char *s; + int i, arity; + term_t targ = PL_new_term_ref(); + + if ((env = py_Builtin) == NULL) { + // no point in even trying + { pyErrorAndReturn(false); } + } + if (PL_get_name_arity(caller, &name, &arity)) { + if (!(s = PL_atom_chars(name))) { + pyErrorAndReturn(false); + } + if ((pI = PyObject_GetAttrString(env, s)) == NULL) { + PyErr_Print(); + { pyErrorAndReturn(false); } + } + Py_INCREF(pI); + } else { + // Prolog should make sure this never happens. + { 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); + } + /* ignore (_) */ + if (i == 0 && PL_is_variable(targ)) { + pArg = Py_None; + } else { + pArg = term_to_python(targ, true, NULL, true); + if (pArg == NULL) { + pyErrorAndReturn(false); + } + } + /* pArg reference stolen here: */ + if (PyTuple_SetItem(pArgs, i, pArg)) { + PyErr_Print(); + { pyErrorAndReturn(false); } + } + } + pOut = PyObject_CallObject(pI, pArgs); + Py_DECREF(pArgs); + Py_DECREF(pI); + if (pOut == NULL) { + PyErr_Print(); + { pyErrorAndReturn(false); } + } + { + foreign_t rc = address_to_term(pOut, out); + ; + pyErrorAndReturn(rc); + } +} + +static foreign_t python_access(term_t obj, term_t f, term_t out) { + PyErr_Clear(); + PyObject *o = term_to_python(obj, true, NULL, true), *pValue, *pArgs, *pF; + atom_t name; + char *s = NULL; + int i, arity; + term_t targ = PL_new_term_ref(); + + if (o == NULL) { + pyErrorAndReturn(false); + } + if (PL_is_atom(f)) { + if (!PL_get_atom_chars(f, &s)) { + pyErrorAndReturn(false); + } + if ((pValue = PyObject_GetAttrString(o, s)) == NULL) { + PyErr_Print(); + { pyErrorAndReturn(false); } + } + Py_INCREF(pValue); + { pyErrorAndReturn(python_to_term(pValue, out) ); } + } + if (!PL_get_name_arity(f, &name, &arity)) { + { pyErrorAndReturn(false); } + } + s = PL_atom_chars(name); + if (!s) { + pyErrorAndReturn(false); + } + if ((pF = PyObject_GetAttrString(o, s)) == NULL) { + DebugPrintf("Function %p\n", o); + PyErr_Print(); + { 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); + } + /* ignore (_) */ + if (i == 0 && PL_is_variable(targ)) { + pArgs = Py_None; + } + pArg = term_to_python(targ, true, NULL, true); + if (pArg == NULL) { + pyErrorAndReturn(false); + } + /* pArg reference stolen here: */ + PyTuple_SetItem(pArgs, i, pArg); + } + pValue = PyObject_CallObject(pF, pArgs); + Py_DECREF(pArgs); + Py_DECREF(pF); + if (pValue == NULL) { + { pyErrorAndReturn(false); } + } + { pyErrorAndReturn(python_to_term(pValue, out)); } +} + +static foreign_t python_field(term_t parent, term_t att, term_t tobj) { + PyObject *pF; + atom_t name; + char *s; + int arity; + + if (!PL_get_name_arity(att, &name, &arity)) { + { pyErrorAndReturn(false); } + } else { + PyObject *p; + + // got Scope.Exp + // get Scope ... + p = term_to_python(parent, true, NULL, true); + // Exp + if (!PL_get_name_arity(att, &name, &arity)) { + { pyErrorAndReturn(false); } + } + s = PL_atom_chars(name); + if (arity == 1 && !strcmp(s, "()")) { + if (!PL_get_arg(1, att, att)) { + pyErrorAndReturn(false); + } + if (!PL_get_name_arity(att, &name, &arity)) { + { pyErrorAndReturn(false); } + } + s = PL_atom_chars(name); + } + if (!s || !p) { + { pyErrorAndReturn(false); } + } else if ((pF = PyObject_GetAttrString(p, s)) == NULL) { + PyErr_Clear(); + { pyErrorAndReturn(false); } + } + } + { + foreign_t rc; + rc = address_to_term(pF, tobj); + pyErrorAndReturn(rc); + } +} + +static foreign_t python_main_module(term_t mod) { + { + foreign_t rc; + PyErr_Clear(); + rc = address_to_term(py_Main, mod); + pyErrorAndReturn(rc); + } +} + +static foreign_t python_function(term_t tobj) { + PyErr_Clear(); + PyObject *obj = term_to_python(tobj, true, NULL, true); + foreign_t rc = PyFunction_Check(obj); + + pyErrorAndReturn(rc); +} + +foreign_t python_builtin(term_t out) { + { + foreign_t rc; + PyErr_Clear(); + rc = address_to_term(py_Builtin, out); + pyErrorAndReturn(rc); + } +} + +static foreign_t python_run_file(term_t file) { + char *s; + size_t len; + char si[256]; + s = si; + PyErr_Clear(); + if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) { +#if PY_MAJOR_VERSION < 3 + PyObject *PyFileObject = PyFile_FromString(si, "r"); + PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "test.py", 1); +#else + FILE *f = fopen(s, "r"); + if (f == NULL) { + pyErrorAndReturn(false); + } + PyRun_SimpleFileEx(f, s, 1); +#endif + { + { pyErrorAndReturn(true); } + } + } + { pyErrorAndReturn(false); } +} + +extern PyThreadState *YAP_save; + +static foreign_t python_run_command(term_t cmd) { + char *s; + bool rc = false; + size_t len; + char si[256]; + + PyErr_Clear(); + s = si; + if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) { + PyRun_SimpleString(s); + rc = true; + } + pyErrorAndReturn(rc); +} + +static foreign_t python_run_script(term_t cmd, term_t fun) { + char si[256], sf[256]; + size_t len = 255, len1 = 255; + PyObject *pName, *pModule, *pFunc; + PyObject *pArgs = NULL, *pValue; + char *s; + + PyErr_Clear(); + s = si; + if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) && + (s = sf) != NULL && + PL_get_nchars(fun, &len1, &s, CVT_ALL | CVT_EXCEPTION)) { + +#if PY_MAJOR_VERSION < 3 + pName = PyString_FromString("rbm"); +#else + // asssumes UTF-8 + pName = PyUnicode_FromString("rbm"); +#endif + /* Error checking of pName left out */ + + pModule = PyImport_Import(pName); + PyErr_Clear(); + Py_DECREF(pName); + + if (pModule != NULL) { + pFunc = PyObject_GetAttrString(pModule, sf); + /* pFunc is a new reference */ + + if (pFunc && PyCallable_Check(pFunc)) { + pValue = PyObject_CallObject(pFunc, pArgs); + if (pValue != NULL) { + Py_DECREF(pValue); + } else { + Py_DECREF(pFunc); + Py_DECREF(pModule); + PyErr_Print(); + fprintf(stderr, "Call failed\n"); + { pyErrorAndReturn(false); } + } + } else { + pyErrorHandler(); + if (PyErr_Occurred()) + PyErr_Print(); + fprintf(stderr, "Cannot find function \"%s\"\n", sf); + } + Py_XDECREF(pFunc); + Py_DECREF(pModule); + } else { + PyErr_Print(); + { pyErrorAndReturn(false); } + } + { pyErrorAndReturn(true); } + } + { pyErrorAndReturn(false); } +} + +static foreign_t python_export(term_t t, term_t pl) { + foreign_t rc = false; + PyErr_Clear(); + if (PL_is_functor(t, FUNCTOR_pointer1)) { + void *ptr; + term_t targ = PL_new_term_ref(); + + if (!PL_get_arg(1, t, targ)) { + pyErrorAndReturn(false); + } + if (!PL_get_pointer(targ, &ptr)) { + pyErrorAndReturn(false); + } + Py_INCREF((PyObject *)ptr); + /* pyErrorAndReturn( __main__) */ + rc = python_to_term((PyObject *)ptr, pl); + } + pyErrorAndReturn(rc); +} + +/** + * @pred python_import(MName, Mod) + * Import a python module to the YAP environment. + * + * @param mname module name, either an atom or a sequence of atoms, + * eg os.sys + * @param mod the pointer to the Python object + * @return success? + */ +static int python_import(term_t mname, term_t mod) { + PyObject *pName; + bool do_as = false; + + term_t arg = PL_new_term_ref(); + char s0[MAXPATHLEN], *s = s0, *t; + functor_t f; + while (true) { + size_t len; + //PyErr_Clear(); + len = (MAXPATHLEN - 1) - (s - s0); + if (PL_is_pair(mname)) { + char *sa = NULL; + if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) || + !PL_get_arg(2, mname, mname)) { + pyErrorAndReturn(false); + } + PL_get_atom_chars(arg, &sa); + strcpy(s, sa); + s += strlen(s); + *s++ = '.'; + s[0] = '\0'; + } else if (PL_get_functor(mname, &f) && f == FUNCTOR_as2 && PL_get_arg(2, mname,arg) && + PL_get_atom_chars(arg, &t)) { + do_as = true; + PL_get_arg(1, mname,mname); + } else if (!PL_get_nchars(mname, &len, &s, + CVT_ATOM | CVT_EXCEPTION | REP_UTF8)) { + pyErrorAndReturn(false); + } else { + break; + } + } + term_t t0 = python_acquire_GIL(); +#if PY_MAJOR_VERSION < 3 + pName = PyString_FromString(s0); +#else + pName = PyUnicode_FromString(s0); +#endif + if (pName == NULL) { + python_release_GIL(t0); + pyErrorAndReturn(false); + } + + PyObject *pModule = PyImport_Import(pName); + + Py_XDECREF(pName); + if (pModule == NULL) { + python_release_GIL(t0); + + pyErrorAndReturn(false); + } + { + foreign_t rc = address_to_term(pModule, mod); + + if (do_as && PyObject_SetAttrString(py_Main, t, pModule) <0) + return false; + python_release_GIL(t0); + pyErrorAndReturn(rc); + } +} + +static foreign_t python_to_rhs(term_t inp, term_t t) { + PyObject *pVal; + PyErr_Clear(); + pVal = term_to_python(inp, true, NULL, true); + if (pVal == NULL) + pyErrorAndReturn(false); + pyErrorAndReturn(address_to_term(pVal, t)); +} + +// static PyThreadState *_saveP = NULL; +static bool _threaded = true; + +/* +static YAP_Int + p_python_ensure(term_t ptr) + { + PyGILState_STATE _tState = PyGILState_Ensure(); + pyErrorAndReturn( PL_unify_int64(ptr), false); + } + +static YAP_Int + p_python_release(term_t ptr) + { + + PyGILState_STATE _tState; + PL_get_int64( ptr, &_tState); + PyGILState_Release( _tState ); + pyErrorAndReturn( true); + } +*/ + +int _locked = 0; +PyThreadState *tstate; + +static YAP_Int p_python_threaded(void) { + + PyErr_Clear(); + // PyEval_ReleaseThread(tstate); + // _threaded = true; + // _locked = 0; + pyErrorAndReturn(true); +} + +static PyGILState_STATE gstates[64]; +static int gstatei = 0; + +term_t python_acquire_GIL(void) { + term_t curSlot = PL_new_term_ref(); + // extern int Yap_do_low_level_trace; + // Yap_do_low_level_trace = 1; + // fprintf( stderr, "++%d\n", ++_locked); + // if (_locked > 0) { _locked++ ; } + // else + 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) { + 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) { + 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); + PL_register_foreign("python_import", 2, python_import, 0); + PL_register_foreign("python_to_rhs", 2, python_to_rhs, 0); + PL_register_foreign("python_len", 2, python_len, 0); + PL_register_foreign("python_is", 2, python_is, 0); + PL_register_foreign("python_dir", 2, python_dir, 0); + PL_register_foreign("python_apply", 4, python_apply, 0); + PL_register_foreign("python_index", 3, python_index, 0); + PL_register_foreign("python_field", 3, python_field, 0); + PL_register_foreign("python_assign", 2, assign_python, 0); + PL_register_foreign("python_export", 2, python_export, 0); + PL_register_foreign("python_function", 1, python_function, 0); + PL_register_foreign("python_slice", 4, python_slice, 0); + PL_register_foreign("python_run_file", 1, python_run_file, 0); + PL_register_foreign("python_proc", 1, python_proc, 0); + PL_register_foreign("python_run_command", 1, python_run_command, 0); + PL_register_foreign("python_run_script", 2, python_run_script, 0); + PL_register_foreign("python_main_module", 1, python_main_module, 0); + PL_register_foreign("python_import", 2, python_import, 0); + PL_register_foreign("python_access", 3, python_access, 0); + PL_register_foreign("python_threaded", 0, p_python_threaded, 0); + + init_python_vfs(); +} diff --git a/packages/python/python.c b/packages/python/python.c index bcb1b53e6..f930a3d5b 100644 --- a/packages/python/python.c +++ b/packages/python/python.c @@ -9,7 +9,7 @@ atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t, ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V, ATOM_self, ATOM_nil, ATOM_brackets, ATOM_curly_brackets; -functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1, +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, @@ -68,6 +68,7 @@ static void install_py_constants(void) { FUNCTOR_abs1 = PL_new_functor(PL_new_atom("abs"), 1); FUNCTOR_all1 = PL_new_functor(PL_new_atom("all"), 1); FUNCTOR_any1 = PL_new_functor(PL_new_atom("any"), 1); + FUNCTOR_as2 = PL_new_functor(PL_new_atom("as"), 2); FUNCTOR_bin1 = PL_new_functor(PL_new_atom("bin"), 1); FUNCTOR_ord1 = PL_new_functor(PL_new_atom("ord"), 1); FUNCTOR_int1 = PL_new_functor(PL_new_atom("int"), 1); diff --git a/packages/python/python.pl b/packages/python/python.pl index d0b2e6b06..791d39dbc 100644 --- a/packages/python/python.pl +++ b/packages/python/python.pl @@ -34,8 +34,8 @@ op(100,fy,$), op(950,fy,:=), op(950,yfx,:=), - op(950,fx,<-), - op(950,yfx,<-), +% op(950,fx,<-), +% op(950,yfx,<-), op(50, yf, []), op(50, yf, '()'), op(100, xfy, '.'), @@ -112,8 +112,9 @@ Data types are :- multifile user:(:=)/2, user:(:=)/1, - user:(<-)/1, - user:(<-)/2, user:'()'/1, user:'{}'/1, user:dot_qualified_goal/2, user:import_arg/1. + % user:(<-)/1, + % user:(<-)/2, + user:'()'/1, user:'{}'/1, user:dot_qualified_goal/2, user:import_arg/1. import( F ) :- catch( python:python_import(F), _, fail ). @@ -139,11 +140,13 @@ user:(:= F) :- catch( python:python_proc(F), _, fail ). user:( V := F ) :- python:python_assign(F, V). +/* user:(<- F) :- catch( python:python_proc(F), _, fail ). user:(V <- F) :- V := F. +*/ python:python_import(Module) :- python:python_import(Module, _). diff --git a/packages/python/swig/prolog/yapi.yap b/packages/python/swig/prolog/yapi.yap index d20f7a60a..39a2aea97 100644 --- a/packages/python/swig/prolog/yapi.yap +++ b/packages/python/swig/prolog/yapi.yap @@ -1,3 +1,4 @@ + %% @file yapi.yap %% @brief support yap shell %% @@ -21,7 +22,9 @@ :- reexport( library(python) ). :- python_import(yap4py.yapi). -:- python_import(gc). +%:- python_import(gc). + +:- meta_predicate( yapi_query(:,+) ). %:- start_low_level_trace. diff --git a/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py b/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py index a3273a6b3..8cc8b22b6 100644 --- a/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py +++ b/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py @@ -2661,7 +2661,6 @@ class InteractiveShell(SingletonConfigurable): result : :class:`ExecutionResult` """ - print("go") result = None try: # import trace diff --git a/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap b/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap index bf26c388d..e48564a62 100644 --- a/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap +++ b/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap @@ -62,16 +62,18 @@ blankc('\t'). streams(false) :- +nb_setval(jupyter_cell, false), flush_output, forall( stream_property( S, mode(_) ), close(S) ). streams(true) :- -% open('/python/input', read, _Input, [alias(user_input),bom(false)]), + nb_setval(jupyter_cell, true), + open('/python/input', read, _Input, [alias(user_input),bom(false)]), open('/python/sys.stdout', append, _Output, [alias(user_output)]), open('/python/sys.stderr', append, _Error, [alias(user_error)]), -% set_prolog_flag(user_input,_Input), + set_prolog_flag(user_input,_Input), set_prolog_flag(user_output,_Output), set_prolog_flag(user_error,_Error). diff --git a/packages/python/yap_kernel/yap_ipython/yapi.py b/packages/python/yap_kernel/yap_ipython/yapi.py index 47ef152f4..4aca0e5f2 100644 --- a/packages/python/yap_kernel/yap_ipython/yapi.py +++ b/packages/python/yap_kernel/yap_ipython/yapi.py @@ -555,9 +555,6 @@ class YAPRun: self.bindings += [answer] self.iterations += 1 if stop and howmany == self.iterations: - self.query.close() - self.query = None - self.os = None return True, self.bindings if self.query.port == "exit": self.query.close() @@ -565,13 +562,13 @@ class YAPRun: self.os = None sys.stderr.writeln('Done, with', self.bindings) return True,self.bindings + if self.bindings: + sys.stderr.write('Done, with', self.bindings, '\n') + else: self.query.close() self.query = None self.os = None - if self.bindings: - sys.stderr.write('Done, with', self.bindings, '\n') - else: - sys.stderr.write('Fail\n') + sys.stderr.write('Fail\n') return True,{} except Exception as e: has_raised = True @@ -614,7 +611,7 @@ class YAPRun: # vs is the list of variables # you can print it out, the left-side is the variable name, # the right side wraps a handle to a variable - # pdb.set_trace() + #import pdb; pdb.set_trace() # #pdb.set_trace() # atom match either symbols, or if no symbol exists, strings, In this case # variable names should match strings @@ -655,19 +652,6 @@ 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 - # else: - # if False and len(cell.splitlines()) == 1: - # # Dynamic transformations - only applied for single line commands - # with self.shell.builtin_trap: - # try: - # # use prefilter_lines to handle trailing newlines - # # restore trailing newline for ast.parse - # cell = self.shell.prefilter_manager.prefilter_lines(cell) + '\n' - # except Exception: - # # don't allow prefilter errors to crash IPython - # preprocessing_exc_tuple = sys.exc_info() - - for i in self.syntaxErrors(raw_cell): try: (what,lin,_,text) = i @@ -683,18 +667,17 @@ class YAPRun: if not silent: self.shell.logger.log(cell, raw_cell) # # Display the exception if input processing failed. - # if preprocessing_exc_tuple is not None: - # self.showtraceback(preprocessing_exc_tuple) - # if store_history: - # self.shell.execution_count += 1 - # return error_before_exec(preprocessing_exc_tuple[2]) + if preprocessing_exc_tuple is not None: + self.showtraceback(preprocessing_exc_tuple) + if store_history: + self.shell.execution_count += 1 + return 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) - if cell[0] == '%': if cell[1] == '%': linec = False @@ -709,15 +692,15 @@ class YAPRun: line = txt[1] else: line = "" - if len(txt0) == 2: - cell = txt0[1] - else: - cell = "" if linec: self.shell.run_line_magic(magic, line) else: - print(txt0[1]) - self.shell.run_cell_magic(magic, line, txt0[1]) + print("txt0: ",txt0,"\n") + if len(txt0) == 1: + cell = "" + else: + body = txt0[1]+'\n'+txt0[2] + self.shell.run_cell_magic(magic, line, body) cell = "" # Give the displayhook a reference to our ExecutionResult so it # can fill in the output value. @@ -729,16 +712,16 @@ class YAPRun: if cell.strip('\n \t'): #create a Trace object, telling it what to ignore, and whether to # do tracing or line-counting or both. - tracer = trace.Trace( - #ignoredirs=[sys.prefix, sys.exec_prefix], - trace=1, - count=0) - - def f(self, cell): - self.jupyter_query( cell ) + # tracer = trace.Trace( + # #ignoredirs=[sys.prefix, sys.exec_prefix], + # trace=1, + # count=0) + # + # def f(self, cell): + # self.jupyter_query( cell ) # run the new command using the given tracer - # + # try: self.yapeng.mgoal(streams(True),"user") #state = tracer.runfunc(f,self,cell) @@ -746,7 +729,7 @@ class YAPRun: self.yapeng.mgoal(streams(False),"user") except Exception as e: has_raised = True - self.yapeng.mgoal(streams("off"),"user") + self.yapeng.mgoal(streams("off"),"user") if state: self.shell.last_execution_succeeded = True self.result.result = (True, dicts)