jup
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
|
||||
#include "Yap.h"
|
||||
#include "Yap.h"
|
||||
|
||||
#include "py4yap.h"
|
||||
|
||||
@@ -51,7 +51,6 @@ static PyObject *s_to_python(const char *s, bool eval, PyObject *p0) {
|
||||
*/
|
||||
X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) {
|
||||
|
||||
|
||||
char *buf = malloc(strlen(s) + 1), *child;
|
||||
while ((child = strchr(s, '.')) != NULL) {
|
||||
size_t len = child - s;
|
||||
@@ -108,9 +107,8 @@ static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
|
||||
* @return a Python object descriptor or NULL if failed
|
||||
*/
|
||||
PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
// o≈
|
||||
//
|
||||
YAP_Term yt = YAP_GetFromSlot(t);
|
||||
Yap_DebugPlWriteln(yt);
|
||||
switch (PL_term_type(t)) {
|
||||
case PL_VARIABLE: {
|
||||
if (yt == 0) {
|
||||
@@ -137,7 +135,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
o = PyUnicode_FromString(s);
|
||||
}
|
||||
if (o) {
|
||||
//PyDict_SetItemString(py_Atoms, s, Py_None);
|
||||
// PyDict_SetItemString(py_Atoms, s, Py_None);
|
||||
Py_INCREF(o);
|
||||
return o;
|
||||
}
|
||||
@@ -149,20 +147,20 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
} else if (YAP_IsStringTerm(yt)) {
|
||||
s = YAP_StringOfTerm(yt);
|
||||
} else {
|
||||
return CHECKNULL(t, NULL);
|
||||
return CHECKNULL(t, NULL);
|
||||
}
|
||||
PyObject *pobj = PyUnicode_FromString(s);
|
||||
|
||||
PyObject *pobj = PyUnicode_FromString(s);
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (proper_ascii_string(s)) {
|
||||
PyObject *o = PyString_FromStringAndSize(s, strlen(s));
|
||||
return CHECKNULL(t, o);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// char *p = malloc(strlen(s)+1);
|
||||
// strcpy(p, s);
|
||||
Py_IncRef(pobj);
|
||||
return CHECKNULL(t, pobj);
|
||||
// char *p = malloc(strlen(s)+1);
|
||||
// strcpy(p, s);
|
||||
Py_IncRef(pobj);
|
||||
return CHECKNULL(t, pobj);
|
||||
} break;
|
||||
case PL_INTEGER: {
|
||||
int64_t j;
|
||||
@@ -189,21 +187,21 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
if (PL_is_pair(t)) {
|
||||
Term t0 = Yap_GetFromHandle(t);
|
||||
Term *tail;
|
||||
size_t len,i;
|
||||
if ((len = Yap_SkipList(&t0, &tail))>=0 && *tail == TermNil) {
|
||||
size_t len, i;
|
||||
if ((len = Yap_SkipList(&t0, &tail)) >= 0 && *tail == TermNil) {
|
||||
PyObject *out, *a;
|
||||
|
||||
out = PyList_New(len);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
Term ai = HeadOfTerm(t0);
|
||||
a = term_to_python(Yap_InitHandle(ai), eval, o, cvt);
|
||||
Term ai = HeadOfTerm(t0);
|
||||
a = term_to_python(Yap_InitHandle(ai), eval, o, cvt);
|
||||
if (a) {
|
||||
if (PyList_SetItem(out, i, a) < 0) {
|
||||
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||
}
|
||||
}
|
||||
t0 = TailOfTerm(t0);
|
||||
t0 = TailOfTerm(t0);
|
||||
}
|
||||
return out;
|
||||
} else {
|
||||
@@ -256,11 +254,11 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
PyObject *ip = term_to_python(trhs, eval, o, cvt);
|
||||
if (PySequence_Check(v)) {
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (PyLong_Check(ip)) {
|
||||
if (PyLong_Check(ip)) {
|
||||
min = PyLong_AsLong(ip);
|
||||
} else if (PyInt_Check(ip)) {
|
||||
} else if (PyInt_Check(ip)) {
|
||||
min = PyInt_asInt(ip);
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (PyLong_Check(ip)) {
|
||||
PyObject *o = PySequence_GetItem(v, PyLong_AsLong(ip));
|
||||
@@ -337,7 +335,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
AOK(PL_get_arg(1, t, t), NULL);
|
||||
if (!(dict = PyDict_New()))
|
||||
return NULL;
|
||||
Py_INCREF(dict);
|
||||
Py_INCREF(dict);
|
||||
DebugPrintf("Dict %p\n", dict);
|
||||
|
||||
while (PL_is_functor(t, FUNCTOR_comma2)) {
|
||||
|
@@ -953,12 +953,11 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
|
||||
if (!PL_get_arg(1, t, targ))
|
||||
return NULL;
|
||||
// Yap_DebugPlWriteln(YAP_GetFromSlot(t));
|
||||
// Yap_DebugPlWriteln(YAP_GetFromSlot(t));
|
||||
lhs = term_to_python(targ, true, NULL, true);
|
||||
AOK(PL_get_arg(2, t, targ), NULL);
|
||||
rhs = term_to_python(targ, true, NULL, true);
|
||||
Yap_DebugPlWriteln(YAP_GetFromSlot(targ));
|
||||
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
|
||||
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
|
||||
return PySequence_Concat(lhs, rhs);
|
||||
}
|
||||
if (!PyNumber_Check(lhs))
|
||||
@@ -1036,11 +1035,12 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
PyDict_SetItem(pyDict, key, val);
|
||||
} else {
|
||||
indict = false;
|
||||
pArgs = PyTuple_New(i);
|
||||
pArgs = PyTuple_New(i);
|
||||
}
|
||||
}
|
||||
fprintf(stderr,"Tuple %p: %s\n", pyDict, PyUnicode_AsUTF8(PyObject_Str(pyDict)));
|
||||
if (!indict) {
|
||||
fprintf(stderr, "Tuple %p: %s\n", pyDict,
|
||||
PyUnicode_AsUTF8(PyObject_Str(pyDict)));
|
||||
if (!indict) {
|
||||
if (PL_is_variable(tleft)) {
|
||||
pArg = Py_None;
|
||||
} else {
|
||||
|
@@ -8,7 +8,6 @@
|
||||
|
||||
YAP_Term TermErrStream, TermOutStream;
|
||||
|
||||
|
||||
static void pyflush(StreamDesc *st) {
|
||||
#if 0
|
||||
st->u.w_irl.ptr[0] = '\0';
|
||||
@@ -75,8 +74,7 @@ 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 ||
|
||||
strcmp(name, "sys.stderr") == 0 ||
|
||||
if (strcmp(name, "sys.stdout") == 0 || strcmp(name, "sys.stderr") == 0 ||
|
||||
strcmp(name, "input") == 0) {
|
||||
st->status |= Tty_Stream_f;
|
||||
}
|
||||
@@ -131,20 +129,23 @@ static bool py_close(int sno) {
|
||||
|
||||
static bool getLine(StreamDesc *rl_iostream, int sno) {
|
||||
char *myrl_line = NULL;
|
||||
term_t ctk = python_acquire_GIL();
|
||||
|
||||
// term_t ctk = python_acquire_GIL();
|
||||
PyObject_Print(rl_iostream->u.private_data, stderr, 0);
|
||||
/* window of vulnerability opened */
|
||||
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(rl_iostream->u.private_data, NULL));
|
||||
python_release_GIL(ctk);
|
||||
PyObject *readl =
|
||||
PyObject_GetAttrString(rl_iostream->u.private_data, "readline");
|
||||
PyObject_Print(readl, stderr, 0);
|
||||
|
||||
myrl_line = PyUnicode_AsUTF8(
|
||||
PyObject_CallFunctionObjArgs(readl, rl_iostream->u.private_data, NULL));
|
||||
// python_release_GIL(ctk);
|
||||
PyObject *err;
|
||||
if ((err = PyErr_Occurred())) {
|
||||
PyErr_SetString(
|
||||
err,
|
||||
"Error in getLine\n");
|
||||
if ((err = PyErr_Occurred())) {
|
||||
PyErr_SetString(err, "Error in getLine\n");
|
||||
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
|
||||
}
|
||||
size_t size = strlen (myrl_line)+1;
|
||||
rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf =
|
||||
size_t size = strlen(myrl_line) + 1;
|
||||
rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf =
|
||||
(const unsigned char *)malloc(size);
|
||||
memmove((void *)rl_iostream->u.irl.buf, myrl_line, size);
|
||||
return true;
|
||||
|
@@ -79,11 +79,7 @@ close_esh( _Engine , Stream ) :-
|
||||
p3_message( _Severity, Engine, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
|
||||
python_clear_errors,
|
||||
!,
|
||||
writeln(E),
|
||||
NE := [t(Cause,LN,CharPos,Details)]+Engine.errors,
|
||||
writeln(E),
|
||||
writeln(NE),
|
||||
Engine.errors := NE.
|
||||
Engine.errors := [t(Cause,LN,CharPos,Details)]+Engine.errors.
|
||||
p3_message(error, Engine, E) :-
|
||||
python_clear_errors,
|
||||
!.
|
||||
@@ -181,4 +177,3 @@ p3_message(error, Engine, E) :-
|
||||
%% Self.errors := [t(C,L,N,A)] + Self.errors,
|
||||
%% fail.
|
||||
%% close_events( _ ).
|
||||
|
||||
|
Reference in New Issue
Block a user