jupyter
This commit is contained in:
parent
bf712034a9
commit
828a5ec1e9
@ -14,6 +14,8 @@ PyObject *YE(term_t t, int line, const char *file, const char *code) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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,
|
||||
@ -24,12 +26,48 @@ PyObject *YEC(PyObject *f, PyObject *a, PyObject *d, int line, const char *file,
|
||||
fprintf(stderr,"<null>");
|
||||
if (a)
|
||||
PyObject_Print(a, stderr, 0);
|
||||
if (a)
|
||||
PyObject_Print(a, stderr, 0);
|
||||
if (d)
|
||||
PyObject_Print(d, stderr, 0);
|
||||
fprintf(stderr,"\n");
|
||||
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,
|
||||
file, line, exp);
|
||||
}
|
||||
@ -305,7 +343,10 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
}
|
||||
if (fun == FUNCTOR_brackets1) {
|
||||
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) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
@ -131,7 +131,7 @@ else {
|
||||
else if (PyList_Check(pVal)) {
|
||||
Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
|
||||
if (sz == 0)
|
||||
return TermNil;
|
||||
return repr_term(pVal);
|
||||
Term t = TermNil;
|
||||
for (i = sz; i > 0; --i) {
|
||||
PyObject *p = PyTuple_GetItem(pVal, i);
|
||||
@ -152,7 +152,7 @@ else if (PyDict_Check(pVal)) {
|
||||
PyObject *key, *value;
|
||||
Term f, *opt = &f, t;
|
||||
if (left == 0) {
|
||||
return ATOM_curly_brackets;
|
||||
return repr_term(pVal);
|
||||
} else {
|
||||
while (PyDict_Next(pVal, &pos, &key, &value)) {
|
||||
Term t0[2], to;
|
||||
@ -177,10 +177,8 @@ else if (PyDict_Check(pVal)) {
|
||||
}
|
||||
|
||||
foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
term_t t0 = PL_new_term_ref();
|
||||
bool rc = python_to_term__(pVal);
|
||||
PL_reset_term_refs(t0);
|
||||
return rc;
|
||||
Term o = python_to_term__(pVal);
|
||||
return YAP_Unify(o,YAP_GetFromSlot(t));
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
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: {
|
||||
char *s = NULL;
|
||||
PL_get_atom_chars(t, &s);
|
||||
@ -238,7 +247,6 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
case PL_STRING:
|
||||
case PL_INTEGER:
|
||||
case PL_FLOAT:
|
||||
// domain or type erro?
|
||||
|
@ -6,7 +6,7 @@
|
||||
#define PY4YAP_H 1
|
||||
|
||||
#define PASS_REGS
|
||||
#define USES_REGSg
|
||||
#define USES_REGS
|
||||
|
||||
//@{
|
||||
|
||||
@ -23,6 +23,8 @@
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <Yap.h>
|
||||
|
||||
#include <SWI-Prolog.h>
|
||||
#ifdef HAVE_STAT
|
||||
#undef HAVE_STATa
|
||||
@ -154,6 +156,28 @@ static inline PyObject *atom_to_python_string(term_t t) {
|
||||
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) \
|
||||
(rc != NULL ? rc : YE(t, __LINE__, __FILE__, __FUNCTION__))
|
||||
#define AOK(rc, err) \
|
||||
@ -162,6 +186,8 @@ static inline PyObject *atom_to_python_string(term_t t) {
|
||||
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 *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);
|
||||
|
@ -987,8 +987,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
}
|
||||
if (PyNumber_Check(lhs) && PyNumber_Check(rhs))
|
||||
return PyNumber_Add(lhs, rhs);
|
||||
PyObject_Print(builtin("+"), stderr, 0);
|
||||
return PyObject_CallFunctionObjArgs(builtin("+"), lhs, rhs, NULL);
|
||||
return CALL_BIP2(builtin("+"), lhs, rhs);
|
||||
} else if (fun == FUNCTOR_sub2) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
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);
|
||||
if (PyNumber_Check(rhs) && PyNumber_Check(lhs))
|
||||
return PyNumber_Subtract(lhs, rhs);
|
||||
return PyObject_CallFunctionObjArgs(builtin("-"), lhs, rhs, NULL);
|
||||
return CALL_BIP2(builtin("-"), lhs, rhs);
|
||||
} else if (fun == FUNCTOR_mul2) {
|
||||
term_t targ = PL_new_term_ref();
|
||||
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))
|
||||
return PyNumber_Multiply(lhs, rhs);
|
||||
return PyObject_CallFunctionObjArgs(builtin("*"), lhs, rhs, NULL);
|
||||
return PyObject_CallFunctionObjArgs(builtin("*"), lhs, rhs);
|
||||
}
|
||||
if (!arity) {
|
||||
char *s = NULL;
|
||||
@ -1037,7 +1036,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
return pValue;
|
||||
} else {
|
||||
char *s = PL_atom_chars(name);
|
||||
if (!strcmp(s,"t")) {
|
||||
if (!strcmp(s,"t") || !strcmp(s,"tuple")) {
|
||||
YAP_Term tt = YAP_GetFromSlot(t), tleft;
|
||||
int i;
|
||||
PyObject *rc = PyTuple_New(arity);
|
||||
@ -1069,11 +1068,17 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
/* ignore (_) */
|
||||
if (indict) {
|
||||
if (PL_get_functor(tleft, &fun) && fun == FUNCTOR_equal2) {
|
||||
term_t tatt = PL_new_term_ref();
|
||||
AOK(PL_get_arg(1, tleft, tatt), NULL);
|
||||
PyObject *key = term_to_python(tatt, true, NULL, true);
|
||||
AOK(PL_get_arg(2, tleft, tatt), NULL);
|
||||
PyObject *val = term_to_python(tatt, true, NULL, true);
|
||||
Term tatt = ArgOfTerm(1,Yap_GetFromSlot(tleft));
|
||||
const char *sk;
|
||||
if (IsAtomTerm(tatt))
|
||||
sk = RepAtom(AtomOfTerm(tatt))->StrOfAE;
|
||||
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);
|
||||
} else {
|
||||
indict = false;
|
||||
@ -1086,7 +1091,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
if (PL_is_variable(tleft)) {
|
||||
pArg = Py_None;
|
||||
} else {
|
||||
pArg = term_to_python(tleft, true, NULL, true);
|
||||
pArg = term_to_python(tleft, true, o, cvt);
|
||||
// PyObject_Print(pArg,fdopen(2,"w"),0);
|
||||
if (pArg == NULL) {
|
||||
pArg = Py_None;
|
||||
@ -1105,9 +1110,9 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
|
||||
PyObject *rc;
|
||||
if (ys && PyCallable_Check(ys)) {
|
||||
// PyObject_Print(ys, stderr, 0);
|
||||
// PyObject_Print(pArgs, stderr, 0);
|
||||
// PyObject_Print(pyDict, stderr, 0);
|
||||
PyObject_Print(ys, stderr, 0);
|
||||
PyObject_Print(pArgs, stderr, 0);
|
||||
PyObject_Print(pyDict, stderr, 0);
|
||||
|
||||
// PyObject_Print(pArgs, stderr, 0);
|
||||
// PyObject_Print(o, stderr, 0);
|
||||
|
@ -131,11 +131,24 @@ static bool py_close(int sno) {
|
||||
static bool pygetLine(StreamDesc *rl_iostream, int sno) {
|
||||
// term_t ctk = python_acquire_GIL();
|
||||
const char *myrl_line;
|
||||
PyObject *user_line;
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
//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 *user_inp = PyObject_CallFunction(readl, NULL);
|
||||
myrl_line = PyUnicode_AsUTF8(user_inp);
|
||||
user_line = PyObject_CallFunction(readl, NULL);
|
||||
}
|
||||
myrl_line = PyUnicode_AsUTF8(user_line);
|
||||
if (myrl_line == NULL)
|
||||
return NULL;
|
||||
PyObject *err;
|
||||
|
@ -552,6 +552,27 @@ static foreign_t python_export(term_t t, term_t pl) {
|
||||
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)
|
||||
* Import a python module to the YAP environment.
|
||||
@ -593,6 +614,7 @@ static int python_import(term_t mname, term_t mod) {
|
||||
else
|
||||
return false;
|
||||
strcat(s, sn);
|
||||
//get_mod(s);
|
||||
strcat(s, ".");
|
||||
}
|
||||
sm = t;
|
||||
|
@ -25,6 +25,7 @@ class Engine( YAPEngine ):
|
||||
# type: (object) -> object
|
||||
if not args:
|
||||
args = EngineArgs(**kwargs)
|
||||
args.setEmbedded(True)
|
||||
if self_contained:
|
||||
yap_lib_path = dirname(__file__)
|
||||
args.setYapShareDir(join(yap_lib_path, "prolog"))
|
||||
|
@ -92,7 +92,7 @@ blank(Text) :-
|
||||
close(user_output),
|
||||
close(user_error).
|
||||
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.stderr', append, _Error, [alias(user_error)]).
|
||||
|
||||
|
Reference in New Issue
Block a user