jupyter
This commit is contained in:
@@ -4,26 +4,46 @@
|
||||
#include <VFS.h>
|
||||
|
||||
#include "YapStreams.h"
|
||||
#include "YapUTF8.h"
|
||||
|
||||
YAP_Term TermErrStream, TermOutStream;
|
||||
|
||||
static unsigned char *outbuf, *errbuf;
|
||||
|
||||
static void pyflush( StreamDesc *st)
|
||||
{
|
||||
#if 0
|
||||
st->u.w_irl.ptr[0] = '\0';
|
||||
fprintf(stderr,"%s\n", st->u.w_irl.buf);
|
||||
term_t tg = python_acquire_GIL();
|
||||
if (st->user_name == TermOutStream){
|
||||
PySys_WriteStdout("%s", st->u.w_irl.buf);
|
||||
} else {
|
||||
PySys_WriteStderr("%s", st->u.w_irl.buf);
|
||||
|
||||
}
|
||||
python_release_GIL(tg);
|
||||
st->u.w_irl.ptr = st->u.w_irl.buf;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
static int py_putc(int sno, int ch) {
|
||||
// PyObject *pyw; // buffer
|
||||
// int pyw_kind;
|
||||
// PyObject *pyw_data;
|
||||
StreamDesc *st = YAP_GetStreamFromId(sno);
|
||||
if (st->user_name == TermOutStream) {
|
||||
term_t tg = python_acquire_GIL();
|
||||
PySys_WriteStdout("%C", ch);
|
||||
python_release_GIL(tg);
|
||||
return ch;
|
||||
}
|
||||
if (st->user_name == TermErrStream) {
|
||||
term_t tg = python_acquire_GIL();
|
||||
PySys_WriteStderr("%C", ch);
|
||||
python_release_GIL(tg);
|
||||
return ch;
|
||||
#if 0
|
||||
if (false && (st->user_name == TermOutStream || st->user_name == TermErrStream)) {
|
||||
size_t sz = put_utf8(st->u.w_irl.ptr, ch);
|
||||
if (sz > 0) {
|
||||
st->u.w_irl.ptr += sz;
|
||||
if (ch == '\n' || st->u.w_irl.ptr - st->u.w_irl.buf > 256)
|
||||
{pyflush(st); }
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
#endif
|
||||
char s[2];
|
||||
PyObject *err;
|
||||
s[0] = ch;
|
||||
@@ -58,13 +78,20 @@ static void *py_open(VFS_t *me, const char *name, const char *io_mode,
|
||||
}
|
||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||
st->name = YAP_LookupAtom(name);
|
||||
if (strcmp(name, "sys.stdout") == 0) {
|
||||
/* if (strcmp(name, "sys.stdout") == 0) {
|
||||
if (!outbuf)
|
||||
outbuf = ( unsigned char *)malloc(1024);
|
||||
st->u.w_irl.ptr = st->u.w_irl.buf = outbuf;
|
||||
st->user_name = TermOutStream;
|
||||
} else if (strcmp(name, "sys.stderr") == 0) {
|
||||
st->user_name = TermErrStream;
|
||||
if (!errbuf)
|
||||
errbuf = ( unsigned char *)malloc(1024);
|
||||
st->u.w_irl.ptr = st->u.w_irl.buf = errbuf;
|
||||
// } else if (strcmp(name, "input") == 0) {
|
||||
//pystream = PyObject_Call(pystream, PyTuple_New(0), NULL);
|
||||
} else {
|
||||
} else */
|
||||
{
|
||||
st->user_name = YAP_MkAtomTerm(st->name);
|
||||
}
|
||||
st->u.private_data = pystream;
|
||||
@@ -75,10 +102,23 @@ static void *py_open(VFS_t *me, const char *name, const char *io_mode,
|
||||
}
|
||||
|
||||
|
||||
static void py_flush(int sno) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
term_t tg = python_acquire_GIL();
|
||||
PyObject *flush = PyObject_GetAttrString(s->u.private_data, "flush");
|
||||
pyflush(s);
|
||||
PyObject_CallFunction(flush, NULL);
|
||||
python_release_GIL(tg);
|
||||
}
|
||||
|
||||
|
||||
static bool py_close(int sno) {
|
||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||
if (st->status & (Output_Stream_f|Append_Stream_f))
|
||||
py_flush(sno);
|
||||
if (strcmp(st->name, "sys.stdout") && strcmp(st->name, "sys.stderr")) {
|
||||
Py_XDECREF(st->u.private_data);
|
||||
st->u.w_irl.buf = st->u.w_irl.ptr = NULL;
|
||||
}
|
||||
st->u.private_data = NULL;
|
||||
st->vfs = NULL;
|
||||
@@ -163,14 +203,6 @@ static int64_t py_seek(int sno, int64_t where, int how) {
|
||||
return PyLong_AsLong(pyr);
|
||||
}
|
||||
|
||||
static void py_flush(int sno) {
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
term_t tg = python_acquire_GIL();
|
||||
PyObject *flush = PyObject_GetAttrString(s->u.private_data, "flush");
|
||||
PyObject_CallFunction(flush, NULL);
|
||||
python_release_GIL(tg);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void python_output(void) {
|
||||
PyObject *stream = string_to_python("sys.stdout", true, NULL);
|
||||
|
@@ -34,26 +34,18 @@ jupyter_cell( _Caller, _, Line ) :-
|
||||
jupyter_cell( _Caller, _, [] ) :- !.
|
||||
jupyter_cell( Caller, _, Line ) :-
|
||||
Self := Caller.query,
|
||||
gated_call( streams(true),
|
||||
python_query(Self,Line),
|
||||
Gate,
|
||||
restreams(Gate)
|
||||
).
|
||||
python_query(Self,Line).
|
||||
|
||||
restreams(redo) :-
|
||||
restreams(call) :-
|
||||
streams(true).
|
||||
restreams(fail) :-
|
||||
streams(false).
|
||||
restreams(answer) :-
|
||||
streams(false).
|
||||
restreams(answer).
|
||||
restreams(exit) :-
|
||||
streams(false).
|
||||
restreams(!) :-
|
||||
streams(false).
|
||||
restreams(external_exception(_)) :-
|
||||
streams(false).
|
||||
restreams(exception) :-
|
||||
streams(false).
|
||||
restreams(!).
|
||||
restreams(external_exception(_)).
|
||||
restreams(exception).
|
||||
|
||||
jupyter_consult(Text) :-
|
||||
blank( Text ),
|
||||
@@ -83,7 +75,6 @@ streams(false) :-
|
||||
streams(false).
|
||||
streams(true) :-
|
||||
nb_setval(jupyter_cell, true),
|
||||
start_low_level_trace,
|
||||
\+ current_stream('/python/input',_,_),
|
||||
open('/python/input', read, Input, [alias(user_input),bom(false),script(false)]),
|
||||
assert( cell_stream( Input) ),
|
||||
|
@@ -560,6 +560,8 @@ class YAPRun:
|
||||
pg = jupyter_query( self, program, squery)
|
||||
self.query = self.yapeng.query(pg)
|
||||
self.query.answer = {}
|
||||
self.yapeng.mgoal(streams(False),"user", True)
|
||||
self.yapeng.mgoal(streams(True),"user", True)
|
||||
while self.query.next():
|
||||
answer = self.query.answer
|
||||
found = True
|
||||
@@ -568,6 +570,7 @@ class YAPRun:
|
||||
if self.query.port == "exit":
|
||||
self.os = None
|
||||
sys.stderr.writeln('Done, with', self.bindings)
|
||||
self.yapeng.mgoal(streams(False),"user", True)
|
||||
return True,self.bindings
|
||||
if stop or howmany == self.iterations:
|
||||
return True, self.bindings
|
||||
@@ -576,12 +579,14 @@ class YAPRun:
|
||||
else:
|
||||
self.os = None
|
||||
self.query.close()
|
||||
self.query = None
|
||||
`` self.query = None
|
||||
sys.stderr.write('Fail\n')
|
||||
return True,self.bindings
|
||||
self.yapeng.mgoal(streams(False),"user", True)
|
||||
return True,self.bindings
|
||||
except Exception as e:
|
||||
sys.stderr.write('Exception after', self.bindings, '\n')
|
||||
has_raised = True
|
||||
self.yapeng.mgoal(streams(False),"user", True)
|
||||
return False,[]
|
||||
|
||||
|
||||
@@ -628,6 +633,7 @@ class YAPRun:
|
||||
# ask = True
|
||||
# launch the query
|
||||
|
||||
|
||||
info = interactiveshell.ExecutionInfo(
|
||||
raw_cell, store_history, silent, shell_futures)
|
||||
|
||||
@@ -731,11 +737,10 @@ class YAPRun:
|
||||
|
||||
# run the new command using the given tracer
|
||||
#
|
||||
# self.yapeng.mgoal(streams(True),"user", True)
|
||||
# tracer.runfunc(f,self,cell,state)
|
||||
self.jupyter_query( cell )
|
||||
# state = tracer.runfunc(jupyter_query( self, cell ) )
|
||||
# self.yapeng.mgoal(streams(False),"user", True)
|
||||
self.yapeng.mgoal(streams(False),"user", True)
|
||||
self.shell.last_execution_succeeded = True
|
||||
self.result.result = (True, dicts)
|
||||
|
||||
|
Reference in New Issue
Block a user