2018-03-20 00:57:27 +00:00
|
|
|
#include "py4yap.h"
PyObject *py_Main;
void pyErrorHandler__(int line, const char *file, const char *code) {
// this code is called if a Python error is found.
fprintf(stderr, " Python error detected at %s %s:%d\n\n", code, file, line);
PyErr_Print();
}
static foreign_t python_len(term_t tobj, term_t tf) {
Py_ssize_t len;
PyObject *o;
o = term_to_python(tobj, true, NULL, true);
if (o == NULL) {
pyErrorAndReturn(false);
}
len = PyObject_Length(o);
pyErrorAndReturn(PL_unify_int64(tf, len));
}
static foreign_t python_dir(term_t tobj, term_t tf) {
PyObject *dir;
PyObject *o;
o = term_to_python(tobj, true, NULL, true);
if (o == NULL) {
pyErrorAndReturn(false);
}
dir = PyObject_Dir(o);
{
foreign_t rc = address_to_term(dir, tf);
;
pyErrorAndReturn(rc);
}
}
static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
PyObject *i;
PyObject *o;
PyObject *f;
o = term_to_python(tobj, true, NULL, true);
if (o == NULL) {
pyErrorAndReturn(false);
}
if (!PySequence_Check(o)) {
pyErrorAndReturn(false);
}
i = term_to_python(tindex, true, NULL, true);
if (i == NULL) {
pyErrorAndReturn(false);
}
#if PY_MAJOR_VERSION < 3
f = PyObject_CallMethodObjArgs(o, PyString_FromString("getitem"), i);
#else
f = PyObject_CallMethodObjArgs(o, PyUnicode_FromString("getitem"), i);
#endif
{
foreign_t rc = address_to_term(f, val);
;
pyErrorAndReturn(rc);
}
}
static foreign_t python_is(term_t tobj, term_t tf) {
PyObject *o;
term_t lim = python_acquire_GIL();
o = term_to_python(tobj, true, NULL, true);
if (!o) {
python_release_GIL(lim);
pyErrorAndReturn(false);
}
foreign_t rc = python_to_term(o, tf);
if (rc)
PyErr_Clear();
python_release_GIL(lim);
pyErrorAndReturn(rc);
}
static foreign_t python_proc(term_t tobj) {
PyObject *o;
term_t lim = python_acquire_GIL();
o = term_to_python(tobj, true, NULL, true);
python_release_GIL(lim);
bool rc = o != NULL;
pyErrorAndReturn(rc);
}
static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
PyObject *pF, *pI;
PyObject *p;
// get Scope ...
pI = term_to_python(indx, true, NULL, true);
// got Scope.Exp
// get Scope ...
p = term_to_python(parent, true, NULL, true);
// Exp
if (!pI || !p) {
{ pyErrorAndReturn(false); }
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
PyErr_Print();
{ pyErrorAndReturn(false); }
}
Py_DecRef(pI);
Py_DecRef(p);
Py_INCREF(pF);
{
foreign_t rc;
rc = address_to_term(pF, tobj);
pyErrorAndReturn(rc);
}
}
static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t tf) {
PyObject *pF;
PyObject *pArgs, *pKeywords;
PyObject *pValue;
int i, arity;
atom_t aname;
foreign_t out;
term_t targ = PL_new_term_ref();
pF = term_to_python(tin, true, NULL, true);
PyErr_Clear();
if (pF == NULL) {
{ pyErrorAndReturn(false); }
}
if (PL_is_atom(targs)) {
pArgs = NULL;
} else {
if (!PL_get_name_arity(targs, &aname, &arity)) {
{ pyErrorAndReturn(false); }
}
if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) {
/* ignore (_) */
pArgs = NULL;
} else {
pArgs = PyTuple_New(arity);
DebugPrintf("Tuple %p\n", pArgs);
if (!pArgs) {
pyErrorAndReturn(false);
}
for (i = 0; i < arity; i++) {
PyObject *pArg;
if (!PL_get_arg(i + 1, targs, targ)) {
pyErrorAndReturn(false);
}
pArg = term_to_python(targ, true, NULL, true);
if (pArg == NULL) {
pyErrorAndReturn(false);
}
/* pArg reference stolen here: */
PyTuple_SetItem(pArgs, i, pArg);
}
}
}
if (PL_is_atom(keywds)) {
pKeywords = NULL;
} else {
pKeywords = term_to_python(keywds, true, NULL, true);
}
if (PyCallable_Check(pF)) {
pValue = PyEval_CallObjectWithKeywords(pF, pArgs, pKeywords);
// PyObject_Print(pF,stderr,0);fprintf(stderr, "\n");
/
|