This commit is contained in:
Vitor Santos Costa
2016-10-16 17:18:51 -05:00
parent 4a5540e645
commit 08dd1dcdb3
22 changed files with 247 additions and 111 deletions

View File

@@ -15,7 +15,7 @@ set (PYTHON_HEADERS
target_link_libraries(YAPPython libYap ${PYTHON_LIBRARIES})
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
add_custom_target ( YAPex ALL
COMMAND ${PYTHON_EXECUTABLE} setup.py build -f

View File

@@ -2,18 +2,23 @@
#include "python.h"
PyObject *yap_to_python(YAP_Term t, bool eval) {
term_t yt = PL_new_term_ref();
PyObject *o = term_to_python(yt, eval);
PL_reset_term_refs(yt);
return o;
}
/**
* term_to_python translates and evaluates from Prolog to Python
*
* @param t handle to Prolog term
* @param t whether should try to evaluate evaluables.
* @param t whether should try to evaluate evaluables.
*
* @return a Python object descriptor or NULL if failed
*/
PyObject *term_to_python(term_t t, bool eval) {
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
// am\n");
YAP_Term yt = YAP_GetFromSlot(t);
switch (PL_term_type(t)) {
case PL_VARIABLE: {
@@ -64,7 +69,7 @@ PyObject *term_to_python(term_t t, bool eval) {
if (!c && PyObject_HasAttrString(py_Yapex, "A"))
c = PyObject_GetAttrString(py_Yapex, "A");
if (!c || !PyCallable_Check(c)) {
return NULL;
return o;
} else {
PyObject *t = PyTuple_New(1);
PyTuple_SET_ITEM(t, 0, PyUnicode_FromString(s));
@@ -79,7 +84,7 @@ PyObject *term_to_python(term_t t, bool eval) {
case PL_STRING: {
char *s = NULL;
if (!PL_get_chars(t, &s,
REP_UTF8 | CVT_ATOM | CVT_STRING | BUF_DISCARDABLE)) {
REP_UTF8 | CVT_ATOM | CVT_STRING | BUF_MALLOC)) {
return NULL;
}
#if PY_MAJOR_VERSION < 3
@@ -90,6 +95,10 @@ PyObject *term_to_python(term_t t, bool eval) {
{
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
// fprintf(stderr, "%s\n", s);
free(s);
if (pobj) {
Py_IncRef(pobj);
}
return pobj;
}
} break;

View File

@@ -254,7 +254,7 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
return rc;
#else
// new interface
char *s = PyUnicode_AsUTF8AndSize(pVal, &sz);
char *s = PyUnicode_AsUTF8AndSize(pValR, &sz);
return repr_term(s, sz, t);
#endif
}

View File

@@ -651,7 +651,7 @@ term_to_nametuple( const char *s, int arity, term_t t) {
PyTypeObject *typp;
PyObject *o;
PyObject *key = PyUnicode_FromString(s);
if (PyDict_Contains(py_F2P, key)) {
if (py_F2P && PyDict_Contains(py_F2P, key)) {
typp = (PyTypeObject*)PyDict_GetItem(py_F2P, key);
} else {
@@ -670,7 +670,8 @@ if (PyDict_Contains(py_F2P, key)) {
Py_INCREF(typp);
// typp->tp_flags |= Py_TPFLAGS_HEAPTYPE;
PyModule_AddObject(py_Yapex, s, (PyObject *)typp);
PyDict_SetItem(py_F2P, key, (PyObject *)typp);
if (py_F2P)
PyDict_SetItem(py_F2P, key, (PyObject *)typp);
}
o = PyStructSequence_New(typp);
term_t tleft = PL_new_term_ref();
@@ -1264,7 +1265,9 @@ PyObject *compound_to_pyeval(term_t t, functor_t fun) {
/* pArg reference stolen here: */
PyTuple_SetItem(pArgs, i, pArg);
}
return PyObject_CallObject(o, pArgs);
PyObject *rc;
rc = PyObject_CallObject(o, pArgs);
return rc;
} else {
atom_t name;
int len;

View File

@@ -94,6 +94,8 @@ static inline PyObject *atom_to_python_string(term_t t) {
extern PyObject *compound_to_pyeval(term_t t, functor_t fun);
extern PyObject *compound_to_pytree(term_t t, functor_t fun);
extern PyObject *yap_to_python(YAP_Term t, bool eval);
extern PyObject *term_to_python(term_t t, bool eval);
extern foreign_t python_to_ptr(PyObject *pVal, term_t t);