This commit is contained in:
Vitor Santos Costa 2018-12-12 00:39:17 +00:00
parent 7e84455117
commit 473132c3ec
4 changed files with 26 additions and 37 deletions

View File

@ -11,7 +11,7 @@ YAP_Term TermErrStream, TermOutStream;
static void pyflush(StreamDesc *st) { static void pyflush(StreamDesc *st) {
#if 0 #if 0
st->u.w_irl.ptr[0] = '\0'; st->u.w_irl.ptr[0] = '\0';
fprintf(stderr,"%s\n", st->u.w_irl.buf); // fprintf(stderr,"%s\n", st->u.w_irl.buf);
term_t tg = python_acquire_GIL(); term_t tg = python_acquire_GIL();
if (st->user_name == TermOutStream){ if (st->user_name == TermOutStream){
PySys_WriteStdout("%s", st->u.w_irl.buf); PySys_WriteStdout("%s", st->u.w_irl.buf);

View File

@ -73,8 +73,7 @@ python_query( Caller, String ) :-
query_to_answer( Goal, VarNames, Status, Bindings), query_to_answer( Goal, VarNames, Status, Bindings),
Caller.port := Status, Caller.port := Status,
write_query_answer( Bindings ), write_query_answer( Bindings ),
nl(user_error), nl(user_error),
Caller.answer := {},
maplist(in_dict(Caller.answer, Bindings), Bindings). maplist(in_dict(Caller.answer, Bindings), Bindings).
/** /**

View File

@ -522,7 +522,7 @@ class YAPRun(InteractiveShell):
self.query = None self.query = None
self.os = None self.os = None
self.it = None self.it = None
self.port = None self.port = "None"
self.answers = None self.answers = None
self.bindings = dicts = [] self.bindings = dicts = []
self.shell.yapeng = self.yapeng self.shell.yapeng = self.yapeng
@ -566,42 +566,37 @@ class YAPRun(InteractiveShell):
self.query.close() self.query.close()
self.query = None self.query = None
self.port = None self.port = None
self.answers = None self.answers = []
self.os = program+squery self.os = program+squery
self.iterations = 0 self.iterations = 0
pg = jupyter_query( self, program, squery) pg = jupyter_query( self, program, squery)
self.query = self.yapeng.query(pg) self.query = self.yapeng.query(pg)
self.answers = [] self.answers = []
self.port = "call" self.port = "call"
found = False
self.answer = {} self.answer = {}
while self.query.next(): while self.query.next():
#sys.stderr.write('B '+str( self.answer) +'\n') #sys.stderr.write('B '+str( self.answer) +'\n')
#sys.stderr.write('C '+ str(self.port) +'\n'+'\n') #sys.stderr.write('C '+ str(self.port) +'\n'+'\n')
found = True found = True
print( "uek",self.answer )
self.answers += [self.answer] self.answers += [self.answer]
print( "ek",self.answers )
self.iterations += 1 self.iterations += 1
if self.port == "exit": if self.port == "exit" or stop or howmany == self.iterations:
self.os = None self.os = None
#sys.stderr.write('Done, with'+str(self.answers)+'\n') self.query.close()
result.result = True,self.bindings self.query = None
if found:
sys.stderr.write('Completed, with '+str(self.answers)+'\n')
result.result = self.answers
return result return result
if stop or howmany == self.iterations:
result.result = True, self.answers
return result
if found:
sys.stderr.write('Done, with '+str(self.answers)+'\n')
else:
self.os = None
self.query.close()
self.query = None
sys.stderr.write('Fail\n')
result.result = True,self.bindings
return result
except Exception as e: except Exception as e:
sys.stderr.write('Exception '+str(e)+'in query '+ str(self.query)+ sys.stderr.write('Exception '+str(e)+'in query '+ str(self.query)+
':'+pg+'\n '+str( self.bindings)+ '\n') ':'+pg+'\n '+str( self.bindings)+ '\n')
has_raised = True has_raised = True
result.result = False result.result = []
return result return result
@ -698,8 +693,7 @@ class YAPRun(InteractiveShell):
except: except:
line = "" line = ""
self.shell.last_execution_succeeded = True self.shell.last_execution_succeeded = True
self.shell.run_cell_magic(magic, line, body) result.result = self.shell.run_cell_magic(magic, line, body)
result.result = True
return return
else: else:
linec = True linec = True
@ -720,7 +714,7 @@ class YAPRun(InteractiveShell):
# can fill in the output value. # can fill in the output value.
self.shell.displayhook.exec_result = result self.shell.displayhook.exec_result = result
if self.syntaxErrors(cell): if self.syntaxErrors(cell):
result.result = False result.result = []
return return
has_raised = False has_raised = False
try: try:
@ -745,11 +739,10 @@ class YAPRun(InteractiveShell):
self.jupyter_query( cell ) self.jupyter_query( cell )
# state = tracer.runfunc(jupyter_query( self, cell ) ) # state = tracer.runfunc(jupyter_query( self, cell ) )
self.shell.last_execution_succeeded = True self.shell.last_execution_succeeded = True
result.info += [self.answer] result.result = []
result.result = True
except Exception as e: except Exception as e:
has_raised = True has_raised = True
result.result = False result.result = []
try: try:
(etype, value, tb) = e (etype, value, tb) = e
traceback.print_exception(etype, value, tb) traceback.print_exception(etype, value, tb)
@ -774,7 +767,7 @@ class YAPRun(InteractiveShell):
self.shell.execution_count += 1 self.shell.execution_count += 1
self.yapeng.mgoal(streams(False),"user", True) self.yapeng.mgoal(streams(False),"user", True)
return result.result return
def clean_end(self,s): def clean_end(self,s):
""" """
@ -809,7 +802,7 @@ class YAPRun(InteractiveShell):
def prolog_cell(self,s): def prolog_cell(self,s):
""" """
Trasform a text into program+query. A query is the Trasform a text into program+query. A query is the
last line if the last line is non-empty and does not terminate last line if the last line is non-empty and does not terminate

View File

@ -494,9 +494,6 @@ write_query_answer( Bindings ) :-
'$write_goal_output'(G1, First, NG, Next, IG), '$write_goal_output'(G1, First, NG, Next, IG),
'$write_vars_and_goals'(LG, Next, IG). '$write_vars_and_goals'(LG, Next, IG).
'$goal_to_string'(Format, G, String) :-
format(codes(String),Format,G).
'$write_goal_output'(var([V|VL]), First, [var([V|VL])|L], next, L) :- !, '$write_goal_output'(var([V|VL]), First, [var([V|VL])|L], next, L) :- !,
( First = first -> true ; format(user_error,',~n',[]) ), ( First = first -> true ; format(user_error,',~n',[]) ),
format(user_error,'~a',[V]), format(user_error,'~a',[V]),
@ -516,14 +513,14 @@ write_query_answer( Bindings ) :-
G = [_|_], !, G = [_|_], !,
% dump on string first so that we can check whether we actually % dump on string first so that we can check whether we actually
% had any output from the solver. % had any output from the solver.
'$goal_to_string'(Format, G, String), term_to_string( G, String),
( String == [] -> ( String == `` ->
% we didn't % we didn't
IG = NG, First = Next IG = NG, First = Next
; ;
% we did % we did
( First = first -> true ; format(user_error,',~n',[]) ), ( First = first -> true ; format(user_error,',~n',[]) ),
format(user_error, '~s', [String]), format(user_error, '~N~s', [String]),
NG = [G|IG] NG = [G|IG]
). ).
'$write_goal_output'(_-G, First, [G|NG], next, NG) :- !, '$write_goal_output'(_-G, First, [G|NG], next, NG) :- !,