From 4a5002091f32dbf5eb835b85b4c17259c5538f90 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Thu, 14 Dec 2017 18:40:22 +0000 Subject: [PATCH] tmp --- CMakeLists.txt | 2 +- packages/python/pypreds.c | 2 +- packages/python/python.c | 5 +- .../yap_ipython/core/interactiveshell.py | 79 ++++++++++++++----- .../yap_kernel/yap_ipython/prolog/jupyter.yap | 27 +++++-- 5 files changed, 87 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15704642f..6a629599a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -227,7 +227,7 @@ ADD_CUSTOM_TARGET(run_install COMMAND ${CMAKE_MAKE_PROGRAM} install) if (APPLE) set(MACOSX_RPATH ON) - if (NOT ANACONDA) + if (NOT $ENV{CONDA_BUILD}x STREQUAL "1x") set(PATH $ENV{PATH}) list(FIND ${PATH} pos /usr/local) if (pos EQUAL -1) diff --git a/packages/python/pypreds.c b/packages/python/pypreds.c index 361270761..4eef7f4f2 100644 --- a/packages/python/pypreds.c +++ b/packages/python/pypreds.c @@ -266,7 +266,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, false); } + { pyErrorAndReturn(false, false); } } { foreign_t rc = address_to_term(pOut, out); diff --git a/packages/python/python.c b/packages/python/python.c index c7337a456..489ac1f50 100644 --- a/packages/python/python.c +++ b/packages/python/python.c @@ -66,10 +66,13 @@ static int py_put(int sno, int ch) { char s[2]; StreamDesc *st = YAP_GetStreamFromId(sno); // PyUnicode_WRITE(pyw_kind, pyw_data, 0, ch); - PyObject *fput = PyObject_GetAttrString(st->u.private_data, "write"); + PyObject *err,*fput = PyObject_GetAttrString(st->u.private_data, "write"); s[0] = ch; s[1] = '\0'; PyObject_CallFunctionObjArgs(fput, PyUnicode_FromString(s), NULL); +if ((err = PyErr_Occurred())) { + PyErr_SetString(err, "Error in put\n");// %s:%s:%d!\n", __FILE__, __FUNCTION__, __LINE__); +} return ch; } diff --git a/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py b/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py index b5ddae5ad..2dbc81201 100644 --- a/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py +++ b/packages/python/yap_kernel/yap_ipython/core/interactiveshell.py @@ -23,7 +23,7 @@ library = namedtuple('library', 'list') v = namedtuple('_', 'slot') load_files = namedtuple('load_files', 'file ofile args') python_query= namedtuple('python_query', 'query_mgr string') -jupyter_query = namedtuple('jupyter_query', 'self string') +jupyter_query = namedtuple('jupyter_query', 'self text query') enter_cell = namedtuple('enter_cell', 'self' ) exit_cell = namedtuple('exit_cell', 'self' ) completions = namedtuple('completions', 'txt self' ) @@ -325,7 +325,6 @@ class YAPInteractive(InteractiveShell): # Reset this so later displayed values do not modify the # ExecutionResult self.displayhook.exec_result = None - print(self.complete) self.events.trigger('post_execute') if not silent: self.events.trigger('post_run_cell') @@ -339,6 +338,45 @@ class YAPInteractive(InteractiveShell): return result + def prolog_cell(self,s): + """" + Trasform a text into program+query. A query is the + last line if the last line is non-empty and does not terminate + on a dot. You can also finish with + + - `*`: you request all solutions + - '^': you want to check if there is an answer + - '?'[N]: you want an answer; optionally you want N answers + + If the line terminates on a `*/` or starts on a `%` we assume the line + is a comment. + """ + s = s.rstrip() + take = 0 + its = 0 + [program,x,query] = s.partition('\n') + if query == '': + query = program + while take < len(query): + take += 1 + ch = query[-take] + if ch.isdigit(): + its = its + ord(ch) - ord('0') + elif ch == '*' and take == 1: + return program, query[:-take], -1 + elif ch == '.' and take == 1: + return s, '', 1 + elif ch == '/' and query[-2] == '*' and take == 1: + return program, query[:-take], -1 + elif ch == '^' and take == 1: + return program, query[:-take], 1 + elif ch == '?': + return program, query[:-take], its+1 + else: + return program, query, 1 + return s, '', 1 + + def jupyter_query(self, s): # import pdb; pdb.set_trace() # @@ -350,7 +388,9 @@ class YAPInteractive(InteractiveShell): self.q.close() self.q = None if not self.q: - self.q = self.yapeng.query(jupyter_query(self, s)) + import pdb; pdb.set_trace() + program,query,self.iterations = self.prolog_cell(s) + self.q = self.yapeng.query(jupyter_query(self, program, query)) self.os = s # vs is the list of variables # you can print it out, the left-side is the variable name, @@ -365,21 +405,24 @@ class YAPInteractive(InteractiveShell): # return # ask = True # launch the query - # run the new command using the given tracer - rc = self.answer(self.q) - if rc: - # deterministic = one solution - if self.port == "exit": - # done - self.q.close() - self.q = None - self.os = "" - print("yes") - return True, self.bindings - print("No (more) answers") - self.q.close() - self.q = None - return True, None + # run the new commbnand using the given tracer + solutions = [] + while self.iterations > 0: + self.iterations -= 1 + rc = self.answer(self.q) + if rc: + # deterministic = one solution + if self.port == "exit": + # done + self.q.close() + self.q = None + self.os = "" + print("yes") + solutions += [self.bindings] + print("No (more) answers") + self.q.close() + self.q = None + return True, solutions def answer(self, q): try: diff --git a/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap b/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap index 14c5f4c0f..64feff480 100644 --- a/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap +++ b/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap @@ -6,18 +6,31 @@ :- python_import(sys). -jupyter_query(Self, Cell) :- +:- start_low_level_trace. + +user:jupyter_query(Self, Cell, Line ) :- setup_call_cleanup( enter_cell(Self), - python_query(Self, Cell), - exit_cell(Self) - ). + jupyter_cell(Self, Cell, Line), + exit_cell(Self) ). + +jupyter_cell(_Self, Cell, _) :- + open_mem_read_stream( Cell, Stream), + load_files(['jupyter cell'],[stream(Stream)]), + close( Stream ), + fail. +jupyter_cell( Self, _, Line ) :- + python_query( Self, Line ). enter_cell(_Self) :- - open('//python/sys.stdout', append, _Output, [alias(jupo)]), - open('//python/sys.stdout', append, _Error, [alias(jupe)]), + open('//python/sys.stdout', append, _Output, []), + open('//python/sys.stdout', append, _Error, []), set_prolog_flag(user_output, _Output), - set_prolog_flag(user_error, _Error). + set_prolog_flag(user_error, _Error), + writeln(hello), + format(user_error,'h~n',[]), + := print("py"), + := sys.stderr.write("ok\n"). exit_cell(_Self) :- close( user_output),