Merge /home/vsc/yap

This commit is contained in:
Vítor Santos Costa
2018-07-11 19:32:04 +01:00
32 changed files with 610 additions and 465 deletions

View File

@@ -46,11 +46,12 @@ foreign_t assign_to_symbol(term_t t, PyObject *e) {
PyObject *dic;
if (!lookupPySymbol(s, NULL, &dic))
dic = py_Main;
Py_INCREF(e);
Py_INCREF(e);
return PyObject_SetAttrString(dic, s, e) == 0;
}
foreign_t python_to_term(PyObject *pVal, term_t t) {
foreign_t python_to_term(PyObject *pVal, term_t t)
{
bool rc = true;
term_t to = PL_new_term_ref();
// fputs(" <<*** ",stderr); PyObject_Print(pVal,stderr,0);
@@ -89,14 +90,14 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
#else
const char *s = PyUnicode_AsUTF8(pVal);
#endif
// if (PyDict_GetItemString(py_Atoms, s))
// rc = rc && PL_unify_atom_chars(t, s);
// else
if (Yap_AtomInUse(s))
rc = rc && PL_unify_atom_chars(t, s);
} else if (PyByteArray_Check(pVal)) {
rc = rc && PL_unify_string_chars(t, PyByteArray_AsString(pVal));
#if PY_MAJOR_VERSION < 3
} else if (PyString_Check(pVal)) {
else
rc = rc && PL_unify_string_chars(t, s);
} else if (PyByteArray_Check(pVal)) {
rc = rc && PL_unify_string_chars(t, PyByteArray_AsString(pVal));
#if PY_MAJOR_VERSION < 3
} else if (PyString_Check(pVal)) {
rc = rc && PL_unify_string_chars(t, PyString_AsString(pVal));
#endif
} else if (PyTuple_Check(pVal)) {
@@ -130,15 +131,18 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
}
if (PL_unify_functor(t, f)) {
for (i = 0; i < sz; i++) {
if (!PL_get_arg(i + 1, t, to))
term_t to = PL_new_term_ref();
if (!PL_get_arg(i + 1, t, to))
rc = false;
PyObject *p = PyTuple_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
p = Py_None;
}
rc = rc && python_to_term(p, to);
}
} else {
rc = rc && python_to_term(p, to);
}
PL_reset_term_refs(to);
}
} else {
rc = false;
}
@@ -150,11 +154,13 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
for (i = 0; i < sz; i++) {
PyObject *obj;
term_t to = PL_new_term_ref();
rc = rc && PL_unify_list(t, to, t);
if ((obj = PyList_GetItem(pVal, i)) == NULL) {
obj = Py_None;
}
rc = rc && python_to_term(obj, to);
PL_reset_term_refs(to);
if (!rc)
return false;
}
@@ -163,7 +169,6 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
// Yap_DebugPlWrite(yt); fputs("[***]\n", stderr);
} else if (PyDict_Check(pVal)) {
Py_ssize_t pos = 0;
term_t to = PL_new_term_ref(), ti = to;
int left = PyDict_Size(pVal);
PyObject *key, *value;
@@ -173,6 +178,7 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
while (PyDict_Next(pVal, &pos, &key, &value)) {
term_t tkey = PL_new_term_ref(), tval = PL_new_term_ref(), tint,
tnew = PL_new_term_ref();
term_t to = PL_new_term_ref();
/* do something interesting with the values... */
if (!python_to_term(key, tkey)) {
continue;
@@ -191,21 +197,22 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
PL_reset_term_refs(tkey);
rc = false;
}
if (!PL_unify(ti, tint)) {
if (!PL_unify(to, tint)) {
rc = false;
}
ti = tnew;
PL_reset_term_refs(tkey);
}
rc = rc && PL_unify(t, to);
}
} else {
rc = rc && repr_term(pVal, t);
}
PL_reset_term_refs(to);
return rc;
}
X_API YAP_Term pythonToYAP(PyObject *pVal) {
term_t t = PL_new_term_ref();
@@ -215,12 +222,13 @@ X_API YAP_Term pythonToYAP(PyObject *pVal) {
}
YAP_Term tt = YAP_GetFromSlot(t);
PL_reset_term_refs(t);
//Py_DECREF(pVal);
// Py_DECREF(pVal);
return tt;
}
PyObject *py_Local, *py_Global;
/**
* assigns the Python RHS to a Prolog term LHS, ie LHS = RHS
*
@@ -317,7 +325,7 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
if (PySequence_Check(o) && PyInt_Check(i)) {
long int j;
j = PyInt_AsLong(i);
return PySequence_SetItem(o, i, exp) == 0;
return PySequence_SetItem(o, i, exp) == 0;
}
#endif
if (PyDict_Check(o)) {

View File

@@ -41,6 +41,18 @@ class Engine( YAPEngine ):
self.goal(release)
class JupyterEngine( Engine ):
def __init__(self, args=None,self_contained=False,**kwargs):
# type: (object) -> object
if not args:
args = EngineArgs(**kwargs)
args.jupyter = True
Engine.__init__(self, args)
self.goal(set_prolog_flag('verbose', 'silent'),True)
self.goal(compile(library('jupyter')), True)
self.goal(set_prolog_flag('verbose', 'normal'), True)
class EngineArgs( YAPEngineArgs ):
""" Interface to Engine Options class"""
def __init__(self, args=None,**kwargs):

View File

@@ -273,14 +273,15 @@ set (RESOURCES
#yap_kernel/resources/logo-32x32.png
#yap_kernel/resourcess/logo-64x64.png
)
set (RENAMED_RESOURCES
set (RENAMED_RESOURCES
yap_kernel/resources/logo-32x32.png
yap_kernel/resources/logo-64x64.png
# yap_kernel/resources/codemirror/mode/prolog/prolog.js
)
set (PL_SOURCES yap_ipython/prolog/jupyter.yap yap_ipython/prolog/complete.yap
yap_ipython/prolog/verify.yap
)
set(FILES ${PYTHON_SOURCES} ${PL_SOURCES} ${EXTRAS} ${RESOURCES})
@@ -321,7 +322,7 @@ add_custom_target(YAP_KERNEL ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/logo-32x32.png ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/logo-64x64.png yap.tgz ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/kernel.js ${CMAKE_CURRENT_BINARY_DIR}/yap_kernel/resources/prolog.js ${CMAKE_CURRENT_BINARY_DIR}/yap.tgz
)
install(CODE "execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} build sdist bdist
COMMAND ${PYTHON_EXECUTABLE} -m pip install ${PYTHON_USER_INSTALL} --ignore-installed --no-deps .

View File

@@ -4,6 +4,7 @@
* @brief JUpyter support.
*/
:- yap_flag(gc_trace,verbose).
% :- module( jupyter,
% [jupyter_query/3,
@@ -35,7 +36,12 @@ jupyter_cell( _Caller, _, Line ) :-
!.
jupyter_cell( Caller, _, Line ) :-
Self := Caller.query,
python_query(Self,Line).
catch(
python_query(Self,Line),
E=error(A,B),
system_error(A,B)
).
jupyter_cell(_,_,_).
restreams(call) :-
streams(true).
@@ -55,43 +61,37 @@ jupyter_consult(Cell) :-
% Name = 'Inp',
% stream_property(Stream, file_name(Name) ),
% setup_call_cleanup(
open_mem_read_stream( Cell, Stream),
load_files(user:'jupyter cell',[stream(Stream)]),
close(Stream).
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).
:- dynamic cell_stream/1.
blank(Text) :-
string(Text),
!,
string_codes(Text, L),
maplist( code_type(space), L).
streams(false) :-
nb_setval(jupyter_cell, false),
retract(cell_stream(S)),
close(S),
fail.
streams(false).
close(user_input),
close(user_output),
close(user_error).
streams(true) :-
streams( false ),
nb_setval(jupyter_cell, true),
% \+ current_stream('/python/input',_,_),
open('/python/input', read, Input, [alias(user_input),bom(false),script(false)]),
assert( cell_stream( Input) ),
set_prolog_flag(user_input,Input),
fail.
streams(true) :-
% \+ current_stream('/python/sys.stdout',_,_),
open('/python/sys.stdout', append, Output, [alias(user_output)]),
set_prolog_flag(user_output, Output),
assert( cell_stream( Output) ),
fail.
streams(true) :-
% \+ current_stream('/python/sys.stderr',_,_),
open('/python/sys.stderr', append, Error, [alias(user_error)]),
assert( cell_stream( Error) ),
set_prolog_flag(user_error, Error),
fail.
streams(true).
open('/python/sys.stderr', append, Error, [alias(user_error)]).
ready(_Self, Line ) :-
blank( Line ),
@@ -208,4 +208,3 @@ plot_inline :-
:- endif.
:- ( start_low_level_trace ).

View File

@@ -0,0 +1,62 @@
/**
* @file jupyter.yap4py
*
* @brief JUpyter support.
*/
% :- module( verify,
% [all_clear/4,
% errors/2,
% ready/2,
s % completion/2,
% ]
%% ).
:- use_module(library(hacks)).
:- use_module(library(lists)).
:- use_module(library(maplist)).
:- use_module(library(python)).
:- use_module(library(yapi)).
:- python_import(sys).
p_errors( Errors, Cell) :-
blank( Cell ),
!.
p_errors( Errors, Cell) :-
no_errors( Errors , Cell ).
no_errors( _Errors , Text ) :-
blank(Text).
no_errors( Errors , Text ) :-
setup_call_cleanup(
open_esh( Errors , Text, Stream),
esh(Errors , Stream),
close_esh( Errors , Stream )
).
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(_Errors , E) :- throw(E).
open_esh(_Errors , Text, Stream) :-
open_mem_read_stream( Text, Stream ).
esh(Errors , Stream) :-
repeat,
catch(
read_clause(Stream, Cl, [term_position(_Pos), syntax_errors(fail)] ),
Error,
syntax(Errors , Error)
),
Cl == end_of_file,
!.
close_esh( _Errors , Stream ) :-
close(Stream).

View File

@@ -510,10 +510,9 @@ 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
@@ -573,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,[]
@@ -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