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

View File

@@ -14,7 +14,8 @@ setup(
ext_modules=[Extension('_yap', ['yap.i'],
define_macros = [('MAJOR_VERSION', '1'),
('MINOR_VERSION', '0'),
('_YAP_NOT_INSTALLED_', '1')],
('_YAP_NOT_INSTALLED_', '1'),
('YAP_PYTHON', '1')],
runtime_library_dirs=['${dlls}'],
swig_opts=['-modern','-outcurrentdir', '-c++', '-py3','-I${CMAKE_SOURCE_DIR}/CXX'],
library_dirs=['../../..','../../../CXX',

View File

@@ -14,51 +14,49 @@ class YAPEngine;
#ifdef SWIGPYTHON
%typemap(typecheck) YAPTerm* {
%typemap(typecheck) Term* {
$1 = PySequence_Check($input);
}
// Map a Python sequence into any sized C double array
%typemap(in) YAPTerm* {
%typemap(in) Term* {
int i;
if (!PySequence_Check($input)) {
PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
$1 = nullptr;
} else {
int sz = PyObject_Length($input);
std::vector<YAPTerm> v(sz);
std::vector<Term> v(sz);
for (i =0; i < sz; i++) {
PyObject *o = PySequence_GetItem($input,i);
v[i] = YAPTerm(pythonToYAP(o));
v[i] = Term(pythonToYAP(o));
Py_DECREF(o);
}
}
$1 = &v[0];
}
}
%typemap(typecheck) YAPTerm {
%typemap(typecheck) YPTerm {
$1 = true;
}
%typemap(in) Term { $1 = pythonToYAP($input); }
%typemap(in) YAPTerm { $1 = YAPTerm(pythonToYAP($input)); }
%typemap(out) Term { return $result = yap_to_python($1, false);}
%typemap(out) YAPTerm {$result = term_to_python($1.handle(), false);}
%extend(out) YAPTerm{YAPTerm & __getitem__(size_t i){Term t0 = $self->term();
%extend(out) Term{Term & __getitem__(size_t i){Term t0 = $self;
if (IsApplTerm(t0)) {
Functor f = FunctorOfTerm(t0);
if (!IsExtensionFunctor(f))
return *new YAPTerm(ArgOfTerm(i + 1, t0));
return (ArgOfTerm(i + 1, t0);
} else if (IsPairTerm(t0)) {
if (i == 0)
return *new YAPTerm(HeadOfTerm(t0));
return HeadOfTerm(t0);
else if (i == 1)
return *new YAPTerm(TailOfTerm(t0));
return TailOfTerm(t0);
}
}
}