This commit is contained in:
Vitor Santos Costa
2018-07-06 16:51:19 +01:00
parent 3bf897c503
commit 260dcbe754
11 changed files with 50 additions and 37 deletions

View File

@@ -5,7 +5,7 @@ set (PYTHON_SOURCES python.c pl2py.c pybips.c py2pl.c pl2pl.c pypreds.c pyio.c)
set (PYTHON_HEADERS py4yap.h)
set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
include_directories( ${CMAKE_BINARY_DIR} ${PYTHON_INCLUDE_DIRS}
include_directories( BEFORE ${PYTHON_INCLUDE_DIRS} ${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/os )
#talk to python.pl

View File

@@ -112,7 +112,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
// Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) {
case PL_VARIABLE: {
if (t == 0) {
if (yt == 0) {
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, yt, "in term_to_python");
}
PyObject *out = PyTuple_New(1);
@@ -148,21 +148,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);
#if PY_MAJOR_VERSION < 3
if (proper_ascii_string(s)) {
PyObject *o = PyString_FromStringAndSize(s, strlen(s));
return CHECKNULL(t, o);
} else
}
#endif
{
// char *p = malloc(strlen(s)+1);
// strcpy(p, s);
PyObject *pobj = PyUnicode_FromString(s);
Py_IncRef(pobj);
return CHECKNULL(t, pobj);
}
} break;
case PL_INTEGER: {
int64_t j;
@@ -406,8 +405,8 @@ PyObject *deref_term_to_python(term_t t) {
// am\n");
YAP_Term yt = YAP_GetFromSlot(t);
if (YAP_IsVarTerm(yt)) {
char s[32];
char *o = YAP_WriteBuffer(yt, s, 31, 0);
char b[1024];
char *o = YAP_WriteBuffer(yt, b, 1023, 0);
PyObject *p = PyUnicode_FromString(o);
return p;
}

View File

@@ -953,10 +953,12 @@ 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));
lhs = term_to_python(targ, true, NULL, true);
AOK(PL_get_arg(2, t, targ), NULL);
rhs = term_to_python(targ, true, NULL, true);
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
Yap_DebugPlWriteln(YAP_GetFromSlot(targ));
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
return PySequence_Concat(lhs, rhs);
}
if (!PyNumber_Check(lhs))
@@ -1034,11 +1036,11 @@ 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);
}
}
DebugPrintf("Tuple %p\n", 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 {

View File

@@ -5,9 +5,12 @@ PyObject *py_Main;
void pyErrorHandler__(int line, const char *file, const char *code) {
// this code is called if a Python error is found.
fprintf(stderr, " Python error detcted at %s %s:%d\n\n", code, file, line);
PyErr_Print();
}
//int lvl = push_text_stack();
PyObject *type, *val;
// PyErr_Fetch(&type, &val, NULL);
// PyErr_Print();
// Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python Error %s: %s",PyUnicode_AsUTF8(PyObject_Str(type)), PyUnicode_AsUTF8(PyObject_Str(val)));
};
static foreign_t python_len(term_t tobj, term_t tf) {
Py_ssize_t len;

View File

@@ -709,6 +709,7 @@ class YAPRun:
self.shell.displayhook.exec_result = self.result
has_raised = False
try:
self.yapeng.mgoal(streams(True),"user", True)
self.bindings = dicts = []
if cell.strip('\n \t'):
#create a Trace object, telling it what to ignore, and whether to
@@ -725,16 +726,16 @@ class YAPRun:
# run the new command using the given tracer
#
# tracer.runfunc(f,self,cell,state)
self.yapeng.mgoal(streams(True),"user", True)
self.jupyter_query( cell )
self.yapeng.mgoal(streams(False),"user", True)
# 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.shell.last_execution_succeeded = not has_raised
@@ -771,7 +772,7 @@ class YAPRun:
else:
taken = l0-(i-1)
n = s[i+1:].strip()
s = s[:i-1]
s = s[:i]
if n:
its = 0
for ch in n: