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

@@ -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;