python interface as a separate package
This commit is contained in:
parent
37e4d242ad
commit
bf956ca639
10
Makefile.in
10
Makefile.in
@ -727,7 +727,7 @@ all: startup.yss
|
||||
@ENABLE_CUDD@ (cd packages/ProbLog/simplecudd; $(MAKE))
|
||||
@ENABLE_CUDD@ (cd packages/ProbLog/simplecudd_lfi; $(MAKE))
|
||||
@ENABLE_JPL@ @INSTALL_DLLS@ (cd packages/jpl; $(MAKE))
|
||||
@ENABLE_PYTHON@ @INSTALL_DLLS@ (cd packages/pyswip; $(MAKE))
|
||||
@ENABLE_PYTHON@ @INSTALL_DLLS@ (cd packages/python; $(MAKE))
|
||||
|
||||
startup.yss: yap@EXEC_SUFFIX@ $(PL_SOURCES)
|
||||
-rm -f startup.yss
|
||||
@ -797,8 +797,8 @@ install_unix: startup.yss libYap.a
|
||||
@INSTALL_MATLAB@ (cd library/matlab; $(MAKE) install)
|
||||
@ENABLE_REAL@ (cd packages/real; $(MAKE) install)
|
||||
@ENABLE_JPL@ @INSTALL_DLLS@ (cd packages/jpl; $(MAKE) install)
|
||||
@ENABLE_PYTHON@ @INSTALL_DLLS@ (cd packages/pyswip; $(MAKE) install)
|
||||
#@ENABLE_JPL@ @INSTALL_DLLS@ (cd packages/pyswip; $(MAKE) install)
|
||||
@ENABLE_PYTHON@ @INSTALL_DLLS@ (cd packages/python; $(MAKE) install)
|
||||
#@ENABLE_JPL@ @INSTALL_DLLS@ (cd packages/python; $(MAKE) install)
|
||||
mkdir -p $(DESTDIR)$(INCLUDEDIR)
|
||||
mkdir -p $(DESTDIR)$(INCLUDEDIR)/src
|
||||
$(INSTALL) $(HEADERS) $(DESTDIR)$(INCLUDEDIR)/src
|
||||
@ -863,7 +863,7 @@ install_win32: startup.yss @ENABLE_WINCONSOLE@ yap-win@EXEC_SUFFIX@
|
||||
(cd packages/CLPBN ; $(MAKE) install)
|
||||
@ENABLE_CLPBN_BP@ (cd packages/CLPBN/horus; $(MAKE) install)
|
||||
@ENABLE_JPL@ (cd packages/jpl ; $(MAKE) install)
|
||||
@ENABLE_PYTHON@ (cd packages/pyswip ; $(MAKE) install)
|
||||
@ENABLE_PYTHON@ (cd packages/python ; $(MAKE) install)
|
||||
@ENABLE_MINISAT@ (cd packages/swi-minisat2/C; $(MAKE) install)
|
||||
@ENABLE_CPLINT@ (cd packages/cplint; $(MAKE) install)
|
||||
@ENABLE_PRISM@ (cd packages/prism/src/c; $(MAKE) install)
|
||||
@ -940,7 +940,7 @@ clean: clean_docs
|
||||
@ENABLE_CUDD@ (cd packages/ProbLog/simplecudd; $(MAKE) clean)
|
||||
@ENABLE_CUDD@ (cd packages/ProbLog/simplecudd_lfi; $(MAKE) clean)
|
||||
@ENABLE_JPL@ @INSTALL_DLLS@ (cd packages/jpl; $(MAKE) clean)
|
||||
@ENABLE_PYTHON@ @INSTALL_DLLS@ (cd packages/pyswip; $(MAKE) clean)
|
||||
@ENABLE_PYTHON@ @INSTALL_DLLS@ (cd packages/python; $(MAKE) clean)
|
||||
|
||||
|
||||
|
||||
|
9
configure
vendored
9
configure
vendored
@ -6848,8 +6848,8 @@ fi
|
||||
if test "$yap_cv_python" = "no"; then
|
||||
PYTHON_TARGET="dummy"
|
||||
ENABLE_PYTHON="@# "
|
||||
elif test -e "$srcdir"/packages/pyswip/Makefile.in ; then
|
||||
PYTHON_TARGET="pyswip"
|
||||
elif test -e "$srcdir"/packages/python/Makefile.in ; then
|
||||
PYTHON_TARGET="pitf"
|
||||
ENABLE_PYTHON=""
|
||||
if test "$yap_cv_python" = "yes"
|
||||
then
|
||||
@ -6902,6 +6902,7 @@ test -n "$PYTHON" || PYTHON=""none""
|
||||
PYTHONHOME=`$PYTHON -c'import sys; print sys.prefix'`
|
||||
PYTHONVERSION=`"$PYTHON" -c "import sys; print sys.version[:3]"`
|
||||
PYTHON_LIBS="-L $PYTHONHOME/lib -lpython$PYTHONVERSION"
|
||||
LIBS="$LIBS $PYTHON_LIBS"
|
||||
PYTHON_INCLUDES="-I $PYTHONHOME/include/python$PYTHONVERSION"
|
||||
else
|
||||
PYTHON_TARGET="dummy"
|
||||
@ -11354,7 +11355,7 @@ ac_config_files="$ac_config_files packages/jpl/Makefile packages/jpl/jpl_paths.y
|
||||
fi
|
||||
|
||||
if test "$ENABLE_PYTHON" = ""; then
|
||||
ac_config_files="$ac_config_files packages/pyswip/Makefile"
|
||||
ac_config_files="$ac_config_files packages/python/Makefile"
|
||||
|
||||
fi
|
||||
|
||||
@ -12171,7 +12172,7 @@ do
|
||||
"packages/jpl/Makefile") CONFIG_FILES="$CONFIG_FILES packages/jpl/Makefile" ;;
|
||||
"packages/jpl/jpl_paths.yap") CONFIG_FILES="$CONFIG_FILES packages/jpl/jpl_paths.yap" ;;
|
||||
"packages/jpl/src/java/Makefile") CONFIG_FILES="$CONFIG_FILES packages/jpl/src/java/Makefile" ;;
|
||||
"packages/pyswip/Makefile") CONFIG_FILES="$CONFIG_FILES packages/pyswip/Makefile" ;;
|
||||
"packages/python/Makefile") CONFIG_FILES="$CONFIG_FILES packages/python/Makefile" ;;
|
||||
"packages/pldoc/Makefile") CONFIG_FILES="$CONFIG_FILES packages/pldoc/Makefile" ;;
|
||||
"packages/pldoc/server/man_server.pl") CONFIG_FILES="$CONFIG_FILES packages/pldoc/server/man_server.pl" ;;
|
||||
"packages/plunit/Makefile") CONFIG_FILES="$CONFIG_FILES packages/plunit/Makefile" ;;
|
||||
|
@ -849,8 +849,8 @@ fi
|
||||
if test "$yap_cv_python" = "no"; then
|
||||
PYTHON_TARGET="dummy"
|
||||
ENABLE_PYTHON="@# "
|
||||
elif test -e "$srcdir"/packages/pyswip/Makefile.in ; then
|
||||
PYTHON_TARGET="pyswip"
|
||||
elif test -e "$srcdir"/packages/python/Makefile.in ; then
|
||||
PYTHON_TARGET="pitf"
|
||||
ENABLE_PYTHON=""
|
||||
if test "$yap_cv_python" = "yes"
|
||||
then
|
||||
@ -861,6 +861,7 @@ elif test -e "$srcdir"/packages/pyswip/Makefile.in ; then
|
||||
PYTHONHOME=`$PYTHON -c'import sys; print sys.prefix'`
|
||||
PYTHONVERSION=`"$PYTHON" -c "import sys; print sys.version[[:3]]"`
|
||||
PYTHON_LIBS="-L $PYTHONHOME/lib -lpython$PYTHONVERSION"
|
||||
LIBS="$LIBS $PYTHON_LIBS"
|
||||
PYTHON_INCLUDES="-I $PYTHONHOME/include/python$PYTHONVERSION"
|
||||
else
|
||||
PYTHON_TARGET="dummy"
|
||||
@ -2565,7 +2566,7 @@ AC_CONFIG_FILES([packages/jpl/Makefile packages/jpl/jpl_paths.yap packages/jpl/s
|
||||
fi
|
||||
|
||||
if test "$ENABLE_PYTHON" = ""; then
|
||||
AC_CONFIG_FILES([ packages/pyswip/Makefile ])
|
||||
AC_CONFIG_FILES([ packages/python/Makefile ])
|
||||
fi
|
||||
|
||||
if test "$ENABLE_PLDOC" = ""; then
|
||||
|
@ -4,8 +4,8 @@
|
||||
# Copyright: Perl License
|
||||
################################################################
|
||||
|
||||
PACKAGE=pyswip
|
||||
DOC=pyswip
|
||||
PACKAGE=python
|
||||
DOC=python
|
||||
PKGCFLAGS=@PYTHON_INCLUDES@
|
||||
include ../Makefile.defs
|
||||
|
||||
@ -16,7 +16,7 @@ LIBPL= python.pl
|
||||
|
||||
all: @PYTHON_TARGET@
|
||||
|
||||
pyswip: $(SOLIBS)
|
||||
pitf: $(SOLIBS)
|
||||
dummy::
|
||||
|
||||
python.@SO@: $(OBJS)
|
||||
@ -24,7 +24,7 @@ python.@SO@: $(OBJS)
|
||||
|
||||
install: install-@PYTHON_TARGET@
|
||||
install-dummy::
|
||||
install-pyswip: $(SOLIBS) $(addprefix $(srcdir)/, $(LIBPL))
|
||||
install-pitf: $(SOLIBS) $(addprefix $(srcdir)/, $(LIBPL))
|
||||
mkdir -p $(DESTDIR)$(SOLIBDIR)
|
||||
rm -f $(DESTDIR)$(SOLIBDIR)/python.@SO@
|
||||
$(INSTALL_PROGRAM) $(SOLIBS) $(SOLIBDIR)
|
||||
@ -35,9 +35,6 @@ install-pyswip: $(SOLIBS) $(addprefix $(srcdir)/, $(LIBPL))
|
||||
$(MKINDEX)
|
||||
|
||||
|
||||
install-python:
|
||||
(cd $(srcdir); python setup.py install)
|
||||
|
||||
ln-install::
|
||||
$(MAKE) INSTALL_DATA="../ln-install" INSTALL_PROGRAM="../ln-install" install
|
||||
|
@ -189,16 +189,40 @@ term_to_python(term_t t)
|
||||
return NULL;
|
||||
return PyNumber_Divide(lhs, rhs);
|
||||
} else if (fun == FUNCTOR_hat2) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
|
||||
PyObject *lhs, *rhs;
|
||||
|
||||
if (! PL_get_arg(1, t, targ) )
|
||||
if (! PL_get_arg(1, t, targ))
|
||||
return NULL;
|
||||
lhs = term_to_python(targ);
|
||||
if (! PL_get_arg(2, t, targ) )
|
||||
if (! PL_get_arg(2, t, targ) || !PL_is_list(targ) || !PL_get_list(targ, trhs, targ) )
|
||||
return NULL;
|
||||
rhs = term_to_python(targ);
|
||||
rhs = term_to_python(trhs);
|
||||
return PyObject_GetItem(lhs, rhs);
|
||||
} else {
|
||||
atom_t name;
|
||||
int len;
|
||||
|
||||
if (! PL_get_name_arity( t, &name, &len) ) {
|
||||
return NULL;
|
||||
}
|
||||
if (name == ATOM_t) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
PyObject *out;
|
||||
int i;
|
||||
|
||||
out = PyTuple_New(len);
|
||||
if (!out)
|
||||
return NULL;
|
||||
for (i=0; i< len; i++) {
|
||||
if (!PL_get_arg(i+1, t, targ)) {
|
||||
return NULL;
|
||||
}
|
||||
if (PyTuple_SetItem(out, i, term_to_python(targ)) < 0)
|
||||
return NULL;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
@ -206,6 +230,99 @@ term_to_python(term_t t)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
assign_python(PyObject *root, term_t t, PyObject *e)
|
||||
{
|
||||
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I am\n");
|
||||
switch (PL_term_type(t)) {
|
||||
case PL_VARIABLE:
|
||||
return -1;
|
||||
case PL_ATOM:
|
||||
{
|
||||
char *s;
|
||||
|
||||
if (!PL_get_atom_chars(t, &s)) {
|
||||
wchar_t *w;
|
||||
atom_t at;
|
||||
size_t len;
|
||||
PyObject *wo;
|
||||
|
||||
if (!PL_get_atom(t, &at))
|
||||
return -1;
|
||||
if (!(w = PL_atom_wchars(at, &len)))
|
||||
return -1;
|
||||
wo = PyUnicode_FromWideChar(w, wcslen(w) );
|
||||
return PyObject_SetAttr(root, wo, e);
|
||||
}
|
||||
if (proper_ascii_string(s)) {
|
||||
return PyObject_SetAttrString(root, s, e);
|
||||
} else {
|
||||
PyObject *wo= PyUnicode_DecodeLatin1(s, strlen(s), NULL);
|
||||
return PyObject_SetAttr(root, wo, e);
|
||||
}
|
||||
}
|
||||
case PL_INTEGER:
|
||||
case PL_STRING:
|
||||
case PL_FLOAT:
|
||||
return -1;
|
||||
case PL_TERM:
|
||||
if (PL_is_list(t)) {
|
||||
return -1;
|
||||
} else {
|
||||
functor_t fun;
|
||||
|
||||
if (!PL_get_functor(t, &fun))
|
||||
return -1;
|
||||
if (fun == FUNCTOR_dollar1) {
|
||||
char *s;
|
||||
|
||||
|
||||
if (! PL_get_arg(1, t, t) )
|
||||
return -1;
|
||||
if (!PL_get_atom_chars(t, &s)) {
|
||||
wchar_t *w;
|
||||
atom_t at;
|
||||
size_t len;
|
||||
PyObject *attr;
|
||||
|
||||
if (!PL_get_atom(t, &at)) {
|
||||
return -1;
|
||||
}
|
||||
if (!(w = PL_atom_wchars(at, &len)))
|
||||
return -1;
|
||||
attr = PyUnicode_FromWideChar(w, wcslen(w) );
|
||||
if (!attr)
|
||||
return -1;
|
||||
return PyObject_SetAttr(py_Main, attr, e);
|
||||
}
|
||||
if (proper_ascii_string(s)) {
|
||||
return PyObject_SetAttrString(py_Main, s, e);
|
||||
} else {
|
||||
PyObject *attr= PyUnicode_DecodeLatin1(s, strlen(s), NULL);
|
||||
if (!attr)
|
||||
return -1;
|
||||
return PyObject_SetAttr(py_Main, attr, e);
|
||||
}
|
||||
} else if (fun == FUNCTOR_pointer1) {
|
||||
return -1;
|
||||
} else if (fun == FUNCTOR_hat2) {
|
||||
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
|
||||
PyObject *lhs, *rhs;
|
||||
|
||||
if (! PL_get_arg(1, t, targ) )
|
||||
return -1;
|
||||
lhs = term_to_python(targ);
|
||||
if (! PL_get_arg(2, t, targ) || !PL_is_list(targ) || !PL_get_list(targ, trhs, targ ) )
|
||||
return -1;
|
||||
rhs = term_to_python(trhs);
|
||||
return PyObject_SetItem(lhs, rhs, e);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static foreign_t
|
||||
python_to_term(PyObject *pVal, term_t t)
|
||||
{
|
||||
@ -339,25 +456,6 @@ python_o(term_t tmod, term_t fname, term_t tf)
|
||||
return python_to_term(pO, tf);
|
||||
}
|
||||
|
||||
static foreign_t
|
||||
python_set_item(term_t tobj, term_t tpos, term_t titem)
|
||||
{
|
||||
PyObject *obj, *item, *pos;
|
||||
|
||||
obj = term_to_python(tobj);
|
||||
if (obj == NULL)
|
||||
return FALSE;
|
||||
item = term_to_python(titem);
|
||||
if (item == NULL)
|
||||
return FALSE;
|
||||
pos = term_to_python(tpos);
|
||||
if (pos == NULL)
|
||||
return FALSE;
|
||||
if (PyObject_SetItem(obj, pos, item) < 0)
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static foreign_t
|
||||
python_len(term_t tobj, term_t tf)
|
||||
{
|
||||
@ -441,13 +539,10 @@ static foreign_t
|
||||
python_assign(term_t name, term_t exp)
|
||||
{
|
||||
PyObject *e = term_to_python(exp);
|
||||
char *s;
|
||||
|
||||
if (e == NULL)
|
||||
return FALSE;
|
||||
if (!PL_get_atom_chars(name, &s))
|
||||
return FALSE;
|
||||
return PyObject_SetAttrString(py_Main, s, e) >= 0;
|
||||
return assign_python(py_Main, name, e) >= 0;
|
||||
}
|
||||
|
||||
static foreign_t
|
||||
@ -561,7 +656,6 @@ install_python(void)
|
||||
PL_register_foreign("python_apply", 3, python_apply, 0);
|
||||
PL_register_foreign("python_access", 3, python_access, 0);
|
||||
PL_register_foreign("python_assign", 2, python_assign, 0);
|
||||
PL_register_foreign("python_set_item", 3, python_set_item, 0);
|
||||
PL_register_foreign("python_run_command", 1, python_run_command, 0);
|
||||
}
|
||||
|
@ -65,8 +65,7 @@ Data types are
|
||||
:= F :- python(F,_).
|
||||
|
||||
V := F :- var(V), !, python(F,V).
|
||||
'$'(V) := F :- atom(V), !, python(F,F1), python_assign(V, F1).
|
||||
A^Key := F :- python(F,F1), python_set_item(A, Key, F1).
|
||||
A := F :- python(F, F1), python_assign(A, F1).
|
||||
|
||||
python_import(Module) :-
|
||||
python_do_import(Module, _).
|
||||
@ -140,7 +139,8 @@ start_python :-
|
||||
add_cwd_to_python :-
|
||||
unix(getcwd(Dir)),
|
||||
atom_concat(['sys.path.append(\"',Dir,'\")'], Command),
|
||||
python_command(Command).
|
||||
python_command(Command),
|
||||
python_command("sys.argv = [\"yap\"]").
|
||||
% done
|
||||
|
||||
python_assign(Name, Exp, '$'(Name)) :-
|
Reference in New Issue
Block a user