From 260dcbe754d21c8f8abdc228cc792721f9ec8cca Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 6 Jul 2018 16:51:19 +0100 Subject: [PATCH] python --- C/flags.c | 2 +- H/YapFlags.h | 6 ++-- os/alias.c | 29 ++++++++++++------- os/iopreds.c | 1 + os/streams.c | 3 +- packages/python/CMakeLists.txt | 2 +- packages/python/pl2py.c | 15 +++++----- packages/python/pybips.c | 10 ++++--- packages/python/pypreds.c | 9 ++++-- .../python/yap_kernel/yap_ipython/yapi.py | 9 +++--- pl/qly.yap | 1 - 11 files changed, 50 insertions(+), 37 deletions(-) diff --git a/C/flags.c b/C/flags.c index 1a3f588b0..5711565a0 100644 --- a/C/flags.c +++ b/C/flags.c @@ -112,7 +112,7 @@ static Term indexer(Term inp) { return TermZERO; } Yap_Error(TYPE_ERROR_ATOM, inp, - "set_prolog_flag in {dec10,error,fail,quiet}"); + "set_prolog_flag index to an atom"); return TermZERO; } diff --git a/H/YapFlags.h b/H/YapFlags.h index 0207fd91b..ffadb6d5b 100644 --- a/H/YapFlags.h +++ b/H/YapFlags.h @@ -144,12 +144,12 @@ static Term synerr(Term inp) { return inp; if (IsAtomTerm(inp)) { - Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, inp, + Yap_ThrowError(DOMAIN_ERROR_OUT_OF_RANGE, inp, "set_prolog_flag in {dec10,error,fail,quiet}"); return TermZERO; } - Yap_Error(TYPE_ERROR_ATOM, inp, - "set_prolog_flag in {dec10,error,fail,quiet}"); + Yap_ThrowError(TYPE_ERROR_ATOM, inp, + "syntax_error flag must be atom"); return TermZERO; } diff --git a/os/alias.c b/os/alias.c index 430e1108b..6b8534bb6 100644 --- a/os/alias.c +++ b/os/alias.c @@ -221,16 +221,16 @@ Yap_SetAlias (Atom arg, int sno) CACHE_REGS AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases; - while (aliasp < aliasp_max) { - // replace alias - if (aliasp->name == arg) { - aliasp->alias_stream = sno; if (arg == AtomUserIn) LOCAL_c_input_stream = sno; if (arg == AtomUserOut) LOCAL_c_output_stream = sno; if (arg == AtomUserErr) LOCAL_c_error_stream = sno; + while (aliasp < aliasp_max) { + // replace alias + if (aliasp->name == arg) { + aliasp->alias_stream = sno; return; } aliasp++; @@ -317,16 +317,16 @@ ExistsAliasForStream (int sno, Atom al) while (aliasp < aliasp_max) { if (aliasp->alias_stream == sno && aliasp->name == al) { if (al == AtomUserIn) { - LOCAL_c_input_stream = StdInStream; - aliasp->alias_stream = StdInStream; - } + LOCAL_c_input_stream = sno; + aliasp->alias_stream = sno; + } else if (al == AtomUserOut) { - LOCAL_c_output_stream = StdOutStream; - aliasp->alias_stream = StdOutStream; + LOCAL_c_output_stream = sno; + aliasp->alias_stream = sno; } if (al == AtomUserErr) { - LOCAL_c_error_stream = StdErrStream; - aliasp->alias_stream = StdErrStream; + LOCAL_c_error_stream = sno; + aliasp->alias_stream = sno; } return true; } @@ -387,6 +387,12 @@ Yap_AddAlias (Atom arg, int sno) AliasDesc aliasp = GLOBAL_FileAliases, aliasp_max = GLOBAL_FileAliases+GLOBAL_NOfFileAliases; + if (arg == AtomUserIn) + LOCAL_c_input_stream = sno; + else if (arg == AtomUserOut) + LOCAL_c_output_stream = sno; + else if (arg == AtomUserErr) + LOCAL_c_error_stream = sno; while (aliasp < aliasp_max) { if (aliasp->name == arg) { aliasp->alias_stream = sno; @@ -394,6 +400,7 @@ Yap_AddAlias (Atom arg, int sno) } aliasp++; } + /* we have not found an alias neither a hole */ if (aliasp == GLOBAL_FileAliases+GLOBAL_SzOfFileAliases) ExtendAliasArray(); diff --git a/os/iopreds.c b/os/iopreds.c index 0918e2db0..9cea702d5 100644 --- a/os/iopreds.c +++ b/os/iopreds.c @@ -1129,6 +1129,7 @@ static void check_bom(int sno, StreamDesc *st) { bool Yap_initStream(int sno, FILE *fd, const char *name, const char *io_mode, Term file_name, encoding_t encoding, stream_flags_t flags, void *vfs) { + fprintf(stderr,"+ %s --> %d\n", name, sno); StreamDesc *st = &GLOBAL_Stream[sno]; __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "init %s %s:%s stream <%d>", io_mode, CurrentModule == 0? "prolog": RepAtom(AtomOfTerm(CurrentModule))->StrOfAE, diff --git a/os/streams.c b/os/streams.c index 0286ee460..b0748e835 100644 --- a/os/streams.c +++ b/os/streams.c @@ -998,7 +998,8 @@ static void CloseStream(int sno) { // __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "close stream <%d>", // sno); VFS_t *me; - if ((me = GLOBAL_Stream[sno].vfs) != NULL && + fprintf( stderr, "- %d\n",sno); +if ((me = GLOBAL_Stream[sno].vfs) != NULL && GLOBAL_Stream[sno].file == NULL) { if (me->close) { me->close(sno); diff --git a/packages/python/CMakeLists.txt b/packages/python/CMakeLists.txt index 17787b257..1314dca1e 100644 --- a/packages/python/CMakeLists.txt +++ b/packages/python/CMakeLists.txt @@ -5,7 +5,7 @@ set (PYTHON_SOURCES python.c pl2py.c pybips.c py2pl.c pl2pl.c pypreds.c pyio.c) set (PYTHON_HEADERS py4yap.h) set (CMAKE_POSITION_INDEPENDENT_CODE TRUE) -include_directories( ${CMAKE_BINARY_DIR} ${PYTHON_INCLUDE_DIRS} +include_directories( BEFORE ${PYTHON_INCLUDE_DIRS} ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/os ) #talk to python.pl diff --git a/packages/python/pl2py.c b/packages/python/pl2py.c index 65e12dacf..167df5679 100644 --- a/packages/python/pl2py.c +++ b/packages/python/pl2py.c @@ -112,7 +112,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) { // Yap_DebugPlWriteln(yt); switch (PL_term_type(t)) { case PL_VARIABLE: { - if (t == 0) { + if (yt == 0) { Yap_ThrowError(SYSTEM_ERROR_INTERNAL, yt, "in term_to_python"); } PyObject *out = PyTuple_New(1); @@ -148,21 +148,20 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) { } else if (YAP_IsStringTerm(yt)) { s = YAP_StringOfTerm(yt); } else { - return CHECKNULL(t, NULL); + return CHECKNULL(t, NULL); } + PyObject *pobj = PyUnicode_FromString(s); + #if PY_MAJOR_VERSION < 3 if (proper_ascii_string(s)) { PyObject *o = PyString_FromStringAndSize(s, strlen(s)); return CHECKNULL(t, o); - } else + } #endif - { // char *p = malloc(strlen(s)+1); // strcpy(p, s); - PyObject *pobj = PyUnicode_FromString(s); Py_IncRef(pobj); return CHECKNULL(t, pobj); - } } break; case PL_INTEGER: { int64_t j; @@ -406,8 +405,8 @@ PyObject *deref_term_to_python(term_t t) { // am\n"); YAP_Term yt = YAP_GetFromSlot(t); if (YAP_IsVarTerm(yt)) { - char s[32]; - char *o = YAP_WriteBuffer(yt, s, 31, 0); + char b[1024]; + char *o = YAP_WriteBuffer(yt, b, 1023, 0); PyObject *p = PyUnicode_FromString(o); return p; } diff --git a/packages/python/pybips.c b/packages/python/pybips.c index 8874f731e..ecf7ba072 100644 --- a/packages/python/pybips.c +++ b/packages/python/pybips.c @@ -953,10 +953,12 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) { if (!PL_get_arg(1, t, targ)) return NULL; + // Yap_DebugPlWriteln(YAP_GetFromSlot(t)); lhs = term_to_python(targ, true, NULL, true); AOK(PL_get_arg(2, t, targ), NULL); rhs = term_to_python(targ, true, NULL, true); - if (PySequence_Check(lhs) && PySequence_Check(rhs)) { + Yap_DebugPlWriteln(YAP_GetFromSlot(targ)); + if (PySequence_Check(lhs) && PySequence_Check(rhs)) { return PySequence_Concat(lhs, rhs); } if (!PyNumber_Check(lhs)) @@ -1034,11 +1036,11 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) { PyDict_SetItem(pyDict, key, val); } else { indict = false; - pArgs = PyTuple_New(i); + pArgs = PyTuple_New(i); } } - DebugPrintf("Tuple %p\n", pyDict); - if (!indict) { + fprintf(stderr,"Tuple %p: %s\n", pyDict, PyUnicode_AsUTF8(PyObject_Str(pyDict))); + if (!indict) { if (PL_is_variable(tleft)) { pArg = Py_None; } else { diff --git a/packages/python/pypreds.c b/packages/python/pypreds.c index f7be0bb09..835835f64 100644 --- a/packages/python/pypreds.c +++ b/packages/python/pypreds.c @@ -5,9 +5,12 @@ 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 detcted at %s %s:%d\n\n", code, file, line); - PyErr_Print(); -} + //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))); + }; static foreign_t python_len(term_t tobj, term_t tf) { Py_ssize_t len; diff --git a/packages/python/yap_kernel/yap_ipython/yapi.py b/packages/python/yap_kernel/yap_ipython/yapi.py index 8169da61a..16063ea3d 100644 --- a/packages/python/yap_kernel/yap_ipython/yapi.py +++ b/packages/python/yap_kernel/yap_ipython/yapi.py @@ -709,6 +709,7 @@ class YAPRun: self.shell.displayhook.exec_result = self.result has_raised = False try: + self.yapeng.mgoal(streams(True),"user", True) self.bindings = dicts = [] if cell.strip('\n \t'): #create a Trace object, telling it what to ignore, and whether to @@ -725,16 +726,16 @@ class YAPRun: # run the new command using the given tracer # # tracer.runfunc(f,self,cell,state) - self.yapeng.mgoal(streams(True),"user", True) self.jupyter_query( cell ) - self.yapeng.mgoal(streams(False),"user", True) # state = tracer.runfunc(jupyter_query( self, cell ) ) self.shell.last_execution_succeeded = True self.result.result = (True, dicts) - + self.yapeng.mgoal(streams(False),"user", True) + except Exception as e: has_raised = True self.result.result = False + self.yapeng.mgoal(streams(False),"user", True) self.shell.last_execution_succeeded = not has_raised @@ -771,7 +772,7 @@ class YAPRun: else: taken = l0-(i-1) n = s[i+1:].strip() - s = s[:i-1] + s = s[:i] if n: its = 0 for ch in n: diff --git a/pl/qly.yap b/pl/qly.yap index 78433dc28..ec948ecbe 100755 --- a/pl/qly.yap +++ b/pl/qly.yap @@ -236,7 +236,6 @@ qend_program :- prolog_flag_property(X, [access(read_write)]), atom(X), yap_flag(X, V), -writeln(X:V), X \= gc_margin, % different machines will have different needs, X \= argv, X \= os_argv,