Merge branch 'master' of https://github.com/vscosta/yap-6.3
This commit is contained in:
commit
efaf86c675
@ -150,19 +150,19 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
|||||||
out = PyList_New(len);
|
out = PyList_New(len);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return CHECKNULL(t, Py_None);
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (!PL_get_list(t, arg, t)) {
|
if (!PL_get_list(t, arg, t)) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return Py_None;
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||||
}
|
}
|
||||||
a = term_to_python(arg, eval, o);
|
a = term_to_python(arg, eval, o);
|
||||||
if (a) {
|
if (a) {
|
||||||
if (PyList_SetItem(out, i, a) < 0) {
|
if (PyList_SetItem(out, i, a) < 0) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return Py_None;
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,11 +170,33 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
|||||||
return CHECKNULL(t, out);
|
return CHECKNULL(t, out);
|
||||||
} else {
|
} else {
|
||||||
functor_t fun;
|
functor_t fun;
|
||||||
|
atom_t name;
|
||||||
|
int arity;
|
||||||
PyObject *rc;
|
PyObject *rc;
|
||||||
|
|
||||||
if (!PL_get_functor(t, &fun)) {
|
if (!PL_get_functor(t, &fun)) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return CHECKNULL(t, Py_None);
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||||
|
}
|
||||||
|
AOK(PL_get_name_arity(t, &name, &arity), NULL);
|
||||||
|
if (name == ATOM_t) {
|
||||||
|
int i;
|
||||||
|
rc = PyTuple_New(arity);
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
term_t arg = PL_new_term_ref();
|
||||||
|
if (!PL_get_arg(i+1, t, arg)) {
|
||||||
|
PL_reset_term_refs(arg);
|
||||||
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "t(...)->python");
|
||||||
|
}
|
||||||
|
PyObject *a = term_to_python(arg, eval, o);
|
||||||
|
if (a) {
|
||||||
|
if (PyTuple_SetItem(rc, i, a) < 0) {
|
||||||
|
PL_reset_term_refs(arg);
|
||||||
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "t(...)->python");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PL_reset_term_refs(arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (eval)
|
if (eval)
|
||||||
rc = compound_to_pyeval(t, o);
|
rc = compound_to_pyeval(t, o);
|
||||||
@ -185,7 +207,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return CHECKNULL(t, Py_None);
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
|
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
|
||||||
@ -209,3 +231,7 @@ PyObject *deref_term_to_python(term_t t) {
|
|||||||
}
|
}
|
||||||
return term_to_python(t, false, NULL);
|
return term_to_python(t, false, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
|
||||||
|
yap_error_number type, term_t where, ...);
|
||||||
|
@ -1,5 +1,55 @@
|
|||||||
|
|
||||||
#include "py4yap.h"
|
#include "py4yap.h"
|
||||||
|
#include <frameobject.h>
|
||||||
|
|
||||||
|
void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
|
||||||
|
yap_error_number type, term_t where, ...) {
|
||||||
|
va_list ap;
|
||||||
|
char tmpbuf[MAXPATHLEN];
|
||||||
|
YAP_Term wheret = YAP_GetFromSlot(where);
|
||||||
|
|
||||||
|
if (PyErr_Occurred()) {
|
||||||
|
PyErr_Print();
|
||||||
|
} else {
|
||||||
|
PyObject *ptype, *pvalue, *ptraceback;
|
||||||
|
PyObject *pystr, *module_name, *pyth_module, *pyth_func;
|
||||||
|
|
||||||
|
/* See if we can get a full traceback */
|
||||||
|
module_name = PyUnicode_FromString("traceback");
|
||||||
|
pyth_module = PyImport_Import(module_name);
|
||||||
|
Py_DECREF(module_name);
|
||||||
|
|
||||||
|
if (pyth_module != NULL) {
|
||||||
|
pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
|
||||||
|
if (pyth_func && PyCallable_Check(pyth_func)) {
|
||||||
|
PyObject *pyth_val;
|
||||||
|
|
||||||
|
pyth_val = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
|
||||||
|
|
||||||
|
pystr = PyObject_Str(pyth_val);
|
||||||
|
fprintf(stderr, "%s", PyUnicode_AsUTF8(pystr));
|
||||||
|
Py_DECREF(pyth_val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PyFrameObject *fr;
|
||||||
|
if ((fr = PyEval_GetFrame())) {
|
||||||
|
fprintf(stderr, "at frame %p, line %d\n", fr, PyFrame_GetLineNumber(fr));
|
||||||
|
}
|
||||||
|
va_start(ap, where);
|
||||||
|
char *format = va_arg(ap, char *);
|
||||||
|
if (format != NULL) {
|
||||||
|
#if HAVE_VSNPRINTF
|
||||||
|
(void)vsnprintf(tmpbuf, MAXPATHLEN - 1, format, ap);
|
||||||
|
#else
|
||||||
|
(void)vsprintf(tnpbuf, format, ap);
|
||||||
|
#endif
|
||||||
|
// fprintf(stderr, "warning: ");
|
||||||
|
Yap_ThrowError__(file, function, lineno, type, wheret, tmpbuf);
|
||||||
|
} else {
|
||||||
|
Yap_ThrowError__(file, function, lineno, type, wheret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static foreign_t repr_term(PyObject *pVal, term_t t) {
|
static foreign_t repr_term(PyObject *pVal, term_t t) {
|
||||||
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
|
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
|
||||||
|
@ -197,4 +197,9 @@ extern PyObject *PythonLookup(const char *s, PyObject *o);
|
|||||||
|
|
||||||
extern PyObject *PythonLookupSpecial(const char *s);
|
extern PyObject *PythonLookupSpecial(const char *s);
|
||||||
|
|
||||||
|
#define YAPPy_ThrowError(id, inp, ...) \
|
||||||
|
YAPPy_ThrowError__(__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__)
|
||||||
|
|
||||||
|
extern void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
|
||||||
|
yap_error_number type, term_t where, ...);
|
||||||
#endif
|
#endif
|
||||||
|
@ -716,17 +716,19 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
|
|||||||
desc->n_in_sequence = arity;
|
desc->n_in_sequence = arity;
|
||||||
if (PyStructSequence_InitType2(typp, desc) < 0)
|
if (PyStructSequence_InitType2(typp, desc) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
typp->tp_flags &= ~Py_TPFLAGS_HEAPTYPE;
|
// typp->tp_flags &= ~Py_TPFLAGS_HEAPTYPE;
|
||||||
|
// typp->tp_flags &= ~Py_TPFLAGS_HAVE_GC;
|
||||||
// typp->tp_str = structseq_str;
|
// typp->tp_str = structseq_str;
|
||||||
typp->tp_repr = structseq_repr;
|
typp->tp_repr = structseq_repr;
|
||||||
// typp = PyStructSequence_NewType(desc);
|
// typp = PyStructSequence_NewType(desc);
|
||||||
// don't do this: we cannot add a type as an atribute.
|
// don't do this: we cannot add a type as an atribute.
|
||||||
// PyModule_AddObject(py_Main, s, (PyObject *)typp);
|
// PyModule_AddGObject(py_Main, s, (PyObject *)typp);
|
||||||
if (py_F2P)
|
if (py_F2P)
|
||||||
PyDict_SetItem(py_F2P, key, (PyObject *)typp);
|
PyDict_SetItem(py_F2P, key, (PyObject *)typp);
|
||||||
Py_INCREF(typp);
|
Py_INCREF(typp);
|
||||||
}
|
}
|
||||||
o = PyStructSequence_New(typp);
|
o = PyStructSequence_New(typp);
|
||||||
|
Py_INCREF(typp);
|
||||||
arity_t i;
|
arity_t i;
|
||||||
for (i = 0; i < arity; i++) {
|
for (i = 0; i < arity; i++) {
|
||||||
PyObject *pArg = PyTuple_GET_ITEM(tuple, i);
|
PyObject *pArg = PyTuple_GET_ITEM(tuple, i);
|
||||||
@ -747,7 +749,7 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *bip_range(term_t t) {
|
static PyObject *bip_range(term_t t) {
|
||||||
long ilow = 0, ihigh = 0, istep = 1;
|
long ilow = 0, ihigh = 0, istep = 1;
|
||||||
long bign;
|
long bign;
|
||||||
Py_ssize_t i, n;
|
Py_ssize_t i, n;
|
||||||
@ -802,9 +804,9 @@ static PyObject *bip_range(term_t t) {
|
|||||||
ilow += istep;
|
ilow += istep;
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
|
static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
|
||||||
bool eval) {
|
bool eval) {
|
||||||
PyObject *lhs, *rhs;
|
PyObject *lhs, *rhs;
|
||||||
term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref();
|
term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref();
|
||||||
@ -838,9 +840,9 @@ static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
|
|||||||
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
||||||
// Py_DECREF(lhs);
|
// Py_DECREF(lhs);
|
||||||
// Py_DECREF(rhs);
|
// Py_DECREF(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
|
PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
|
||||||
atom_t name;
|
atom_t name;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
@ -1013,9 +1015,9 @@ PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
|
|||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *compound_to_pytree(term_t t, PyObject *context) {
|
PyObject *compound_to_pytree(term_t t, PyObject *context) {
|
||||||
PyObject *o = py_Main, *no;
|
PyObject *o = py_Main, *no;
|
||||||
functor_t fun;
|
functor_t fun;
|
||||||
atom_t name;
|
atom_t name;
|
||||||
@ -1069,9 +1071,9 @@ PyObject *compound_to_pytree(term_t t, PyObject *context) {
|
|||||||
}
|
}
|
||||||
return term_to_nametuple(s, arity, out);
|
return term_to_nametuple(s, arity, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *compound_to_pyeval(term_t t, PyObject *context) {
|
PyObject *compound_to_pyeval(term_t t, PyObject *context) {
|
||||||
PyObject *o = NULL, *no;
|
PyObject *o = NULL, *no;
|
||||||
atom_t name;
|
atom_t name;
|
||||||
int arity;
|
int arity;
|
||||||
@ -1236,4 +1238,4 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context) {
|
|||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,7 @@ static void add_modules(void) {
|
|||||||
py_Yapex = PyImport_AddModule("yap4py.yapi");
|
py_Yapex = PyImport_AddModule("yap4py.yapi");
|
||||||
if (py_Yapex)
|
if (py_Yapex)
|
||||||
Py_INCREF(py_Yapex);
|
Py_INCREF(py_Yapex);
|
||||||
//py_F2P = PyObject_GetAttrString(py_Yap, "globals");
|
py_F2P = PyDict_New();
|
||||||
py_F2P = NULL;
|
|
||||||
init_python_stream();
|
init_python_stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,8 @@ add_custom_target( YAP4PY_SETUP_DIRS
|
|||||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os
|
||||||
)
|
)
|
||||||
|
|
||||||
add_custom_target( YAP4PY ALL
|
add_custom_target( YAP4PY ALL
|
||||||
|
COMMAND ${PYTHON_EXECUTABLE} -m pip uninstall -y YAP4PY
|
||||||
COMMAND ${SWIG_EXECUTABLE} -python -modern -c++ -py3 -DX_API -I${CMAKE_SOURCE_DIR}/CXX -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/H -I${CMAKE_SOURCE_DIR}/H/generated -I${CMAKE_SOURCE_DIR}/OPTYap -I../../.. -o yap_wrap.cpp yap.i
|
COMMAND ${SWIG_EXECUTABLE} -python -modern -c++ -py3 -DX_API -I${CMAKE_SOURCE_DIR}/CXX -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/H -I${CMAKE_SOURCE_DIR}/H/generated -I${CMAKE_SOURCE_DIR}/OPTYap -I../../.. -o yap_wrap.cpp yap.i
|
||||||
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel
|
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
@ -83,8 +84,9 @@ DEPENDS YAP4PY_SETUP)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install --force --no-index -f dist yap4py
|
install(CODE "execute_process(
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})"
|
COMMAND ${PYTHON_EXECUTABLE} -m pip install --force --no-index -f packages/python/swig/dist YAP4PY
|
||||||
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})"
|
||||||
DEPENDS Py4YAP ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${dlls} )
|
DEPENDS Py4YAP ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${dlls} )
|
||||||
|
|
||||||
install(FILES ${PROLOG_SOURCES} DESTINATION ${libpl})
|
install(FILES ${PROLOG_SOURCES} DESTINATION ${libpl})
|
||||||
|
@ -7,11 +7,12 @@ from collections import namedtuple
|
|||||||
|
|
||||||
from yap import *
|
from yap import *
|
||||||
|
|
||||||
|
|
||||||
class Engine( YAPEngine ):
|
class Engine( YAPEngine ):
|
||||||
def __init__(self, args=None):
|
def __init__(self, args=None,**kwargs):
|
||||||
# type: (object) -> object
|
# type: (object) -> object
|
||||||
if not args:
|
if not args:
|
||||||
args = YAPEngineArgs()
|
args = EngineArgs(**kwargs)
|
||||||
yap_lib_path = os.path.dirname(__file__)
|
yap_lib_path = os.path.dirname(__file__)
|
||||||
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
||||||
args.setYapLibDir(yap_lib_path)
|
args.setYapLibDir(yap_lib_path)
|
||||||
@ -32,11 +33,55 @@ class Engine( YAPEngine ):
|
|||||||
|
|
||||||
class EngineArgs( YAPEngineArgs ):
|
class EngineArgs( YAPEngineArgs ):
|
||||||
""" Interface to Engine Options class"""
|
""" Interface to Engine Options class"""
|
||||||
|
def __init__(self, args=None,**kwargs):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
|
||||||
class Predicate( YAPPredicate ):
|
class Predicate( YAPPredicate ):
|
||||||
""" Interface to Generic Predicate"""
|
""" Interface to Generic Predicate"""
|
||||||
|
|
||||||
|
class Predicate:
|
||||||
|
"""Goal is a predicate instantiated under a specific environment """
|
||||||
|
def __init__( self, name, args, module=None, engine = None):
|
||||||
|
self = namedtuple( name, args )
|
||||||
|
if module:
|
||||||
|
self.p = YAPPredicate( name, len(self), module )
|
||||||
|
else:
|
||||||
|
self.p = YAPPredicate( name, len(self) )
|
||||||
|
self.e = engine
|
||||||
|
|
||||||
|
def goals( self, engine):
|
||||||
|
self.e = engine
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return PrologTableIter(self.e, self.p)
|
||||||
|
|
||||||
|
def holds(self):
|
||||||
|
return self.e.run(self._make_())
|
||||||
|
|
||||||
|
class PrologTableIter:
|
||||||
|
|
||||||
|
def __init__(self, e, goal):
|
||||||
|
try:
|
||||||
|
self.e = e
|
||||||
|
self.q = e.YAPQuery(goal)
|
||||||
|
except:
|
||||||
|
print('Error')
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
# Iterators are iterables too.
|
||||||
|
# Adding this functions to make them so.
|
||||||
|
return self
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
if self.q.next():
|
||||||
|
return goal
|
||||||
|
else:
|
||||||
|
self.q.close()
|
||||||
|
self.q = None
|
||||||
|
raise StopIteration()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PrologPredicate( YAPPrologPredicate ):
|
class PrologPredicate( YAPPrologPredicate ):
|
||||||
""" Interface to Prolog Predicate"""
|
""" Interface to Prolog Predicate"""
|
||||||
@ -163,6 +208,17 @@ def live(**kwargs):
|
|||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
|
def boot_yap(**kwargs):
|
||||||
|
args = EngineArgs(**kwarg)
|
||||||
|
yap_lib_path = os.path.dirname(__file__)
|
||||||
|
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
||||||
|
args.setYapLibDir(yap_lib_path)
|
||||||
|
args.setSavedState(os.path.join(yap_lib_path,"startup.yss"))
|
||||||
|
engine = YAPEngine(args)
|
||||||
|
engine.goal( set_prolog_flag('verbose', 'silent' ) )
|
||||||
|
engine.goal( use_module(library('yapi') ) )
|
||||||
|
return engine
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
engine = boot_yap()
|
engine = boot_yap()
|
||||||
handler = numbervars
|
handler = numbervars
|
||||||
|
Reference in New Issue
Block a user