python
This commit is contained in:
@@ -116,40 +116,34 @@ X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) {
|
||||
return s_to_python(s, eval, p0);
|
||||
}
|
||||
|
||||
static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
|
||||
static bool entry_to_dictionary(PyObject *dict, Term targ,
|
||||
bool eval, bool cvt) {
|
||||
PyObject *lhs, *rhs;
|
||||
term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref();
|
||||
|
||||
functor_t fun;
|
||||
|
||||
AOK(PL_get_functor(targ, &fun), false);
|
||||
while (fun == FUNCTOR_comma2) {
|
||||
AOK(PL_get_arg(1, targ, tleft), false);
|
||||
if (!copy_to_dictionary(dict, tleft, taux, eval, cvt))
|
||||
return false;
|
||||
AOK(PL_get_arg(2, targ, targ), false);
|
||||
return copy_to_dictionary(dict, tright, taux, eval, cvt);
|
||||
PyObject *lhs = NULL, *rhs;
|
||||
Term t1, t2;
|
||||
const char *s;
|
||||
t1 = ArgOfTerm(1,targ);
|
||||
if (YAP_IsAtomTerm(t1)) {
|
||||
s = YAP_AtomName(YAP_AtomOfTerm(t1));
|
||||
} else if (IsStringTerm(t1)) {
|
||||
s = YAP_StringOfTerm(t1);
|
||||
} else if (IsIntegerTerm(t1)) {
|
||||
lhs = PyLong_FromLong(IntegerOfTerm(t1));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
||||
// Py_DECREF(lhs);
|
||||
// Py_DECREF(rhs);
|
||||
|
||||
AOK(PL_get_arg(1, targ, tleft), false);
|
||||
lhs = atom_to_python_string(tleft);
|
||||
|
||||
if (lhs == NULL) {
|
||||
return FALSE;
|
||||
lhs = PyUnicode_FromString(s);
|
||||
}
|
||||
AOK(PL_get_arg(2, targ, tright), false);
|
||||
rhs = term_to_python(tright, eval, NULL, cvt);
|
||||
|
||||
t2 = ArgOfTerm(2,targ);
|
||||
rhs = term_to_python(Yap_InitSlot(t2), eval, NULL, cvt);
|
||||
if (rhs == NULL) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
return PyDict_SetItem(dict, lhs, rhs) >= 0;
|
||||
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
||||
// Py_DECREF(lhs);
|
||||
// Py_DECREF(rhs);
|
||||
|
||||
return PyDict_SetItem(dict, lhs, rhs) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,10 +260,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
{
|
||||
term_t tail = PL_new_term_ref();
|
||||
functor_t fun;
|
||||
atom_t name;
|
||||
int arity;
|
||||
PyObject *rc;
|
||||
|
||||
|
||||
if (!PL_get_functor(t, &fun)) {
|
||||
PL_reset_term_refs(tail);
|
||||
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||
@@ -343,7 +334,6 @@ 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);
|
||||
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;
|
||||
}
|
||||
@@ -384,33 +374,29 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
return PyComplex_FromDoubles(d1, d2);
|
||||
}
|
||||
if (fun == FUNCTOR_curly1) {
|
||||
term_t targ = PL_new_term_ref(), taux = PL_new_term_ref();
|
||||
PyObject *dict;
|
||||
|
||||
AOK(PL_get_arg(1, t, t), NULL);
|
||||
PyObject *dict;
|
||||
Term yt = ArgOfTerm(1,Yap_GetFromHandle(t));
|
||||
if (!(dict = PyDict_New()))
|
||||
return NULL;
|
||||
Py_INCREF(dict);
|
||||
DebugPrintf("Dict %p\n", dict);
|
||||
|
||||
while (PL_is_functor(t, FUNCTOR_comma2)) {
|
||||
AOK(PL_get_arg(1, t, targ), NULL);
|
||||
AOK(PL_is_functor(targ, FUNCTOR_colon2), NULL);
|
||||
|
||||
AOK(copy_to_dictionary(dict, targ, taux, eval, cvt), NULL);
|
||||
AOK(PL_get_arg(2, t, t), NULL);
|
||||
}
|
||||
|
||||
if (PL_is_functor(t, FUNCTOR_colon2)) {
|
||||
AOK(copy_to_dictionary(dict, t, taux, eval, cvt), NULL);
|
||||
}
|
||||
return dict;
|
||||
while (IsApplTerm(yt) && FunctorOfTerm(yt) == FunctorComma) {
|
||||
if (!entry_to_dictionary(dict, ArgOfTerm(1,yt), eval, cvt))
|
||||
return NULL;
|
||||
yt = ArgOfTerm(2,yt);
|
||||
}
|
||||
if (entry_to_dictionary(dict, yt, eval, cvt))
|
||||
return dict;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
atom_t name;
|
||||
int arity;
|
||||
|
||||
AOK(PL_get_name_arity(t, &name, &arity), NULL);
|
||||
|
||||
if (name == ATOM_t) {
|
||||
int i;
|
||||
rc = PyTuple_New(arity);
|
||||
PyObject *rc = PyTuple_New(arity);
|
||||
for (i = 0; i < arity; i++) {
|
||||
term_t arg = PL_new_term_ref();
|
||||
if (!PL_get_arg(i + 1, t, arg)) {
|
||||
@@ -428,11 +414,9 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
|
||||
}
|
||||
}
|
||||
if (eval)
|
||||
rc = compound_to_pyeval(t, o, cvt);
|
||||
return compound_to_pyeval(t, o, cvt);
|
||||
else
|
||||
rc = compound_to_pytree(t, o, cvt);
|
||||
PL_reset_term_refs(tail);
|
||||
return rc;
|
||||
return compound_to_pytree(t, o, cvt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -133,8 +133,9 @@ else if (PyList_Check(pVal)) {
|
||||
if (sz == 0)
|
||||
return repr_term(pVal);
|
||||
Term t = TermNil;
|
||||
for (i = sz; i > 0; --i) {
|
||||
PyObject *p = PyTuple_GetItem(pVal, i);
|
||||
for (i = sz; i > 0; ) {
|
||||
-- i;
|
||||
PyObject *p = PyList_GetItem(pVal, i);
|
||||
if (p == NULL) {
|
||||
PyErr_Clear();
|
||||
return false;
|
||||
@@ -147,33 +148,33 @@ else if (PyList_Check(pVal)) {
|
||||
return t;
|
||||
}
|
||||
else if (PyDict_Check(pVal)) {
|
||||
Py_ssize_t pos = 0;
|
||||
int left = PyDict_Size(pVal);
|
||||
Py_ssize_t pos = 0, tot = PyDict_Size(pVal);
|
||||
PyObject *key, *value;
|
||||
Term f, *opt = &f, t;
|
||||
if (left == 0) {
|
||||
return repr_term(pVal);
|
||||
} else {
|
||||
while (PyDict_Next(pVal, &pos, &key, &value)) {
|
||||
Term t0[2], to;
|
||||
Term f, *opt = &f, t, to;
|
||||
while (PyDict_Next(pVal, &pos, &key, &value)) {
|
||||
Term t0[2];
|
||||
t0[0] = python_to_term__(key);
|
||||
t0[1] = python_to_term__(value);
|
||||
to = Yap_MkApplTerm(FunctorModule, 2, t0);
|
||||
if (left--) {
|
||||
if (pos < tot) {
|
||||
t = Yap_MkNewApplTerm(FunctorComma, 2);
|
||||
*opt = t;
|
||||
CELL *pt = RepAppl(t) + 1;
|
||||
pt[0] = to;
|
||||
opt = pt + 1;
|
||||
*opt = t;
|
||||
opt = pt+1;
|
||||
} else {
|
||||
*opt = t = to;
|
||||
if (pos == 0) {
|
||||
return repr_term(pVal);
|
||||
}
|
||||
|
||||
*opt = to;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Yap_MkApplTerm(FunctorBraces, 1, &t);
|
||||
return Yap_MkApplTerm(FunctorBraces, 1, &f);
|
||||
}
|
||||
}else {
|
||||
return repr_term(pVal);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
@@ -218,6 +219,7 @@ PyObject *py_Local, *py_Global;
|
||||
*python_assign.
|
||||
*/
|
||||
bool python_assign(term_t t, PyObject *exp, PyObject *context) {
|
||||
PyErr_Print();
|
||||
context = find_obj(context, t, false);
|
||||
// Yap_DebugPlWriteln(yt);
|
||||
switch (PL_term_type(t)) {
|
||||
@@ -244,7 +246,6 @@ bool python_assign(term_t t, PyObject *exp, PyObject *context) {
|
||||
context = py_Main;
|
||||
if (PyObject_SetAttrString(context, s, exp) == 0)
|
||||
return true;
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
case PL_INTEGER:
|
||||
|
@@ -82,15 +82,15 @@ extern functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
|
||||
FUNCTOR_dot2;
|
||||
|
||||
extern X_API PyObject *py_Main;
|
||||
extern X_API PyObject *py_Builtin;
|
||||
extern X_API PyObject *py_Yapex;
|
||||
extern X_API PyObject *py_Local;
|
||||
extern X_API PyObject *py_Atoms;
|
||||
extern X_API PyObject *py_Global;
|
||||
extern X_API PyObject *py_Context;
|
||||
extern PyObject *Py_f2p;
|
||||
extern PyObject *py_Sys;
|
||||
extern PyObject *py_ModDict;
|
||||
#define py_ModDict PyImport_GetModuleDict()
|
||||
#define py_Local PyEval_GetLocals()
|
||||
#define py_Global PyEval_GetGlobals()
|
||||
#define py_Builtin PyEval_GetBuiltins()
|
||||
|
||||
extern X_API bool python_in_python;
|
||||
|
||||
@@ -133,8 +133,13 @@ static inline int proper_ascii_string(const char *s) {
|
||||
static inline PyObject *atom_to_python_string(term_t t) {
|
||||
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
|
||||
// am\n");
|
||||
char *s = NULL;
|
||||
if (!PL_get_atom_chars(t, &s))
|
||||
const char *s = NULL;
|
||||
Term yapt = Yap_GetFromSlot(t);
|
||||
if (IsStringTerm(yapt))
|
||||
s = StringOfTerm(yapt);
|
||||
else if (IsAtomTerm(yapt))
|
||||
s = RepAtom(AtomOfTerm(yapt))->StrOfAE;
|
||||
else
|
||||
return NULL;
|
||||
/* return __main__,s */
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
|
@@ -59,7 +59,6 @@ PyObject *PythonLookupSpecial(const char *s) {
|
||||
}
|
||||
|
||||
static PyObject *builtin(const char *sp) {
|
||||
PyObject *py_Builtin = PyEval_GetBuiltins();
|
||||
return PyDict_GetItemString(py_Builtin, sp);
|
||||
}
|
||||
|
||||
@@ -73,7 +72,6 @@ PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
|
||||
if ((out = finalLookup(py_Context, sp))) {
|
||||
return out;
|
||||
}
|
||||
PyObject *py_Builtin = PyEval_GetBuiltins();
|
||||
if ((out = finalLookup(py_Builtin, sp))) {
|
||||
return out;
|
||||
}
|
||||
@@ -81,12 +79,10 @@ PyObject *lookupPySymbol(const char *sp, PyObject *pContext, PyObject **duc) {
|
||||
{
|
||||
return out;
|
||||
}
|
||||
PyObject *py_Local = PyEval_GetLocals();
|
||||
if ((out = finalLookup(py_Local, sp)) && out != Py_None) {
|
||||
return out;
|
||||
}
|
||||
PyObject *py_Global = PyEval_GetGlobals();
|
||||
if ((out = finalLookup(py_Global, sp))) {
|
||||
if ((out = finalLookup(py_Global, sp))) {
|
||||
return out;
|
||||
}
|
||||
if ((out = finalLookup(py_ModDict, sp))) {
|
||||
@@ -980,6 +976,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
|
||||
AOK(PL_get_arg(1, t, targ), NULL);
|
||||
ptr = term_to_python(targ, true, NULL, true);
|
||||
if (!ptr) return NULL;
|
||||
return PyObject_Dir(ptr);
|
||||
{}
|
||||
}
|
||||
@@ -1123,7 +1120,7 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
|
||||
}
|
||||
|
||||
PyObject *rc;
|
||||
if (ys && PyCallable_Check(ys)) {
|
||||
if ( PyCallable_Check(ys)) {
|
||||
//PyObject_Print(ys, stderr, 0);
|
||||
// PyObject_Print(pArgs, stderr, 0);
|
||||
// PyObject_Print(pyDict, stderr, 0);
|
||||
|
@@ -131,7 +131,7 @@ 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, *prompt;
|
||||
PyObject *user_line;
|
||||
StreamDesc *s = YAP_GetStreamFromId(sno);
|
||||
//term_t tg = python_acquire_GIL();
|
||||
if (!strcmp(RepAtom(s->name)->StrOfAE,"input") ) {
|
||||
@@ -147,7 +147,7 @@ static bool pygetLine(StreamDesc *rl_iostream, int sno) {
|
||||
user_line = PyObject_CallFunctionObjArgs(pystream, PyUnicode_FromString("?- ") , NULL);
|
||||
} else {
|
||||
PyObject *readl = PyObject_GetAttrString(s->u.private_data, "readline");
|
||||
user_line = PyObject_CallFunction(readl, PyUnicode_FromString("?- ") , NULL);
|
||||
user_line = PyObject_CallFunction(readl, "?- ");
|
||||
}
|
||||
myrl_line = PyUnicode_AsUTF8(user_line);
|
||||
if (myrl_line == NULL)
|
||||
|
@@ -8,7 +8,7 @@ PyObject *py_Main;
|
||||
void pyErrorHandler__(int line, const char *file, const char *code) {
|
||||
// this code is called if a Python error is found.
|
||||
// int lvl = push_text_stack();
|
||||
PyObject *type, *val;
|
||||
PyObject *type;
|
||||
// PyErr_Fetch(&type, &val, NULL);
|
||||
// PyErr_Print();
|
||||
// Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python
|
||||
|
@@ -21,34 +21,30 @@ functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1, FUNCTOR_as2
|
||||
FUNCTOR_dot2, FUNCTOR_brackets1;
|
||||
|
||||
X_API PyObject *py_Atoms;
|
||||
X_API PyObject *py_Builtin;
|
||||
X_API PyObject *py_Yapex;
|
||||
X_API PyObject *py_Sys;
|
||||
X_API PyObject * pYAPError;
|
||||
PyObject *py_Context;
|
||||
PyObject *py_ModDict;
|
||||
|
||||
X_API PyObject *Py_f2p;
|
||||
|
||||
extern X_API bool python_in_python;
|
||||
|
||||
static void add_modules(void) {
|
||||
py_Main = PyImport_AddModule("__main__");
|
||||
py_Atoms= PyDict_New();
|
||||
|
||||
if ( PyDict_Contains(PyImport_GetModuleDict(), PyUnicode_FromString("__main__"))) {
|
||||
py_Main = PyDict_GetItemString(PyImport_GetModuleDict(),"__main__");
|
||||
} else {
|
||||
py_Main = PyImport_ImportModule("__main__");
|
||||
}
|
||||
Py_INCREF(py_Main);
|
||||
py_Sys = PyImport_AddModule("sys");
|
||||
py_Atoms = PyDict_New();
|
||||
Py_INCREF(py_Sys);
|
||||
py_Builtin = PyImport_AddModule("__builtin__");
|
||||
Py_INCREF(py_Builtin);
|
||||
py_ModDict = PyObject_GetAttrString(py_Sys, "modules");
|
||||
// py_Yapex = PyImport_ImportModule("yap4py.yapi");
|
||||
// PyObject *py_Yap =
|
||||
py_Yapex = PyImport_AddModule("yap4py.yapi");
|
||||
py_Yapex = PyImport_ImportModule("yap4py.yapi");
|
||||
if (py_Yapex)
|
||||
Py_INCREF(py_Yapex);
|
||||
Py_f2p = PythonLookup("f2p", NULL);
|
||||
if (!Py_f2p)
|
||||
Py_f2p = PyList_New(0);
|
||||
Py_f2p = PyDict_New();
|
||||
Py_INCREF(Py_f2p);
|
||||
init_python_vfs();
|
||||
}
|
||||
@@ -119,7 +115,7 @@ X_API bool do_init_python(void) {
|
||||
|
||||
// PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
term_t t = PL_new_term_ref();
|
||||
if (!python_in_python)
|
||||
if (!Py_IsInitialized())
|
||||
Py_Initialize();
|
||||
install_py_constants();
|
||||
PL_reset_term_refs(t);
|
||||
|
Reference in New Issue
Block a user