wheel/fixes/docs
This commit is contained in:
@@ -3,57 +3,50 @@
|
||||
|
||||
if (PYTHONLIBS_FOUND)
|
||||
|
||||
include(FindPythonModule)
|
||||
include(FindPythonModule)
|
||||
|
||||
set (PYTHON_SOURCES
|
||||
python.c pl2py.c pybips.c py2pl.c pl2pl.c pypreds.c)
|
||||
set (PYTHON_SOURCES python.c pl2py.c pybips.c py2pl.c pl2pl.c pypreds.c)
|
||||
|
||||
set (PYTHON_HEADERS
|
||||
python.h)
|
||||
set (PYTHON_HEADERS python.h)
|
||||
|
||||
set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
|
||||
set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
|
||||
|
||||
add_library (YAPPython SHARED ${PYTHON_SOURCES})
|
||||
add_library (YAPPython SHARED ${PYTHON_SOURCES})
|
||||
|
||||
target_link_libraries(YAPPython libYap ${PYTHON_LIBRARY}
|
||||
${WINDLLS}
|
||||
${GMP_LIBRARIES})
|
||||
target_link_libraries(YAPPython libYap ${PYTHON_LIBRARY} ${WINDLLS} ${GMP_LIBRARIES})
|
||||
|
||||
set_property( SOURCE ${PYTHON_SOURCES} APPEND PROPERTY COMPILE_DEFINITIONS YAP_KERNEL=1)
|
||||
|
||||
set_property( SOURCE ${PYTHON_SOURCES} APPEND PROPERTY COMPILE_DEFINITIONS YAP_KERNEL=1)
|
||||
set (PYTHON_PL python.pl)
|
||||
|
||||
install(FILES python.pl DESTINATION ${libpl} )
|
||||
|
||||
install(FILES python.pl
|
||||
DESTINATION ${libpl}
|
||||
)
|
||||
|
||||
configure_file ("setup.py.cmake" "setup.py" )
|
||||
add_to_group( pl_library PYTHON_PL )
|
||||
|
||||
# configure_file ("setup.py.cmake" "setup.py" )
|
||||
|
||||
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
|
||||
|
||||
#set_target_properties (YAPPython PROPERTIES PREFIX "")
|
||||
#set_target_properties (YAPPython PROPERTIES PREFIX "")
|
||||
|
||||
find_python_module( jupyter )
|
||||
|
||||
find_python_module( jupyter )
|
||||
if (PY_JUPYTER)
|
||||
|
||||
if (PY_JUPYTER)
|
||||
add_subdirectory(yap_kernel)
|
||||
|
||||
add_subdirectory(yap_kernel)
|
||||
ENDIF()
|
||||
|
||||
ENDIF()
|
||||
|
||||
|
||||
|
||||
IF(WIN32)
|
||||
install(TARGETS YAPPython
|
||||
LIBRARY DESTINATION ${dlls}
|
||||
RUNTIME DESTINATION ${bindir}
|
||||
ARCHIVE DESTINATION ${dlls} )
|
||||
else()
|
||||
install(TARGETS YAPPython
|
||||
LIBRARY DESTINATION ${dlls}
|
||||
RUNTIME DESTINATION ${dlls}
|
||||
ARCHIVE DESTINATION ${dlls} )
|
||||
IF(WIN32)
|
||||
install(TARGETS YAPPython
|
||||
LIBRARY DESTINATION ${libdir}
|
||||
RUNTIME DESTINATION ${bindir}
|
||||
ARCHIVE DESTINATION ${libdir} )
|
||||
else()
|
||||
install(TARGETS YAPPython
|
||||
LIBRARY DESTINATION ${libdir}
|
||||
RUNTIME DESTINATION ${libdir}
|
||||
ARCHIVE DESTINATION ${libdir} )
|
||||
endif()
|
||||
|
||||
endif (PYTHONLIBS_FOUND)
|
||||
|
@@ -2,14 +2,14 @@
|
||||
|
||||
#include "python.h"
|
||||
|
||||
PyObject *py_Local, *py_Global;
|
||||
extern PyObject *py_Local, *py_Global;
|
||||
|
||||
PyObject *YE(term_t t, int line, const char *file, const char *code)
|
||||
{
|
||||
char buf[1024];
|
||||
YAP_WriteBuffer(YAP_GetFromSlot(t), buf, 1023, 0);
|
||||
fprintf(stderr, "**** Warning,%s@%s:%d: failed on expression %s\n", code, file, line, buf );
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -32,15 +32,16 @@ void YEM(const char * exp, int line, const char *file, const char *code)
|
||||
PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
||||
// o≈
|
||||
YAP_Term yt = YAP_GetFromSlot(t);
|
||||
// Yap_DebugPlWriteln(yt);
|
||||
Yap_DebugPlWriteln(yt);
|
||||
switch (PL_term_type(t)) {
|
||||
case PL_VARIABLE: {
|
||||
YAP_Term i = YAP_MkIntTerm(t);
|
||||
PyObject *rc = term_to_nametuple(
|
||||
"H", 1, YAP_InitSlot(YAP_MkApplTerm(
|
||||
YAP_MkFunctor(YAP_LookupAtom("H"), 1), 1, &i)));
|
||||
return CHECKNULL( t, rc );
|
||||
};
|
||||
if (t==0) {
|
||||
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, yt, "in term_to_python");
|
||||
}
|
||||
PyObject *out = PyTuple_New(1);
|
||||
PyTuple_SET_ITEM(out, 0, PyLong_FromLong(t));
|
||||
return term_to_nametuple("v", 1, out);
|
||||
};
|
||||
case PL_ATOM: {
|
||||
YAP_Atom at = YAP_AtomOfTerm(yt);
|
||||
const char *s;
|
||||
@@ -105,7 +106,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
||||
term_t tail = PL_new_term_ref(), arg;
|
||||
size_t len, i;
|
||||
if (PL_skip_list(t, tail, &len) && PL_get_nil(tail)) {
|
||||
PyObject *out;
|
||||
PyObject *out, *a;
|
||||
|
||||
arg = tail;
|
||||
out = PyList_New(len);
|
||||
@@ -115,10 +116,14 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
||||
for (i = 0; i < len; i++) {
|
||||
if (!PL_get_list(t, arg, t)) {
|
||||
PL_reset_term_refs(tail);
|
||||
return CHECKNULL(t,NULL);
|
||||
return Py_None;
|
||||
}
|
||||
if (PyList_SetItem(out, i, term_to_python(arg, eval, o)) < 0) {
|
||||
return CHECKNULL(t,NULL);
|
||||
a = term_to_python(arg, eval, o);
|
||||
if (a == NULL) {
|
||||
a = Py_None;
|
||||
}
|
||||
if (PyList_SetItem(out, i, a) < 0) {
|
||||
return Py_None;
|
||||
}
|
||||
}
|
||||
PL_reset_term_refs(tail);
|
||||
@@ -144,6 +149,8 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
||||
}
|
||||
|
||||
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
|
||||
if (t==0)
|
||||
return NULL;
|
||||
term_t yt = YAP_InitSlot(t);
|
||||
o = term_to_python(yt, eval, o);
|
||||
PL_reset_term_refs(yt);
|
||||
|
@@ -6,7 +6,6 @@ static foreign_t repr_term(PyObject *pVal, term_t t) {
|
||||
PL_put_pointer(t1, pVal);
|
||||
PL_cons_functor(to, FUNCTOR_pointer1, t1);
|
||||
Py_INCREF(pVal);
|
||||
PL_reset_term_refs(to);
|
||||
return PL_unify(t, to);
|
||||
}
|
||||
|
||||
@@ -14,7 +13,6 @@ foreign_t assign_to_symbol(term_t t, PyObject *e);
|
||||
|
||||
foreign_t assign_to_symbol(term_t t, PyObject *e) {
|
||||
char *s = NULL;
|
||||
PyErr_Clear();
|
||||
if (!PL_get_atom_chars(t, &s)) {
|
||||
return false;
|
||||
}
|
||||
@@ -25,58 +23,58 @@ foreign_t assign_to_symbol(term_t t, PyObject *e) {
|
||||
}
|
||||
|
||||
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); fputs("<<***\n",stderr);
|
||||
if (pVal == Py_None) {
|
||||
return PL_unify_atom(t, ATOM_none);
|
||||
fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" >>***\n",stderr);
|
||||
rc= PL_unify_atom(t, ATOM_none); fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" >>***\n",stderr);
|
||||
}
|
||||
if (PyBool_Check(pVal)) {
|
||||
if (PyObject_IsTrue(pVal)) {
|
||||
return PL_unify_atom(t, ATOM_true);
|
||||
rc = PL_unify_atom(t, ATOM_true);
|
||||
} else {
|
||||
return PL_unify_atom(t, ATOM_false);
|
||||
rc = PL_unify_atom(t, ATOM_false);
|
||||
}
|
||||
} else if (PyLong_Check(pVal)) {
|
||||
return PL_unify_int64(t, PyLong_AsLong(pVal));
|
||||
rc = PL_unify_int64(t, PyLong_AsLong(pVal));
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
} else if (PyInt_Check(pVal)) {
|
||||
return PL_unify_int64(t, PyInt_AsLong(pVal));
|
||||
rc = PL_unify_int64(t, PyInt_AsLong(pVal));
|
||||
#endif
|
||||
} else if (PyFloat_Check(pVal)) {
|
||||
return PL_unify_float(t, PyFloat_AsDouble(pVal));
|
||||
rc = PL_unify_float(t, PyFloat_AsDouble(pVal));
|
||||
} else if (PyComplex_Check(pVal)) {
|
||||
bool rc;
|
||||
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref(),
|
||||
term_t t1 = PL_new_term_ref(),
|
||||
t2 = PL_new_term_ref();
|
||||
if (!PL_put_float(t1, PyComplex_RealAsDouble(pVal)) ||
|
||||
!PL_put_float(t2, PyComplex_ImagAsDouble(pVal)) ||
|
||||
!PL_cons_functor(to, FUNCTOR_complex2, t1, t2)) {
|
||||
rc = FALSE;
|
||||
rc = false;
|
||||
} else {
|
||||
rc = PL_unify(t, to);
|
||||
}
|
||||
PL_reset_term_refs(to);
|
||||
return rc;
|
||||
} else if (PyUnicode_Check(pVal)) {
|
||||
atom_t tmp_atom;
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
= PyUnicode_GetSize(pVal) + 1;
|
||||
size_t sz = PyUnicode_GetSize(pVal) + 1;
|
||||
wchar_t *ptr = malloc(sizeof(wchar_t) * sz);
|
||||
sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, ptr, sz - 1);
|
||||
tmp_atom = PL_new_atom_wchars(sz, ptr);
|
||||
tmp_atom = PL_new_atom_wchars(sz, ptr);
|
||||
free(ptr);
|
||||
return PL_unify_atom(t, tmp_atom);
|
||||
#else
|
||||
const char *s = PyUnicode_AsUTF8(pVal);
|
||||
tmp_atom = PL_new_atom( s);
|
||||
return PL_unify_atom(t, tmp_atom);
|
||||
tmp_atom = PL_new_atom( s);
|
||||
#endif
|
||||
rc = PL_unify_atom(t, tmp_atom);
|
||||
} else if (PyByteArray_Check(pVal)) {
|
||||
atom_t tmp_atom = PL_new_atom(PyByteArray_AsString(pVal));
|
||||
return PL_unify_atom(t, tmp_atom);
|
||||
rc = PL_unify_atom(t, tmp_atom);
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
} else if (PyString_Check(pVal)) {
|
||||
atom_t tmp_atom = PL_new_atom(PyString_AsString(pVal));
|
||||
return PL_unify_atom(t, tmp_atom);
|
||||
rc = PL_unify_atom(t, tmp_atom);
|
||||
#endif
|
||||
} else if (PyTuple_Check(pVal)) {
|
||||
Py_ssize_t i, sz = PyTuple_Size(pVal);
|
||||
@@ -85,13 +83,10 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
if ((s = (Py_TYPE(pVal)->tp_name))) {
|
||||
if (!strcmp(s, "H")) {
|
||||
pVal = PyTuple_GetItem(pVal, 0);
|
||||
if (PyLong_Check(pVal)) {
|
||||
return PyLong_AsLong(pVal);
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
} else if (PyInt_Check(pVal)) {
|
||||
return PyInt_AsLong(pVal);
|
||||
#endif
|
||||
}
|
||||
if (pVal==NULL) {
|
||||
pVal = Py_None;
|
||||
PyErr_Clear();
|
||||
}
|
||||
}
|
||||
if (s[0] == '$') {
|
||||
char *ns = malloc(strlen(s) + 5);
|
||||
@@ -102,28 +97,44 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
} else {
|
||||
f = PL_new_functor(PL_new_atom(s), sz);
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
f = PL_new_functor(ATOM_t, sz);
|
||||
if (!PL_unify_functor(t, f))
|
||||
return FALSE;
|
||||
term_t to = PL_new_term_ref();
|
||||
for (i = 0; i < sz; i++) {
|
||||
if (!PL_get_arg(i + 1, t, to))
|
||||
return FALSE;
|
||||
if (!python_to_term(PyTuple_GetItem(pVal, i), to))
|
||||
return FALSE;
|
||||
}
|
||||
return true;
|
||||
if (PL_unify_functor(t, f)) {
|
||||
rc = true;
|
||||
for (i = 0; i < sz; i++) {
|
||||
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 = false;
|
||||
}
|
||||
fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" ||***\n",stderr);
|
||||
} else if (PyList_Check(pVal)) {
|
||||
term_t to = PL_new_term_ref();
|
||||
YAP_Term yt = YAP_GetFromSlot(t);
|
||||
Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
|
||||
|
||||
for (i = 0; i < sz; i++) {
|
||||
if (!PL_unify_list(t, to, t) ||
|
||||
!python_to_term(PyList_GetItem(pVal, i), to))
|
||||
return FALSE;
|
||||
PyObject *obj;
|
||||
if (!PL_unify_list(t, to, t)) {
|
||||
rc = false;
|
||||
break;
|
||||
}
|
||||
if ((obj = PyList_GetItem(pVal, i)) == NULL) {
|
||||
obj = Py_None;
|
||||
}
|
||||
rc = rc && python_to_term(obj, to);
|
||||
|
||||
}
|
||||
return PL_unify_nil(t);
|
||||
rc = rc && PL_unify_nil(t);
|
||||
fputs("[***] ", stderr);
|
||||
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;
|
||||
@@ -135,40 +146,39 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
tnew = PL_new_term_ref();
|
||||
/* do something interesting with the values... */
|
||||
if (!python_to_term(key, tkey)) {
|
||||
PL_reset_term_refs(tkey);
|
||||
return FALSE;
|
||||
continue;
|
||||
}
|
||||
if (!python_to_term(value, tval)) {
|
||||
PL_reset_term_refs(tkey);
|
||||
return FALSE;
|
||||
continue;
|
||||
}
|
||||
/* reuse */
|
||||
tint = tkey;
|
||||
if (!PL_cons_functor(tint, FUNCTOR_colon2, tkey, tval)) {
|
||||
PL_reset_term_refs(tkey);
|
||||
return FALSE;
|
||||
rc = false;
|
||||
continue;
|
||||
}
|
||||
if (--left) {
|
||||
if (!PL_cons_functor(tint, FUNCTOR_comma2, tint, tnew))
|
||||
PL_reset_term_refs(tkey);
|
||||
return FALSE;
|
||||
rc = false;
|
||||
}
|
||||
if (!PL_unify(ti, tint)) {
|
||||
PL_reset_term_refs(tkey);
|
||||
return FALSE;
|
||||
}
|
||||
rc = false; }
|
||||
ti = tnew;
|
||||
PL_reset_term_refs(tkey);
|
||||
}
|
||||
return PL_unify(t, to);
|
||||
rc = PL_unify(t, to);
|
||||
} else {
|
||||
return repr_term(pVal, t);
|
||||
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();
|
||||
if (!python_to_term(pVal, t))
|
||||
if (pVal == NULL ||
|
||||
!python_to_term(pVal, t))
|
||||
return 0;
|
||||
YAP_Term tt = YAP_GetFromSlot(t);
|
||||
PL_reset_term_refs(t);
|
||||
@@ -222,20 +232,25 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
|
||||
if (PL_skip_list(t, tail, &len) &&
|
||||
PL_get_nil(tail)) { // true list
|
||||
|
||||
if (PySequence_Check(exp) && PySequence_Length(exp) == len)
|
||||
bool o = true;
|
||||
if (PySequence_Check(exp) && PySequence_Length(exp) == len)
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (!PL_get_list(t, arg, t)) {
|
||||
PL_reset_term_refs(tail);
|
||||
return false;
|
||||
}
|
||||
if (!python_assign(arg, PySequence_GetItem(exp, i), context)) {
|
||||
PL_reset_term_refs(tail);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
PL_reset_term_refs(tail);
|
||||
return true;
|
||||
for (i = 0; i < len; i++) {
|
||||
PyObject *p;
|
||||
if (!PL_get_list(t, arg, t)) {
|
||||
PL_reset_term_refs(tail);
|
||||
o = false;
|
||||
p = Py_None;
|
||||
}
|
||||
if ((p = PySequence_GetItem(exp, i)) == NULL)
|
||||
p = Py_None;
|
||||
if (!python_assign(arg, p, context)) {
|
||||
PL_reset_term_refs(tail);
|
||||
o = false;
|
||||
}
|
||||
}
|
||||
PL_reset_term_refs(tail);
|
||||
return o;
|
||||
} else {
|
||||
functor_t fun;
|
||||
|
||||
@@ -266,6 +281,8 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
|
||||
}
|
||||
if (PyList_Check(i)) {
|
||||
i = PyList_GetItem(i, 0);
|
||||
if (i==NULL)
|
||||
i = Py_None;
|
||||
long int j;
|
||||
if (PyList_Check(o)) {
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
|
@@ -15,7 +15,9 @@
|
||||
static PyObject *finalLookup(PyObject *i, const char *s) {
|
||||
PyObject *rc;
|
||||
if (i == NULL)
|
||||
return NULL;
|
||||
return Py_None;
|
||||
if (strcmp(s,"none") == 0)
|
||||
return Py_None;
|
||||
if (PyDict_Check(i)) {
|
||||
if ((rc = PyDict_GetItemString(i, s)))
|
||||
return rc;
|
||||
@@ -31,6 +33,8 @@ static PyObject *finalLookup(PyObject *i, const char *s) {
|
||||
}
|
||||
|
||||
PyObject *PythonLookupSpecial(const char *s) {
|
||||
if (s == NULL)
|
||||
return Py_None;
|
||||
if (strcmp(s, "true") == 0) {
|
||||
return Py_True;
|
||||
}
|
||||
@@ -51,7 +55,9 @@ PyObject *PythonLookupSpecial(const char *s) {
|
||||
PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
|
||||
PyObject *out = NULL;
|
||||
if (!sp)
|
||||
return NULL;
|
||||
return Py_None;
|
||||
if (strcmp(sp, "none") == 0)
|
||||
return Py_None;
|
||||
if ((out = finalLookup(pContext, sp))) {
|
||||
return out;
|
||||
}
|
||||
@@ -64,7 +70,7 @@ PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
|
||||
}
|
||||
PyObject *py_Local = PyEval_GetLocals();
|
||||
if ((out = finalLookup(py_Local, sp))) {
|
||||
return out;
|
||||
return out;
|
||||
}
|
||||
PyObject *py_Global = PyEval_GetGlobals();
|
||||
if ((out = finalLookup(py_Global, sp))) {
|
||||
@@ -76,7 +82,7 @@ PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
|
||||
if ((out = finalLookup(py_Main, sp))) {
|
||||
return out;
|
||||
}
|
||||
return NULL;
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
int lookupPyModule(Py_mod *q) {
|
||||
@@ -120,10 +126,10 @@ PyObject *find_obj(PyObject *ob, term_t l, bool eval) {
|
||||
yt = YAP_GetFromSlot(l);
|
||||
while (YAP_IsPairTerm(yt)) {
|
||||
hd = YAP_HeadOfTerm(yt);
|
||||
ob = yap_to_python(hd, true, ob);
|
||||
ob = yap_to_python(hd, eval, ob);
|
||||
ob = CHECKNULL(yt, ob);
|
||||
if (!ob){
|
||||
return NULL;
|
||||
return Py_None;
|
||||
}
|
||||
yt = YAP_TailOfTerm(yt);
|
||||
|
||||
@@ -155,7 +161,7 @@ static PyObject *bip_abs(term_t t) {
|
||||
* Python all
|
||||
*
|
||||
* @param t Prolog term with a previously constructed Python iterator
|
||||
*
|
||||
> *
|
||||
* @return the Python boolean `True` if all elements of the iterator are `True`,
|
||||
* `False` otherwise.
|
||||
*/
|
||||
@@ -169,14 +175,14 @@ static PyObject *bip_all(term_t t) {
|
||||
v = CHECKNULL( t, v);
|
||||
it = PyObject_GetIter(v);
|
||||
if (CHECKNULL( t, it) == NULL)
|
||||
return NULL;
|
||||
return Py_None;
|
||||
iternext = *Py_TYPE(it)->tp_iternext;
|
||||
|
||||
if (PyErr_Occurred()) {
|
||||
if (PyErr_ExceptionMatches(PyExc_StopIteration))
|
||||
PyErr_Clear();
|
||||
else
|
||||
return NULL;
|
||||
return Py_None;
|
||||
}
|
||||
// PyObject_Print(v, stderr, 0);
|
||||
for (;;) {
|
||||
@@ -215,7 +221,7 @@ static PyObject *bip_any(term_t t) {
|
||||
v = term_to_python(t, true, NULL);
|
||||
it = PyObject_GetIter(v);
|
||||
if (CHECKNULL(t,it) == NULL)
|
||||
return NULL;
|
||||
return Py_None;
|
||||
iternext = *Py_TYPE(it)->tp_iternext;
|
||||
|
||||
for (;;) {
|
||||
@@ -265,7 +271,7 @@ static PyObject *bip_float(term_t t, bool eval) {
|
||||
} else if (PyFloat_Check(pVal)) {
|
||||
return pVal;
|
||||
} else
|
||||
return NULL;
|
||||
return Py_None;
|
||||
Py_DECREF(pVal);
|
||||
return o;
|
||||
}
|
||||
@@ -291,7 +297,7 @@ static PyObject *bip_int(term_t t) {
|
||||
o = PyLong_FromDouble(PyFloat_AsDouble(pVal));
|
||||
#endif
|
||||
} else
|
||||
return NULL;
|
||||
return Py_None;
|
||||
Py_DECREF(pVal);
|
||||
return o;
|
||||
}
|
||||
@@ -310,7 +316,7 @@ static PyObject *bip_long(term_t t) {
|
||||
} else if (PyFloat_Check(pVal)) {
|
||||
o = pVal;
|
||||
} else
|
||||
return NULL;
|
||||
return Py_None;
|
||||
Py_DECREF(pVal);
|
||||
return o;
|
||||
}
|
||||
@@ -344,19 +350,19 @@ static PyObject *bip_ord(term_t t) {
|
||||
return PyLong_FromLong(ord);
|
||||
#endif
|
||||
}
|
||||
return NULL;
|
||||
return Py_None;
|
||||
} else if (PyByteArray_Check(pVal)) {
|
||||
char *s = PyByteArray_AsString(pVal);
|
||||
|
||||
if (s[1])
|
||||
return NULL;
|
||||
return Py_None;
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
return PyInt_FromLong(s[0]);
|
||||
} else if (PyString_Check(pVal)) {
|
||||
char *s = PyString_AsString(pVal);
|
||||
|
||||
if (s[1])
|
||||
return NULL;
|
||||
return Py_None;
|
||||
return PyInt_FromLong(s[0]);
|
||||
#else
|
||||
return PyLong_FromLong(s[0]);
|
||||
@@ -477,7 +483,7 @@ static PyObject *bip_sum(term_t t) {
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (PyInt_CheckExact(item)) {
|
||||
PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
|
||||
764PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
|
||||
f_result += (double)PyInt_AS_LONG(item);
|
||||
PyFPE_END_PROTECT(f_result) Py_DECREF(item);
|
||||
continue;
|
||||
@@ -580,7 +586,7 @@ static long get_len_of_range(long lo, long hi, long step) {
|
||||
return n;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3 && defined(USE_NAMEDTUPLES)
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
static PyStructSequence_Field pnull[] = {
|
||||
{"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL},
|
||||
{"A5", NULL}, {"A6", NULL}, {"A7", NULL}, {"A8", NULL},
|
||||
@@ -719,9 +725,9 @@ static PyObject *structseq_repr(PyObject *iobj) {
|
||||
}
|
||||
#endif
|
||||
|
||||
PyObject *term_to_nametuple(const char *s, int arity, term_t t) {
|
||||
PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
|
||||
PyObject *o;
|
||||
#if PY_MAJOR_VERSION >= 3 && 0
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
PyTypeObject *typp;
|
||||
PyObject *key = PyUnicode_FromString(s);
|
||||
if (py_F2P && PyDict_Contains(py_F2P, key)) {
|
||||
@@ -748,36 +754,21 @@ PyObject *term_to_nametuple(const char *s, int arity, term_t t) {
|
||||
if (py_F2P)
|
||||
PyDict_SetItem(py_F2P, key, (PyObject *)typp);
|
||||
}
|
||||
#endif
|
||||
o = PyTuple_New(arity);
|
||||
DebugPrintf("Tuple %p\n", o);
|
||||
if (o == NULL)
|
||||
return o;
|
||||
term_t tleft = PL_new_term_ref();
|
||||
int i;
|
||||
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, t, tleft))
|
||||
return NULL;
|
||||
pArg = term_to_python(tleft, false, NULL );
|
||||
if (CHECKNULL(tleft,pArg))
|
||||
return NULL;
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
/* pArg reference stolen here: */
|
||||
Py_INCREF(pArg);
|
||||
o = PyStructSequence_New(typp);
|
||||
for (arity_t i = 0; i < arity; i++) {
|
||||
PyObject *pArg = PyTuple_GET_ITEM(tuple, i);
|
||||
if (pArg)
|
||||
PyStructSequence_SET_ITEM(o, i, pArg);
|
||||
PyObject_Print(pArg,stderr,0);fputc('\n',stderr);
|
||||
}
|
||||
((PyStructSequence *)o)->ob_base.ob_size = arity;
|
||||
PyObject_Print(o,stderr,0);fputc('\n',stderr);
|
||||
return o;
|
||||
#else
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SET_ITEM(o, i, pArg);
|
||||
}
|
||||
PyObject *o1;
|
||||
o1 = PyTuple_New(2);
|
||||
PyTuple_SET_ITEM(o1, 0, PyUnicode_FromString(s));
|
||||
PyTuple_SET_ITEM(o1, 1, o);
|
||||
PyTuple_SET_ITEM(o1, 1, tuple);
|
||||
return o1;
|
||||
#endif
|
||||
}
|
||||
@@ -820,7 +811,7 @@ static PyObject *bip_range(term_t t) {
|
||||
v = PyList_New(n);
|
||||
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
return Py_None;
|
||||
for (i = 0; i < n; i++) {
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
PyObject *w = PyInt_FromLong(ilow);
|
||||
@@ -959,6 +950,8 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
|
||||
#else
|
||||
if (PyLong_Check(ip)) {
|
||||
PyObject *o = PySequence_GetItem(v, PyLong_AsLong(ip));
|
||||
if (o == NULL)
|
||||
o = Py_None;
|
||||
if (CHECKNULL(t,o) == NULL)
|
||||
return NULL;
|
||||
Py_INCREF(o);
|
||||
@@ -967,6 +960,8 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
|
||||
#endif
|
||||
} else {
|
||||
o = PyObject_GetItem(v, ip);
|
||||
if (o == NULL)
|
||||
o = Py_None;
|
||||
Py_INCREF(o);
|
||||
return o;
|
||||
}
|
||||
@@ -1073,20 +1068,32 @@ PyObject *compound_to_pytree(term_t t, PyObject *context) {
|
||||
}
|
||||
term_t tleft;
|
||||
int i;
|
||||
o = PyTuple_New(arity);
|
||||
PyObject *out = PyTuple_New(arity);
|
||||
DebugPrintf("Tuple %p\n", o);
|
||||
tleft = PL_new_term_ref();
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
AOK (PL_get_arg(i + 1, t, tleft), NULL );
|
||||
pArg = term_to_python(tleft, false, NULL);
|
||||
if (pArg == NULL)
|
||||
return NULL;
|
||||
if (pArg) {
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SET_ITEM(o, i, pArg);
|
||||
PyTuple_SET_ITEM(out, i, pArg);
|
||||
Py_INCREF(pArg);
|
||||
}
|
||||
}
|
||||
return term_to_nametuple(s, arity, t);
|
||||
if (CHECKNULL(t, out) == NULL) {
|
||||
PyErr_Print();
|
||||
return NULL;
|
||||
}
|
||||
PyObject *c = lookupPySymbol(s, o, NULL);
|
||||
|
||||
if ( c && PyCallable_Check(c)) {
|
||||
PyObject *n = PyTuple_New(arity);
|
||||
PyTuple_SET_ITEM(n, 0, c);
|
||||
PyTuple_SET_ITEM(n, 1, out);
|
||||
return n;
|
||||
}
|
||||
return term_to_nametuple(s, arity, out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1218,9 +1225,6 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
|
||||
PyErr_Print();
|
||||
return NULL;
|
||||
}
|
||||
if ( !PyCallable_Check(o)) {
|
||||
return term_to_nametuple(s, arity, t);
|
||||
}
|
||||
PyObject *pArgs = PyTuple_New(arity);
|
||||
DebugPrintf("Tuple %p\n", pArgs);
|
||||
int i;
|
||||
@@ -1230,16 +1234,23 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
|
||||
AOK (PL_get_arg(i + 1, t, tleft), NULL );
|
||||
/* ignore (_) */
|
||||
if (i == 0 && PL_is_variable(tleft)) {
|
||||
Py_DECREF(pArgs);
|
||||
pArgs = Py_None;
|
||||
pArg = Py_None;
|
||||
} else {
|
||||
pArg = term_to_python(tleft, true, NULL);
|
||||
PyObject_Print(pArg,fdopen(2,"w"),0);
|
||||
if (pArg == NULL) {
|
||||
pArg = Py_None;
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
Py_INCREF(pArg);
|
||||
}
|
||||
pArg = term_to_python(tleft, true, NULL);
|
||||
pArg = CHECKNULL( tleft, pArg );
|
||||
/* pArg reference stolen here: */
|
||||
Py_INCREF(pArg);
|
||||
|
||||
PyTuple_SetItem(pArgs, i, pArg);
|
||||
}
|
||||
PyObject *rc;
|
||||
if ( !PyCallable_Check(o)) {
|
||||
return term_to_nametuple(s, arity, pArgs);
|
||||
}
|
||||
PyObject *rc;
|
||||
|
||||
//PyObject_Print(pArgs, stderr, 0);
|
||||
//PyObject_Print(o, stderr, 0);
|
||||
|
@@ -23,6 +23,23 @@ X_API PyObject *py_F2P;
|
||||
|
||||
bool python_in_python;
|
||||
|
||||
static void add_modules(void) {
|
||||
py_Main = PyImport_AddModule("__main__");
|
||||
Py_INCREF(py_Main);
|
||||
py_Sys = PyImport_AddModule("sys");
|
||||
Py_INCREF(py_Sys);
|
||||
py_Builtin = PyImport_AddModule("__builtin__");
|
||||
Py_INCREF(py_Builtin);
|
||||
py_ModDict = PyObject_GetAttrString(py_Sys, "modules");
|
||||
// py_Yapex = PyImport_ImportModule("yap4py.yapi");
|
||||
// PyObject *py_Yap =
|
||||
PyImport_ImportModule("yap");
|
||||
if (py_Yapex)
|
||||
Py_INCREF(py_Yapex);
|
||||
//py_F2P = PyObject_GetAttrString(py_Yap, "globals");
|
||||
py_F2P = NULL;
|
||||
}
|
||||
|
||||
static void install_py_constants(void) {
|
||||
FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
|
||||
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
|
||||
@@ -35,7 +52,6 @@ static void install_py_constants(void) {
|
||||
ATOM_true = PL_new_atom("true");
|
||||
ATOM_false = PL_new_atom("false");
|
||||
ATOM_dot = PL_new_atom(".");
|
||||
ATOM_none = PL_new_atom("none");
|
||||
ATOM_self = PL_new_atom("self");
|
||||
ATOM_t = PL_new_atom("t");
|
||||
FUNCTOR_abs1 = PL_new_functor(PL_new_atom("abs"), 1);
|
||||
@@ -68,24 +84,11 @@ static void install_py_constants(void) {
|
||||
FUNCTOR_comma2 = PL_new_functor(PL_new_atom(","), 2);
|
||||
FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
|
||||
FUNCTOR_sqbrackets2 = PL_new_functor(PL_new_atom("[]"), 2);
|
||||
// if (python_in_python) {
|
||||
py_Main = PyImport_AddModule("__main__");
|
||||
Py_INCREF(py_Main);
|
||||
py_Sys = PyImport_AddModule("sys");
|
||||
Py_INCREF(py_Sys);
|
||||
py_Builtin = PyImport_AddModule("__builtin__");
|
||||
Py_INCREF(py_Builtin);
|
||||
py_ModDict = PyObject_GetAttrString(py_Sys, "modules");
|
||||
py_Yapex = PyImport_ImportModule("yapex");
|
||||
// PyObject *py_Yap =
|
||||
PyImport_ImportModule("yap");
|
||||
Py_INCREF(py_Yapex);
|
||||
//py_F2P = PyObject_GetAttrString(py_Yap, "globals");
|
||||
py_F2P = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
foreign_t end_python(void) {
|
||||
Py_Finalize();
|
||||
if (!python_in_python)
|
||||
Py_Finalize();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -100,12 +103,14 @@ bool do_init_python(void) {
|
||||
libpython_initialized = true;
|
||||
// PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
term_t t = PL_new_term_ref();
|
||||
Py_Initialize();
|
||||
if (!python_in_python)
|
||||
Py_Initialize();
|
||||
install_py_constants();
|
||||
PL_reset_term_refs(t);
|
||||
install_pypreds();
|
||||
install_pl2pl();
|
||||
// PyGILState_Release(gstate);
|
||||
add_modules();
|
||||
return !python_in_python;
|
||||
|
||||
}
|
||||
|
@@ -133,7 +133,7 @@ static inline PyObject *atom_to_python_string(term_t t) {
|
||||
YE(t,__LINE__,__FILE__,__FUNCTION__);\
|
||||
PyErr_Print(); PyErr_Clear();\
|
||||
}
|
||||
|
||||
|
||||
#define CHECKNULL(t,rc) (rc != NULL ? rc : YE(t,__LINE__,__FILE__,__FUNCTION__) )
|
||||
#define AOK(rc, err) { if (!rc) YEM( #rc ,__LINE__,__FILE__,__FUNCTION__); }
|
||||
|
||||
@@ -165,7 +165,7 @@ extern PyObject *compound_to_pytree(term_t t, PyObject *context);
|
||||
|
||||
extern PyObject *term_to_python(term_t t, bool eval, PyObject *contextxs);
|
||||
|
||||
extern PyObject *term_to_nametuple(const char *s, int arity, term_t t);
|
||||
extern PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *);
|
||||
|
||||
extern foreign_t python_to_term(PyObject *pVal, term_t t);
|
||||
extern bool python_assign(term_t t, PyObject *exp, PyObject *context);
|
||||
|
@@ -39,7 +39,7 @@ setup_args = dict(
|
||||
name = 'yap_kernel',
|
||||
version = '0.0.1',
|
||||
packages = ["yap_kernel"],
|
||||
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python' },
|
||||
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python/yap_kernel' },
|
||||
description = "YAP Kernel for Jupyter",
|
||||
long_description="A simple YAP kernel for Jupyter/IPython",
|
||||
url="https://github.com/vscosta/yap-6.3",
|
||||
|
@@ -1,101 +0,0 @@
|
||||
|
||||
import yap
|
||||
import sys
|
||||
# debugging support.
|
||||
import pdb
|
||||
|
||||
def query_prolog(engine, s):
|
||||
|
||||
def answer(q):
|
||||
try:
|
||||
return q.next()
|
||||
except Exception as e:
|
||||
print(e.args[1])
|
||||
return False
|
||||
|
||||
#
|
||||
#construct a query from a one-line string
|
||||
# q is opaque to Python
|
||||
q = engine.query(s)
|
||||
# vs is the list of variables
|
||||
# you can print it out, the left-side is the variable name,
|
||||
# the right side wraps a handle to a variable
|
||||
vs = q.namedVars()
|
||||
# atom match either symbols, or if no symbol exists, sttrings, In this case
|
||||
# variable names should match strings
|
||||
for eq in vs:
|
||||
if not isinstance(eq[0],str):
|
||||
print( "Error: Variable Name matches a Python Symbol")
|
||||
return
|
||||
ask = True
|
||||
# launch the query
|
||||
while answer(q):
|
||||
# this new vs should contain bindings to vars
|
||||
vs= q.namedVars()
|
||||
#numbervars
|
||||
i=0
|
||||
# iteratw
|
||||
for eq in vs:
|
||||
name = eq[0]
|
||||
# this is tricky, we're going to bind the variables in the term so thay we can
|
||||
# output X=Y. The Python way is to use dictionares.
|
||||
#Instead, we use the T function to tranform the Python term back to Prolog
|
||||
binding = yap.T(eq[1])
|
||||
if binding.isVar():
|
||||
binding.unify(name)
|
||||
else:
|
||||
i = binding.numberVars(i, True)
|
||||
print(name + " = " + binding.text())
|
||||
#ok, that was Prolog code
|
||||
print("yes")
|
||||
# deterministic = one solution
|
||||
if q.deterministic():
|
||||
# done
|
||||
q.close()
|
||||
return
|
||||
if ask:
|
||||
s = input("more(;), all(*), no(\\n), python(#) ?").lstrip()
|
||||
if s.startswith(';') or s.startswith('y'):
|
||||
continue
|
||||
elif s.startswith('#'):
|
||||
try:
|
||||
exec(s.lstrip('#'))
|
||||
except:
|
||||
raise
|
||||
elif s.startswith('*') or s.startswith('a'):
|
||||
ask = False
|
||||
continue
|
||||
else:
|
||||
break
|
||||
print("No (more) answers")
|
||||
q.close()
|
||||
return
|
||||
|
||||
|
||||
def live():
|
||||
engine = yap.YAPEngine()
|
||||
loop = True
|
||||
pdb.set_trace()
|
||||
while loop:
|
||||
try:
|
||||
s = input("?- ")
|
||||
if not s:
|
||||
loop = False
|
||||
query_prolog(engine, s)
|
||||
except SyntaxError as err:
|
||||
print("Syntax Error error: {0}".format(err))
|
||||
except EOFError:
|
||||
return
|
||||
except RuntimeError as err:
|
||||
print("YAP Execution Error: {0}".format(err))
|
||||
except ValueError:
|
||||
print("Could not convert data to an integer.")
|
||||
except:
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
raise
|
||||
engine.close()
|
||||
#
|
||||
# initialize engine
|
||||
# engine = yap.YAPEngine();
|
||||
# engine = yap.YAPEngine(yap.YAPParams());
|
||||
#live()
|
@@ -1,101 +0,0 @@
|
||||
|
||||
import yap
|
||||
import sys
|
||||
# debugging support.
|
||||
import pdb
|
||||
|
||||
def query_prolog(engine, s):
|
||||
|
||||
def answer(q):
|
||||
try:
|
||||
return q.next()
|
||||
except Exception as e:
|
||||
print(e.args[1])
|
||||
return False
|
||||
|
||||
#
|
||||
#construct a query from a one-line string
|
||||
# q is opaque to Python
|
||||
q = engine.query(s)
|
||||
# vs is the list of variables
|
||||
# you can print it out, the left-side is the variable name,
|
||||
# the right side wraps a handle to a variable
|
||||
vs = q.namedVars()
|
||||
# atom match either symbols, or if no symbol exists, sttrings, In this case
|
||||
# variable names should match strings
|
||||
for eq in vs:
|
||||
if not isinstance(eq[0],str):
|
||||
print( "Error: Variable Name matches a Python Symbol")
|
||||
return
|
||||
ask = True
|
||||
# launch the query
|
||||
while answer(q):
|
||||
# this new vs should contain bindings to vars
|
||||
vs= q.namedVars()
|
||||
#numbervars
|
||||
i=0
|
||||
# iteratw
|
||||
for eq in vs:
|
||||
name = eq[0]
|
||||
# this is tricky, we're going to bind the variables in the term so thay we can
|
||||
# output X=Y. The Python way is to use dictionares.
|
||||
#Instead, we use the T function to tranform the Python term back to Prolog
|
||||
binding = yap.T(eq[1])
|
||||
if binding.isVar():
|
||||
binding.unify(name)
|
||||
else:
|
||||
i = binding.numberVars(i, True)
|
||||
print(name + " = " + binding.text())
|
||||
#ok, that was Prolog code
|
||||
print("yes")
|
||||
# deterministic = one solution
|
||||
if q.deterministic():
|
||||
# done
|
||||
q.close()
|
||||
return
|
||||
if ask:
|
||||
s = input("more(;), all(*), no(\\n), python(#) ?").lstrip()
|
||||
if s.startswith(';') or s.startswith('y'):
|
||||
continue
|
||||
elif s.startswith('#'):
|
||||
try:
|
||||
exec(s.lstrip('#'))
|
||||
except:
|
||||
raise
|
||||
elif s.startswith('*') or s.startswith('a'):
|
||||
ask = False
|
||||
continue
|
||||
else:
|
||||
break
|
||||
print("No (more) answers")
|
||||
q.close()
|
||||
return
|
||||
|
||||
|
||||
def live():
|
||||
engine = yap.YAPEngine()
|
||||
loop = True
|
||||
pdb.set_trace()
|
||||
while loop:
|
||||
try:
|
||||
s = input("?- ")
|
||||
if not s:
|
||||
loop = False
|
||||
query_prolog(engine, s)
|
||||
except SyntaxError as err:
|
||||
print("Syntax Error error: {0}".format(err))
|
||||
except EOFError:
|
||||
return
|
||||
except RuntimeError as err:
|
||||
print("YAP Execution Error: {0}".format(err))
|
||||
except ValueError:
|
||||
print("Could not convert data to an integer.")
|
||||
except:
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
raise
|
||||
engine.close()
|
||||
#
|
||||
# initialize engine
|
||||
# engine = yap.YAPEngine();
|
||||
# engine = yap.YAPEngine(yap.YAPParams());
|
||||
#live()
|
Reference in New Issue
Block a user