This commit is contained in:
Vitor Santos Costa 2018-07-31 15:18:56 +01:00
parent bf712034a9
commit 828a5ec1e9
8 changed files with 145 additions and 29 deletions

View File

@ -14,6 +14,8 @@ PyObject *YE(term_t t, int line, const char *file, const char *code) {
return NULL; return NULL;
} }
PyObject *YEC(PyObject *f, PyObject *a, PyObject *d, int line, const char *file, const char *code) { PyObject *YEC(PyObject *f, PyObject *a, PyObject *d, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code, fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code,
@ -24,12 +26,48 @@ PyObject *YEC(PyObject *f, PyObject *a, PyObject *d, int line, const char *file,
fprintf(stderr,"<null>"); fprintf(stderr,"<null>");
if (a) if (a)
PyObject_Print(a, stderr, 0); PyObject_Print(a, stderr, 0);
if (a) if (d)
PyObject_Print(a, stderr, 0); PyObject_Print(d, stderr, 0);
fprintf(stderr,"\n"); fprintf(stderr,"\n");
return NULL; return NULL;
} }
void YEM(const char *exp, int line, const char *file, const char *code) {
PyObject *YED2(PyObject *f, PyObject *a, PyObject *d, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code,
file, line);
if (f)
PyObject_Print(f, stderr, 0);
else
fprintf(stderr,"<null>");
fprintf(stderr,"(");
if (a)
PyObject_Print(a, stderr, 0);
fprintf(stderr,",");
if (d)
PyObject_Print(d, stderr, 0);
fprintf(stderr,")\n");
return NULL;
}
PyObject *YED1(PyObject *f, PyObject *a, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code,
file, line);
if (f)
PyObject_Print(f, stderr, 0);
else
fprintf(stderr,"<null>");
fprintf(stderr,"(");
if (a)
PyObject_Print(a, stderr, 0);
fprintf(stderr,")\n");
return NULL;
}
void YEM(const char *exp, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed while executing %s\n", code, fprintf(stderr, "**** Warning,%s@%s:%d: failed while executing %s\n", code,
file, line, exp); file, line, exp);
} }
@ -305,7 +343,10 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
} }
if (fun == FUNCTOR_brackets1) { if (fun == FUNCTOR_brackets1) {
AOK(PL_get_arg(1, t, t), NULL); AOK(PL_get_arg(1, t, t), NULL);
return term_to_python(t, true, NULL, true); PyObject *ys = term_to_python(t, true, o, true), *rc;
PyObject_Print(ys,stderr,0);fprintf(stderr, "--- \n");
CHECK_CALL(ys, PyTuple_New(0), NULL);
return rc;
} }
if (fun == FUNCTOR_complex2) { if (fun == FUNCTOR_complex2) {
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();

View File

@ -131,7 +131,7 @@ else {
else if (PyList_Check(pVal)) { else if (PyList_Check(pVal)) {
Py_ssize_t i, sz = PyList_GET_SIZE(pVal); Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
if (sz == 0) if (sz == 0)
return TermNil; return repr_term(pVal);
Term t = TermNil; Term t = TermNil;
for (i = sz; i > 0; --i) { for (i = sz; i > 0; --i) {
PyObject *p = PyTuple_GetItem(pVal, i); PyObject *p = PyTuple_GetItem(pVal, i);
@ -152,7 +152,7 @@ else if (PyDict_Check(pVal)) {
PyObject *key, *value; PyObject *key, *value;
Term f, *opt = &f, t; Term f, *opt = &f, t;
if (left == 0) { if (left == 0) {
return ATOM_curly_brackets; return repr_term(pVal);
} else { } else {
while (PyDict_Next(pVal, &pos, &key, &value)) { while (PyDict_Next(pVal, &pos, &key, &value)) {
Term t0[2], to; Term t0[2], to;
@ -177,10 +177,8 @@ else if (PyDict_Check(pVal)) {
} }
foreign_t python_to_term(PyObject *pVal, term_t t) { foreign_t python_to_term(PyObject *pVal, term_t t) {
term_t t0 = PL_new_term_ref(); Term o = python_to_term__(pVal);
bool rc = python_to_term__(pVal); return YAP_Unify(o,YAP_GetFromSlot(t));
PL_reset_term_refs(t0);
return rc;
} }
// extern bool Yap_do_low_level_trace; // extern bool Yap_do_low_level_trace;
@ -228,6 +226,17 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
return python_to_term(exp, t); return python_to_term(exp, t);
} }
case PL_STRING: {
char *s = NULL;
size_t l;
PL_get_string_chars(t, &s,&l);
if (!context)
context = py_Main;
if (PyObject_SetAttrString(context, s, exp) == 0)
return true;
PyErr_Print();
return false;
}
case PL_ATOM: { case PL_ATOM: {
char *s = NULL; char *s = NULL;
PL_get_atom_chars(t, &s); PL_get_atom_chars(t, &s);
@ -238,7 +247,6 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
PyErr_Print(); PyErr_Print();
return false; return false;
} }
case PL_STRING:
case PL_INTEGER: case PL_INTEGER:
case PL_FLOAT: case PL_FLOAT:
// domain or type erro? // domain or type erro?

View File

@ -6,7 +6,7 @@
#define PY4YAP_H 1 #define PY4YAP_H 1
#define PASS_REGS #define PASS_REGS
#define USES_REGSg #define USES_REGS
//@{ //@{
@ -23,6 +23,8 @@
#include <Python.h> #include <Python.h>
#include <Yap.h>
#include <SWI-Prolog.h> #include <SWI-Prolog.h>
#ifdef HAVE_STAT #ifdef HAVE_STAT
#undef HAVE_STATa #undef HAVE_STATa
@ -154,6 +156,28 @@ static inline PyObject *atom_to_python_string(term_t t) {
PyErr_Clear(); \ PyErr_Clear(); \
} }
extern PyObject *YED2(PyObject *f, PyObject *a, PyObject *d, int line, const char *file, const char *code);
static inline PyObject *CALL_BIP2(PyObject *ys,PyObject * pArg1,PyObject * pArg2)
{ PyErr_Clear(); \
PyObject *rc = PyObject_CallFunctionObjArgs(ys, pArg1, pArg2, NULL); \
if (rc == NULL || PyErr_Occurred()) { \
YED2(ys, pArg1, pArg2, __LINE__, __FILE__, __FUNCTION__); \
PyErr_Print(); \
PyErr_Clear(); \
}
return rc;
}
#define CALL_BIP1(ys, pArg1) \
PyErr_Clear(); \
rc = PyObject_CallFunctionObjArgs(ys, pArg1, NULL); \
if (rc == NULL || PyErr_Occurred()) { \
YED1(ys, pArg1, __LINE__, __FILE__, __FUNCTION__); \
PyErr_Print(); \
PyErr_Clear(); \
}
#define CHECKNULL(t, rc) \ #define CHECKNULL(t, rc) \
(rc != NULL ? rc : YE(t, __LINE__, __FILE__, __FUNCTION__)) (rc != NULL ? rc : YE(t, __LINE__, __FILE__, __FUNCTION__))
#define AOK(rc, err) \ #define AOK(rc, err) \
@ -162,6 +186,8 @@ static inline PyObject *atom_to_python_string(term_t t) {
YEM(#rc, __LINE__, __FILE__, __FUNCTION__); \ YEM(#rc, __LINE__, __FILE__, __FUNCTION__); \
} }
extern PyObject *YED1(PyObject *f, PyObject *a, int line, const char *file, const char *code);
extern PyObject *YE(term_t , int line, const char *file, const char *code); extern PyObject *YE(term_t , int line, const char *file, const char *code);
extern PyObject *YEC(PyObject *c,PyObject *a ,PyObject *d , int line, const char *file, const char *code); extern PyObject *YEC(PyObject *c,PyObject *a ,PyObject *d , int line, const char *file, const char *code);
extern void YEM(const char *ex, int line, const char *file, const char *code); extern void YEM(const char *ex, int line, const char *file, const char *code);

View File

@ -987,8 +987,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
} }
if (PyNumber_Check(lhs) && PyNumber_Check(rhs)) if (PyNumber_Check(lhs) && PyNumber_Check(rhs))
return PyNumber_Add(lhs, rhs); return PyNumber_Add(lhs, rhs);
PyObject_Print(builtin("+"), stderr, 0); return CALL_BIP2(builtin("+"), lhs, rhs);
return PyObject_CallFunctionObjArgs(builtin("+"), lhs, rhs, NULL);
} else if (fun == FUNCTOR_sub2) { } else if (fun == FUNCTOR_sub2) {
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs; PyObject *lhs, *rhs;
@ -1001,7 +1000,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
rhs = term_to_python(targ, true, NULL, true); rhs = term_to_python(targ, true, NULL, true);
if (PyNumber_Check(rhs) && PyNumber_Check(lhs)) if (PyNumber_Check(rhs) && PyNumber_Check(lhs))
return PyNumber_Subtract(lhs, rhs); return PyNumber_Subtract(lhs, rhs);
return PyObject_CallFunctionObjArgs(builtin("-"), lhs, rhs, NULL); return CALL_BIP2(builtin("-"), lhs, rhs);
} else if (fun == FUNCTOR_mul2) { } else if (fun == FUNCTOR_mul2) {
term_t targ = PL_new_term_ref(); term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs; PyObject *lhs, *rhs;
@ -1022,7 +1021,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
} }
if (PyNumber_Check(lhs) && PyNumber_Check(rhs)) if (PyNumber_Check(lhs) && PyNumber_Check(rhs))
return PyNumber_Multiply(lhs, rhs); return PyNumber_Multiply(lhs, rhs);
return PyObject_CallFunctionObjArgs(builtin("*"), lhs, rhs, NULL); return PyObject_CallFunctionObjArgs(builtin("*"), lhs, rhs);
} }
if (!arity) { if (!arity) {
char *s = NULL; char *s = NULL;
@ -1037,7 +1036,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
return pValue; return pValue;
} else { } else {
char *s = PL_atom_chars(name); char *s = PL_atom_chars(name);
if (!strcmp(s,"t")) { if (!strcmp(s,"t") || !strcmp(s,"tuple")) {
YAP_Term tt = YAP_GetFromSlot(t), tleft; YAP_Term tt = YAP_GetFromSlot(t), tleft;
int i; int i;
PyObject *rc = PyTuple_New(arity); PyObject *rc = PyTuple_New(arity);
@ -1069,11 +1068,17 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
/* ignore (_) */ /* ignore (_) */
if (indict) { if (indict) {
if (PL_get_functor(tleft, &fun) && fun == FUNCTOR_equal2) { if (PL_get_functor(tleft, &fun) && fun == FUNCTOR_equal2) {
term_t tatt = PL_new_term_ref(); Term tatt = ArgOfTerm(1,Yap_GetFromSlot(tleft));
AOK(PL_get_arg(1, tleft, tatt), NULL); const char *sk;
PyObject *key = term_to_python(tatt, true, NULL, true); if (IsAtomTerm(tatt))
AOK(PL_get_arg(2, tleft, tatt), NULL); sk = RepAtom(AtomOfTerm(tatt))->StrOfAE;
PyObject *val = term_to_python(tatt, true, NULL, true); else if (IsStringTerm(tatt))
sk = StringOfTerm(tatt);
else
return NULL;
PyObject *key = PyUnicode_FromString(sk);
AOK(PL_get_arg(2, tleft, tleft), NULL);
PyObject *val = term_to_python(tleft, true, o, cvt);
PyDict_SetItem(pyDict, key, val); PyDict_SetItem(pyDict, key, val);
} else { } else {
indict = false; indict = false;
@ -1086,7 +1091,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
if (PL_is_variable(tleft)) { if (PL_is_variable(tleft)) {
pArg = Py_None; pArg = Py_None;
} else { } else {
pArg = term_to_python(tleft, true, NULL, true); pArg = term_to_python(tleft, true, o, cvt);
// PyObject_Print(pArg,fdopen(2,"w"),0); // PyObject_Print(pArg,fdopen(2,"w"),0);
if (pArg == NULL) { if (pArg == NULL) {
pArg = Py_None; pArg = Py_None;
@ -1105,9 +1110,9 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
PyObject *rc; PyObject *rc;
if (ys && PyCallable_Check(ys)) { if (ys && PyCallable_Check(ys)) {
// PyObject_Print(ys, stderr, 0); PyObject_Print(ys, stderr, 0);
// PyObject_Print(pArgs, stderr, 0); PyObject_Print(pArgs, stderr, 0);
// PyObject_Print(pyDict, stderr, 0); PyObject_Print(pyDict, stderr, 0);
// PyObject_Print(pArgs, stderr, 0); // PyObject_Print(pArgs, stderr, 0);
// PyObject_Print(o, stderr, 0); // PyObject_Print(o, stderr, 0);

View File

@ -131,11 +131,24 @@ static bool py_close(int sno) {
static bool pygetLine(StreamDesc *rl_iostream, int sno) { static bool pygetLine(StreamDesc *rl_iostream, int sno) {
// term_t ctk = python_acquire_GIL(); // term_t ctk = python_acquire_GIL();
const char *myrl_line; const char *myrl_line;
PyObject *user_line;
StreamDesc *s = YAP_GetStreamFromId(sno); StreamDesc *s = YAP_GetStreamFromId(sno);
//term_t tg = python_acquire_GIL(); //term_t tg = python_acquire_GIL();
if (1) { //!strcmp(RepAtom(s->name)->StrOfAE,"input")) {
// note that input may change
PyObject *pystream = PyDict_GetItemString( Py_B``, "input");
if (pystream == NULL) {
if ((err = PyErr_Occurred())) {
PyErr_Print();
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
}
}
user_line = PyObject_CallFunctionObjArgs(pystream, NULL);
} else {
PyObject *readl = PyObject_GetAttrString(s->u.private_data, "readline"); PyObject *readl = PyObject_GetAttrString(s->u.private_data, "readline");
PyObject *user_inp = PyObject_CallFunction(readl, NULL); user_line = PyObject_CallFunction(readl, NULL);
myrl_line = PyUnicode_AsUTF8(user_inp); }
myrl_line = PyUnicode_AsUTF8(user_line);
if (myrl_line == NULL) if (myrl_line == NULL)
return NULL; return NULL;
PyObject *err; PyObject *err;

View File

@ -552,6 +552,27 @@ static foreign_t python_export(term_t t, term_t pl) {
pyErrorAndReturn(rc); pyErrorAndReturn(rc);
} }
static bool get_mod(const char *s0)
{
PyObject *pName;
term_t t0 = python_acquire_GIL();
#if PY_MAJOR_VERSION < 3
pName = PyString_FromString(s0);
#else
pName = PyUnicode_FromString(s0);
#endif
if (pName == NULL) {
python_release_GIL(t0);
}
PyObject *pModule = PyImport_Import(pName);
Py_XDECREF(pName);
python_release_GIL(t0);
return pModule;
}
/** /**
* @pred python_import(MName, Mod) * @pred python_import(MName, Mod)
* Import a python module to the YAP environment. * Import a python module to the YAP environment.
@ -593,6 +614,7 @@ static int python_import(term_t mname, term_t mod) {
else else
return false; return false;
strcat(s, sn); strcat(s, sn);
//get_mod(s);
strcat(s, "."); strcat(s, ".");
} }
sm = t; sm = t;

View File

@ -25,6 +25,7 @@ class Engine( YAPEngine ):
# type: (object) -> object # type: (object) -> object
if not args: if not args:
args = EngineArgs(**kwargs) args = EngineArgs(**kwargs)
args.setEmbedded(True)
if self_contained: if self_contained:
yap_lib_path = dirname(__file__) yap_lib_path = dirname(__file__)
args.setYapShareDir(join(yap_lib_path, "prolog")) args.setYapShareDir(join(yap_lib_path, "prolog"))

View File

@ -92,7 +92,7 @@ blank(Text) :-
close(user_output), close(user_output),
close(user_error). close(user_error).
streams(true) :- streams(true) :-
open('/python/sys.stdin', read, _Input, [alias(user_input),bom(false),script(false)]), open('/python/input', read, _Input, [alias(user_input),bom(false),script(false)]),
open('/python/sys.stdout', append, _Output, [alias(user_output)]), open('/python/sys.stdout', append, _Output, [alias(user_output)]),
open('/python/sys.stderr', append, _Error, [alias(user_error)]). open('/python/sys.stderr', append, _Error, [alias(user_error)]).