This commit is contained in:
Vitor Santos Costa
2016-09-30 17:11:13 -05:00
parent 41ccbf7367
commit 43d3579c57
19 changed files with 397 additions and 411 deletions

View File

@@ -49,7 +49,11 @@ PyObject *term_to_python(term_t t, bool eval) {
else if (strcmp(s, "{}") == 0)
o = PyDict_New();
/* return __main__,s */
else if (PyObject_HasAttrString(py_Main, s)) {
else if ((o = PyRun_String(s, Py_single_input,
PyEval_GetGlobals(), PyEval_GetLocals()))) {
Py_IncRef(o);
return o;
} else if (PyObject_HasAttrString(py_Main, s)) {
o = PyObject_GetAttrString(py_Main, s);
} else {
o = PyUnicode_FromString(s);

View File

@@ -183,7 +183,12 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
#endif
} else if (PyTuple_Check(pVal)) {
Py_ssize_t i, sz = PyTuple_Size(pVal);
functor_t f = PL_new_functor(ATOM_t, sz);
functor_t f;
const char *s;
if ((s = (Py_TYPE(pVal)->tp_name)))
f = PL_new_functor(PL_new_atom(s), sz);
else
f = PL_new_functor(ATOM_t, sz);
if (!PL_unify_functor(t, f))
return FALSE;
for (i = 0; i < sz; i++) {

View File

@@ -934,7 +934,7 @@ PyObject *compound_to_pytree(term_t t, functor_t fun) {
term_t tleft = PL_new_term_ref();
PyObject *o = py_Main;
while (fun == FUNCTOR_dot2) {
if (!PL_get_arg(1, t, tleft))
if (!PL_get_arg(1, t, tleft))
return FALSE;
o = find_obj(o, tleft);
if (!o)

View File

@@ -45,8 +45,11 @@ static foreign_t python_f(term_t tmod, term_t fname, term_t tf) {
return FALSE;
}
pModule = PyImport_Import(pName);
} else if (!(pModule = term_to_python(tmod, true)))
PyErr_Clear();
} else if (!(pModule = term_to_python(tmod, true))) {
PyErr_Clear();
return FALSE;
}
if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
return FALSE;
}
@@ -263,6 +266,7 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
term_t targ = PL_new_term_ref();
pF = term_to_python(tin, true);
PyErr_Clear();
if (pF == NULL) {
return false;
}
@@ -567,6 +571,7 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
PyErr_Clear();
Py_DECREF(pName);
if (pModule != NULL) {
@@ -617,6 +622,10 @@ static foreign_t python_export(term_t t, term_t pl) {
return rc;
}
static foreign_t p_python_within_python(void) {
return python_in_python;
}
static int python_import(term_t mname, term_t mod) {
PyObject *pName, *pModule;
term_t arg = PL_new_term_ref();
@@ -650,6 +659,7 @@ static int python_import(term_t mname, term_t mod) {
return false;
}
pModule = PyImport_Import(pName);
PyErr_Clear();
Py_DECREF(pName);
if (pModule == NULL) {
#if EXTRA_MESSSAGES
@@ -689,4 +699,5 @@ install_t install_pypreds(void) {
PL_register_foreign("python_main_module", 1, python_main_module, 0);
PL_register_foreign("python_import", 2, python_import, 0);
PL_register_foreign("python_access", 3, python_access, 0);
PL_register_foreign("python_within_python", 0, p_python_within_python, 0);
}

View File

@@ -4,7 +4,7 @@
int assign_python(PyObject *root, term_t t, PyObject *e);
atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V;
ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V, ATOM_self;
functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
@@ -29,6 +29,8 @@ int assign_python(PyObject *root, term_t t, PyObject *e);
PyObject *ActiveModules[32];
int active_modules = 0;
bool python_in_python;
static void install_py_constants(void) {
FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
@@ -42,6 +44,7 @@ static void install_py_constants(void) {
ATOM_false = PL_new_atom("false");
ATOM_dot = PL_new_atom(".");
ATOM_none = PL_new_atom("none");
ATOM_self = PL_new_atom("self");
ATOM_t = PL_new_atom("t");
FUNCTOR_abs1 = PL_new_functor(PL_new_atom("abs"), 1);
FUNCTOR_all1 = PL_new_functor(PL_new_atom("all"), 1);
@@ -85,24 +88,28 @@ foreign_t end_python(void) {
X_API bool init_python(void) {
char **argv;
python_in_python = false;
if (YAP_DelayInit(init_python, "python")) {
// wait for YAP_Init
return false;
}
term_t t = PL_new_term_ref();
YAP_Argv(&argv);
if (argv) {
if (!Py_IsInitialized()) {
python_in_python = true;
YAP_Argv(&argv);
if (argv) {
#if PY_MAJOR_VERSION < 3
Py_SetProgramName(argv[0]);
Py_SetProgramName(argv[0]);
#else
wchar_t *buf = Py_DecodeLocale(argv[0], NULL);
wchar_t *buf = Py_DecodeLocale(argv[0], NULL);
Py_SetProgramName(buf);
#endif
}
Py_Initialize();
}
Py_Initialize();
install_py_constants();
PL_reset_term_refs(t);
install_pypreds();
install_pl2pl();
return true;
return !python_in_python;
}

View File

@@ -21,7 +21,7 @@
typedef YAP_Arity arity_t;
extern atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
ATOM_comma, ATOM_builtin, ATOM_V, ATOM_A;
ATOM_comma, ATOM_builtin, ATOM_V, ATOM_A, ATOM_self;
extern functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
@@ -37,6 +37,8 @@ extern PyObject *py_Builtin;
extern PyObject *py_Yapex;
extern PyObject *py_F2P;
extern bool python_in_python;
static inline Py_ssize_t get_p_int(PyObject *o, Py_ssize_t def) {
if (o == NULL)
return def;

View File

@@ -14,11 +14,40 @@ class YAPEngine;
#ifdef SWIGPYTHON
%typemap(out) YAPTerm {
if ($1.handle() == 0) {
return NULL;
}
%typemap(typecheck) YAPTerm* {
$1 = PySequence_Check($input);
}
// Map a Python sequence into any sized C double array
%typemap(in) YAPTerm* {
int i;
if (!PySequence_Check($input)) {
PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
$1 = nullptr;
} else {
int sz = PyObject_Length($input);
std::vector<YAPTerm> v(sz);
for (i =0; i < sz; i++) {
PyObject *o = PySequence_GetItem($input,i);
v[i] = YAPTerm(pythonToYAP(o));
Py_DECREF(o);
}
$1 = &v[0];
}
}
%typemap(typecheck) YAPTerm {
$1 = true;
}
%typemap(in) YAPTerm { $1 = YAPTerm(pythonToYAP($input)); }
%typemap(out) YAPTerm {$result = term_to_python($1.handle(), false);}
%extend(out) YAPTerm{YAPTerm & __getitem__(size_t i){Term t0 = $self->term();
if (IsApplTerm(t0)) {
@@ -30,120 +59,11 @@ if (IsApplTerm(t0)) {
return *new YAPTerm(HeadOfTerm(t0));
else if (i == 1)
return *new YAPTerm(TailOfTerm(t0));
}
return *new YAPTerm();
}
}
%typemap(in) YAPIntegerTerm {
#if PY_MAJOR_VERSION < 3
$1 = YAPIntegerTerm(PyInt_AsLong($input));
#else
$1 = YAPIntegerTerm(PyLong_AsLong($input));
#endif
}
%typemap(out) YAPIntegerTerm {
Term t = $input.term();
Int j = IntegerOfTerm(t);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong(j);
#else
return PyLong_FromLong(j);
#endif
}
%typemap(in) YAPFloatTerm {
$1 = YAPFloatTerm( PyFloat_AsDouble($input) );
}
%typemap(out) YAPFloatTerm {
Term t = $1nput.term();
Int double j = FloatOfTerm(t);
$1 = PyFloat_FromDouble(j);
}
// translate well-known names and existing
// Python symbols
// Everthing else let wrapped.
// as a term
%typemap(out) YAPAtomTerm {
const char *s = RepAtom(AtomOfTerm($1nput.term()))->StrOfAE;
PyObject *p;
if ((p = AtomToPy(s))) {
$1 = p;
} else {
$1 = Py_None;
}
}
// translate lists as Python Lists
// Python symbols
// Everthing else let wrapped.
// as a termpc
%typemap(in) YAPListTerm {
PyObject *p = $input;
Int len = PyTuple_Size(p);
if (len == 0) {
$1 = YAPListTerm(TermNil);
} else {
t = AbsPair(HR);
for (Int i = 0; i < len; i++) {
HR += 2;
HR[-2] = pythonToYAP(PyTuple_GetItem(p, i));
HR[-1] = AbsPair(HR+2);
}
HR[-1] = TermNil;
$1 = YAPListTerm(t);
}
}
}
%typemap(typecheck) YAPListTerm {
PyObject *it = $input;
$1 = PyTuple_CheckExact(it);
}
%typemap(in) YAPApplTerm {
char *o = Py_TYPE(p)->tp_name;
Int len = PyTuple_Size(p);
if (len == 0) {
$1 = nullptr;
} else {
Term t = MkNewApplTerm(Yap_MkFunctor(Yap_LookupAtom(o),len),len);
for (Int i = 0; i < len; i++) {
o[i] = pythonToYAP(PyTuple_GetItem(p, i));
}
$1 = YAPApplTerm(t);
}
}
%typemap(typecheck) YAPApplTerm {
PyObject *p = $input;
$1 = (PyTuple_Check(p) && !PyTuple_CheckExact(p));
}
// translate lists as Python Lists
// Python symbols
// Everthing else let wrapped.
// as a term
%typemap(out) YAPListTerm {
Term l = $1.term(), *end;
PyObject *list;
Int len = Yap_SkipList(&l, &end);
$result = list = PyList_New(len);
for (Int i = 0; i < len; i++) {
Term a = HeadOfTerm(l);
YAPTerm *argp1 = new YAPTerm(a);
PyObject *obj0 =
SWIG_NewPointerObj(SWIG_as_voidptr(argp1), SWIGTYPE_p_YAPTerm, 0 | 0);
l = TailOfTerm(l);
PyList_SetItem(list, i, obj0);
}
return list;
}
// Language independent exception handler