jupyter-related-fixes

This commit is contained in:
Vitor Santos Costa
2018-07-10 23:21:19 +01:00
parent 387d7cc3fb
commit 2e8dd92d89
28 changed files with 641 additions and 452 deletions

View File

@@ -4,6 +4,7 @@
* @brief JUpyter support.
*/
:- yap_flag(gc_trace,verbose).
% :- module( jupyter,
% [jupyter_query/3,
@@ -20,16 +21,11 @@
:- use_module(library(python)).
:- use_module(library(yapi)).
:- use_module(library(complete)).
:- use_module(library(verify)).
:- python_import(sys).
jupyter_query(Caller, Prog, Query ) :-
catch(
jupyter_cell(Caller, Prog, Query),
error(L,E),
system_error(L,E)
).
jupyter_query(Caller, Cell, Line ) :-
jupyter_cell(Caller, Cell, Line).
jupyter_cell(_Caller, Cell, _Line) :-
jupyter_consult(Cell), %stack_dump,
@@ -38,39 +34,162 @@ jupyter_cell( _Caller, _, '' ) :- !.
jupyter_cell( _Caller, _, Line ) :-
blank( Line ),
!.
jupyter_cell( Self, _, Line ) :-
%Self := Caller.query,
python_query(Self,Line).
jupyter_cell( Caller, _, Line ) :-
Self := Caller.query,
catch(
python_query(Self,Line),
E=error(A,B),
system_error(A,B)
).
jupyter_cell(_,_,_).
restreams(call) :-
streams(true).
restreams(fail) :-
streams(false).
restreams(answer).
restreams(exit) :-
streams(false).
restreams(!).
restreams(external_exception(_)).
restreams(exception).
jupyter_consult(Text) :-
blank( Text ),
!.
jupyter_consult(Cell) :-
open_mem_read_stream( Cell, Stream),
load_files(user:'jupyter cell',[stream(Stream)]).
blank(Text) :-
atom(Text),
!,
atom_codes(Text, L),
maplist( code_type(space), L).
blank(Text) :-
string(Text),
!,
string_codes(Text, L),
maplist( code_type(space), L).
% Name = 'Inp',
% stream_property(Stream, file_name(Name) ),
% setup_call_cleanup(
catch(
(
Options = [],
open_mem_read_stream( Cell, Stream),
load_files(user:'jupyter cell',[stream(Stream)| Options])
),
E=error(A,B),
(close(Stream), system_error(A,B))
),
fail.
jupyter_consult(_Cell).
blank(Text) :-
atom(Text),
!,
atom_codes(Text, L),
maplist( code_type(space), L).
blank(Text) :-
string(Text),
!,
string_codes(Text, L),
maplist( code_type(space), L).
streams(false) :-
nb_setval(jupyter_cell, false),
close(user_input),
close(user_output),
close(user_error).
streams(true) :-
nb_setval(jupyter_cell, true),
open('/python/input', read, _Input, [alias(user_input),bom(false),script(false)]),
open('/python/sys.stdout', append, _Output, [alias(user_output)]),
open('/python/sys.stderr', append, _Error, [alias(user_error)]).
open('/python/input', read, Input, [alias(user_input),bom(false),script(false)]),
open('/python/sys.stdout', append, Output, [alias(user_output)]),
open('/python/sys.stderr', append, Error, [alias(user_error)]).
ready(_Self, Line ) :-
blank( Line ),
!.
ready(Self, Line ) :-
errors( Self, Line ),
\+ syntax_error(_,_).
errors( Self, Text ) :-
setup_call_cleanup(
open_events( Self, Text, Stream),
goals(Self, Stream),
close_events( Self )
).
clauses(_Self, Stream) :-
repeat,
read_clause(Stream, Cl, [term_position(_Pos), syntax_errors(fail)] ),
% command( Self, Cl ),
Cl == end_of_file,
!.
goals(_Self, Stream) :-
repeat,
read_term(Stream, Cl, [term_position(_Pos), syntax_errors(fail)] ),
% command( Self, Cl ),
Cl == end_of_file,
!.
command(_, end_of_file) :- !.
command( _Self, ( :- op(Prio,Assoc,Name) ) ) :-
addop(Prio,Assoc,Name).
command( _Self, ( :- module(Name, Exports) )) :-
retract( active_module( M0 ) ),
atom_concat( '__m0_', Name, M ),
assert( active_module(M) ),
assert( undo( active_module(M0) ) ),
maplist( addop2(M), Exports).
addop(Prio,Assoc,Name) :-
(
current_op(OPrio, SimilarAssoc, Name),
op(Prio, Assoc, Name),
matched_op(Assoc, SimilarAssoc)
->
assertz( undo(op( OPrio, Assoc, Name ) ) )
;
assertz( undo(op( 0, Assoc, Name ) ) )
).
addop2(M, op(Prio, Assoc, Name)) :-
addop( Prio, Assoc, M:Name ).
matched_op(A, B) :-
optype( A, T),
optype( B, T).
optype(fx,pre).
optype(fy,pre).
optype(xfx,in).
optype(xfy,in).
optype(yfx,in).
optype(yfy,in).
optype(xf,pos).
optype(yf,pos).
:- dynamic user:portray_message/2.
:- multifile user:portray_message/2.
:- dynamic syntax_error/4, undo/1.
user:portray_message(_Severity, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
nb_getval(jupyter_cell, on),
assert( syntax_error(Cause,LN,CharPos,Details) ).
user:portray_message(_Severity, error(style_check(_),_) ) :-
nb_getval(jupyter_cell, on).
open_events(Self, Text, Stream) :-
Self.errors := [],
nb_setval( jupyter, on),
open_mem_read_stream( Text, Stream ).
:- initialization( nb_setval( jupyter, off ) ).
close_events( _Self ) :-
nb_setval( jupyter, off ),
retract( undo(G) ),
call(G),
fail.
close_events( Self ) :-
retract( syntax_error( C, L, N, A )),
Self.errors := [t(C,L,N,A)] + Self.errors,
fail.
close_events( _ ).
:- if( current_prolog_flag(apple, true) ).

View File

@@ -22,45 +22,41 @@ s % completion/2,
:- python_import(sys).
all_clear( Self, _Cell, P, Q) :-
no_errors( Self, P ),
yap_flag(singleton_variables, Old, false),
no_errors( Self, Q ),
yap_flag(singleton_variables, _, Old).
p_errors( Errors, Cell) :-
blank( Cell ),
!.
p_errors( Errors, Cell) :-
no_errors( Errors , Cell ).
no_errors( _Self, Text ) :-
no_errors( _Errors , Text ) :-
blank(Text).
no_errors( Self, Text ) :-
no_errors( Errors , Text ) :-
setup_call_cleanup(
open_esh( Self, Text, Stream),
esh(Self, Stream),
close_esh( Self, Stream )
open_esh( Errors , Text, Stream),
esh(Errors , Stream),
close_esh( Errors , Stream )
).
syntax(_Self, E) :- writeln(user_error, E), fail.
syntax(Self, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
Self.errors := [t(Cause,LN,CharPos,Details)] + Self.errors,
syntax(_Errors , E) :- writeln(user_error, E), fail.
syntax(Errors , error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
Errors.errors := [t(Cause,LN,CharPos,Details)] + Errors.errors,
!.
syntax(_Self, E) :- throw(E).
syntax(_Errors , E) :- throw(E).
open_esh(Self, Text, Stream) :-
Self.errors := [],
open_esh(_Errors , Text, Stream) :-
open_mem_read_stream( Text, Stream ).
esh(Self, Stream) :-
esh(Errors , Stream) :-
repeat,
catch(
catch(
read_clause(Stream, Cl, [term_position(_Pos), syntax_errors(fail)] ),
Error,
syntax(Self, Error)
syntax(Errors , Error)
),
Cl == end_of_file,
!,
V := Self.errors,
V == [].
!.
close_esh( _Self, Stream ) :-
close_esh( _Errors , Stream ) :-
close(Stream).

View File

@@ -23,11 +23,11 @@ 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 text p q')
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' )
errors = namedtuple('errors', 'self text p q' )
errors = namedtuple('errors', 'self text' )
streams = namedtuple('streams', ' text' )
nostreams = namedtuple('nostreams', ' text' )
@@ -114,7 +114,7 @@ class YAPInputSplitter(InputSplitter):
if not line:
line = text.rstrip()
self.errors = []
engine.mgoal(errors(self, text ,text,''),"user",True)
engine.mgoal(errors(self, line),"user",True)
return self.errors != []
@@ -510,17 +510,16 @@ class YAPRun:
def __init__(self, shell):
self.shell = shell
self.yapeng = Engine()
self.yapeng = JupyterEngine()
global engine
engine = self.yapeng
self.yapeng.goal(use_module(library("jupyter")),True)
self.query = None
self.os = None
self.it = None
self.shell.yapeng = self.yapeng
self._get_exc_info = shell._get_exc_info
def syntaxErrors(self, text,program,query):
def syntaxErrors(self, text):
"""Return whether a legal query
"""
if not text:
@@ -528,7 +527,8 @@ class YAPRun:
if text == self.os:
return self.errors
self.errors=[]
self.yapeng.mgoal(errors(self,text,program,query),"user",True)
(text,_,_,_) = self.clean_end(text)
self.yapeng.mgoal(errors(self,text),"user",True)
return self.errors
def jupyter_query(self, s):
@@ -572,7 +572,6 @@ class YAPRun:
except Exception as e:
sys.stderr.write('Exception after', self.bindings, '\n')
has_raised = True
self.yapeng.mgoal(streams(False),"user", True)
return False,[]
@@ -654,8 +653,7 @@ 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
(text,program,query,_) = self.clean_end(raw_cell)
for i in self.syntaxErrors(raw_cell,raw_cell,''):
for i in self.syntaxErrors(raw_cell):
try:
(what,lin,_,text) = i
e = SyntaxError(what, ("<string>", lin, 1, text))
@@ -730,13 +728,13 @@ class YAPRun:
# 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.yapeng.mgoal(streams(False),"user", True)
self.shell.last_execution_succeeded = not has_raised
# Reset this so later displayed values do not modify the