update jupyter

This commit is contained in:
Vitor Santos Costa
2018-10-16 14:33:16 +01:00
parent 3fdbbc6a77
commit c59b01e53c
34 changed files with 483 additions and 257 deletions

View File

@@ -40,13 +40,45 @@ static int py_putc(int sno, int ch) {
return ch;
}
#endif
char s[2];
unsigned char s[2];
PyObject *err;
s[0] = ch;
s[1] = '\0';
term_t g0 = python_acquire_GIL();
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
PyUnicode_FromString(s), NULL);
PyUnicode_FromString((char *)s), NULL);
python_release_GIL(g0);
if ((err = PyErr_Occurred())) {
PyErr_SetString(
err,
"Error in put\n"); // %s:%s:%d!\n", __FILE__, __FUNCTION__, __LINE__);
}
return ch;
}
static int py_wputc(int sno, int ch) {
// PyObject *pyw; // buffer
// int pyw_kind;
// PyObject *pyw_data;
StreamDesc *st = YAP_GetStreamFromId(sno);
#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
unsigned char s[8];
PyObject *err;
size_t n = put_utf8(s, ch);
s[n] = '\0';
term_t g0 = python_acquire_GIL();
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
PyUnicode_FromString((char *)s), NULL);
python_release_GIL(g0);
if ((err = PyErr_Occurred())) {
PyErr_SetString(
@@ -84,8 +116,6 @@ static void *py_open(VFS_t *me, const char *name, const char *io_mode,
st->u.w_irl.ptr = st->u.w_irl.buf = outbuf;
]\]
st->user_name = TermOutStream;
} else if (strcmp(name, "sys.stderr") == 0) {
@@ -127,38 +157,49 @@ static bool py_close(int sno) {
return true;
}
static bool pygetLine(StreamDesc *rl_iostream, int sno) {
// term_t ctk = python_acquire_GIL();
const char *myrl_line;
PyObject *user_line;
PyObject *user_line, *readl = NULL;
PyObject *err;
StreamDesc *s = YAP_GetStreamFromId(sno);
//term_t tg = python_acquire_GIL();
if (!strcmp(RepAtom(s->name)->StrOfAE,"input") ) {
// note that input may change
PyObject *pystream = PyDict_GetItemString( py_Builtin, "input");
if (pystream == NULL) {
PyObject *err;
if ((err = PyErr_Occurred())) {
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
// term_t tg = python_acquire_GIL();
PyObject_Print(s->u.private_data,stderr,0);
if (PyFunction_Check( s->u.private_data )) {
readl = s->u.private_data;
}
user_line = PyObject_CallFunctionObjArgs(pystream, PyUnicode_FromString("?- ") , NULL);
} else {
PyObject *readl = PyObject_GetAttrString(s->u.private_data, "readline");
user_line = PyObject_CallFunction(readl, "?- ");
if (!strcmp(RepAtom(s->name)->StrOfAE, "user_input")) {
// note that input may change
readl = PythonLookupSpecial("input");
}
if (readl == NULL) {
readl = PythonLookup("readline", s->u.private_data);
}
if (readl == NULL) {
readl = PythonLookup("read", s->u.private_data);
}
if (readl == NULL) {
if ((err = PyErr_Occurred())) {
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), NULL);
}
}
user_line = PyObject_CallFunctionObjArgs(readl,
NULL);
if ((err = PyErr_Occurred())) {
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
myrl_line = PyUnicode_AsUTF8(user_line);
if (myrl_line == NULL)
return NULL;
PyObject *err;
if ((err = PyErr_Occurred())) {
if (PyErr_GivenExceptionMatches(err, PyExc_EOFError))
return NULL;
PyErr_SetString(err, "Error in getLine\n");
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf = (unsigned char *)myrl_line;
return true;
@@ -182,6 +223,29 @@ static int py_getc(int sno) {
return ch;
}
static int py_wgetc(int sno) {
StreamDesc *s = YAP_RepStreamFromId(sno);
int ch;
bool fetch = (s->u.irl.ptr == NULL);
if (fetch) {
if (!pygetLine(s, sno)) {
return EOF;
}
}
if (s->u.irl.ptr == NULL)
return 10; // ????
const unsigned char *ttyptr = s->u.irl.ptr;
if (*ttyptr == '\0') {
ch = 10;
s->u.irl.ptr = NULL;
} else {
size_t n = get_utf8(ttyptr, strlen((char *)ttyptr), &ch);
s->u.irl.ptr += n;
}
return ch;
}
/**
@brief Yap_ReadlinePeekChar peeks the next char from the
readline buffer, but does not actually grab it.
@@ -253,8 +317,10 @@ bool init_python_vfs(void) {
pystream.open = py_open;
pystream.close = py_close;
pystream.get_char = py_getc;
pystream.get_wchar = py_wgetc;
pystream.peek_char = py_peek;
pystream.put_char = py_putc;
pystream.put_wchar = py_wputc;
pystream.flush = py_flush;
pystream.seek = py_seek;
pystream.next = GLOBAL_VFS;