static foreign_t python_assign_item(term_t parent, term_t indx, term_t tobj) { PyObject *pF, *pI; PyObject *p; // get Scope ... pI = term_to_python(indx, true); // got Scope.Exp // get Scope ... p = term_to_python(parent, true); // Exp // get Scope ... pF = term_to_python(parent, true); // Exp if (!pI || !p) { { return false; } } else if (PyObject_SetItem(p, pI, pF)) { PyErr_Print(); { return false; } } Py_DecRef(pI); Py_DecRef(p); { return true; } }
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); if (o == NULL) { return false; } if (!PySequence_Check(o)) { return false; } i = term_to_python(tindex, true); if (i == NULL) { return 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 = python_to_ptr(f, val); ; return 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); // got Scope.Exp // get Scope ... p = term_to_python(parent, true); // Exp if (!pI || !p) { { return false; } } else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) { PyErr_Print(); { return false; } } Py_DecRef(pI); Py_DecRef(p); { foreign_t rc; rc = address_to_term(pF, tobj); return rc; } }
static foreign_t python_assign_field(term_t source, term_t name, term_t exp) { PyObject *e = term_to_python(exp, true), *root = term_to_python(source, true); if (e == NULL) { return false; } return assign_python(root, name, e) >= 0; }
static PyObject *bip_bin(term_t t) { PyObject *v; if (!PL_get_arg(1, t, t)) return NULL; v = term_to_python(t, true); return PyNumber_ToBase(v, 2); }
static PyObject *bip_int(term_t t) { PyObject *pVal, *o; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, true); #if PY_MAJOR_VERSION < 3 if (PyLong_Check(pVal)) { o = PyInt_FromLong(PyLong_AsLong(pVal)); } else if (PyInt_Check(pVal)) { return pVal; #else if (PyLong_Check(pVal)) { return pVal; #endif } else if (PyFloat_Check(pVal)) { #if PY_MAJOR_VERSION < 3 o = PyInt_FromLong(PyFloat_AsDouble(pVal)); #else o = PyLong_FromDouble(PyFloat_AsDouble(pVal)); #endif } else return NULL; Py_DECREF(pVal); return o; } static PyObject *bip_long(term_t t) { PyObject *pVal, *o; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, true); if (PyLong_Check(pVal)) { return pVal; #if PY_MAJOR_VERSION < 3 } else if (PyInt_Check(pVal)) { o = PyLong_FromLong(PyInt_AsLong(pVal)); #endif } else if (PyFloat_Check(pVal)) { o = pVal; } else return NULL; Py_DECREF(pVal); return o; }
static PyObject *bip_iter(term_t t) { PyObject *v; if (!PL_get_arg(1, t, t)) return NULL; v = term_to_python(t, true); return PyObject_GetIter(v); }
static foreign_t python_assign(term_t name, term_t exp) { PyObject *e = term_to_python(exp, true); if (e == NULL) { return false; } return assign_python(py_Main, name, e) >= 0; }
static PyObject *bip_abs(term_t t) { PyObject *pVal, *nVal; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, true); nVal = PyNumber_Absolute(pVal); Py_DecRef(pVal); return nVal; }
static foreign_t python_is(term_t tobj, term_t tf) { PyObject *o; o = term_to_python(tobj, true); if (!o) { return false; } foreign_t rc = python_to_ptr(o, tf); return rc; }
static foreign_t python_len(term_t tobj, term_t tf) { Py_ssize_t len; PyObject *o; o = term_to_python(tobj, true); if (o == NULL) { return false; } len = PyObject_Length(o); return PL_unify_int64(tf, len); }
/** assign a tuple to something: */ static foreign_t python_assign_tuple(term_t t_lhs, term_t t_rhs) { PyObject *e; Py_ssize_t sz; functor_t f; e = term_to_python(t_rhs, true); if (!e || !PyTuple_Check(e)) { return -1; } sz = PyTuple_Size(e); switch (PL_term_type(t_lhs)) { case PL_VARIABLE: return PL_unify(t_lhs, t_rhs); case PL_ATOM: return assign_python(py_Main, t_rhs, e); case PL_TERM: if (PL_get_functor(t_lhs, &f)) { term_t targ = PL_new_term_ref(); // assign a tuple to a tuple if (PL_functor_name(f) == ATOM_t && ((sz = PL_functor_arity(f)))) { Py_ssize_t i; for (i = 0; i < sz; i++) { PL_get_arg(i + 1, t_lhs, targ); assign_python(py_Main, targ, PyTuple_GetItem(e, i)); } } else if (PL_functor_name(f) == ATOM_comma) { int n = conj_size(t_lhs); if (n != sz) return -1; return conj_copy(t_lhs, e, 0); } else if (PL_functor_name(f) == ATOM_dot) { // vectors size_t len; term_t tail = PL_new_term_ref(); PL_skip_list(t_lhs, tail, &len); if (!PL_get_nil(tail)) return -1; term_t arg = tail; size_t i; for (i = 0; i < len; i++) { if (!PL_get_list(t_rhs, arg, t_rhs)) { return -1; } if (assign_python(py_Main, arg, PyTuple_GetItem(e, i)) < 0) return -1; } } } } return -1; }
PyObject *term_to_nametuple(const char *s, int arity, term_t t) { PyObject *o; #if PY_MAJOR_VERSION >= 3 PyTypeObject *typp; PyObject *key = PyUnicode_FromString(s); if (py_F2P && PyDict_Contains(py_F2P, key)) { typp = (PyTypeObject *)PyDict_GetItem(py_F2P, key); } else { typp = PyMem_Malloc(sizeof(PyTypeObject)); PyStructSequence_Desc *desc = PyMem_Malloc(sizeof(PyStructSequence_Desc)); desc->name = PyUnicode_AsUTF8(key); desc->doc = "YAPTerm"; desc->fields = pnull; desc->n_in_sequence = 32; if (PyStructSequence_InitType2(typp, desc) < 0) return NULL; typp->tp_str = structseq_str; typp->tp_repr = structseq_repr; // typp = PyStructSequence_NewType(desc); Py_INCREF(typp); // typp->tp_flags |= Py_TPFLAGS_HEAPTYPE; PyModule_AddObject(py_Yapex, s, (PyObject *)typp); if (py_F2P) PyDict_SetItem(py_F2P, key, (PyObject *)typp); } o = PyTuple_New(typp); #else o = PyTuple_New(arity); #endif term_t tleft = PL_new_term_ref(); int i; for (i = 0; i < arity; i++) { PyObject *pArg; if (!PL_get_arg(i + 1, t, tleft)) return NULL; pArg = term_to_python(tleft, false); if (pArg == NULL) return NULL; #if PY_MAJOR_VERSION >= 3 /* pArg reference stolen here: */ PyStructSequence_SET_ITEM(o, i, pArg); } ((PyStructSequence *)o)->ob_base.ob_size = arity; return o; #else /* pArg reference stolen here: */ PyTuple_SET_ITEM(o, i, pArg); }
static foreign_t python_field(term_t parent, term_t att, term_t tobj) { PyObject *pF; atom_t name; char *s; int arity; if (!PL_get_name_arity(att, &name, &arity)) { { return false; } } else { PyObject *p; // got Scope.Exp // get Scope ... p = term_to_python(parent, true); // Exp if (!PL_get_name_arity(att, &name, &arity)) { { return false; } } s = PL_atom_chars(name); if (arity == 1 && !strcmp(s, "()")) { if (!PL_get_arg(1, att, att)) { return false; } if (!PL_get_name_arity(att, &name, &arity)) { { return false; } } s = PL_atom_chars(name); } if (!s || !p) { { return false; } } else if ((pF = PyObject_GetAttrString(p, s)) == NULL) { PyErr_Clear(); { return false; } } } { foreign_t rc; rc = address_to_term(pF, tobj); return rc; } }
static foreign_t python_dir(term_t tobj, term_t tf) { PyObject *dir; PyObject *o; o = term_to_python(tobj, true); if (o == NULL) { return false; } dir = PyObject_Dir(o); { foreign_t rc = python_to_ptr(dir, tf); ; return rc; } }
static foreign_t python_f(term_t tmod, term_t fname, term_t tf) { char *s; size_t len; PyObject *pF, *pModule; /* if an atom, fetch again */ if (PL_is_atom(tmod)) { PyObject *pName; if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) { return FALSE; } #if PY_MAJOR_VERSION < 3 pName = PyString_FromString(s); #else pName = PyUnicode_FromString(s); #endif if (pName == NULL) { { return false; } } pModule = PyImport_Import(pName); 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; } } pF = PyObject_GetAttrString(pModule, s); PyErr_Print(); Py_DECREF(pModule); if (pF == NULL || !PyCallable_Check(pF)) { { return false; } } { foreign_t rc = python_to_ptr(pF, tf); return rc; } }
static PyObject *bip_float(term_t t, bool eval) { PyObject *pVal, *o; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, eval); if (PyLong_Check(pVal)) { o = PyFloat_FromDouble(PyLong_AsLong(pVal)); #if PY_MAJOR_VERSION < 3 } else if (PyInt_Check(pVal)) { o = PyFloat_FromDouble(PyInt_AsLong(pVal)); #endif } else if (PyFloat_Check(pVal)) { return pVal; } else return NULL; Py_DECREF(pVal); return o; }
static PyObject *bip_ord(term_t t) { PyObject *pVal; Py_ssize_t size; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, true); if (PyUnicode_Check(pVal)) { #if PY_MAJOR_VERSION < 3 size = PyUnicode_GET_SIZE(pVal); #else size = PyUnicode_GetLength(pVal); #endif if (size == 1) { #if PY_MAJOR_VERSION < 3 long ord = (long)*PyUnicode_AS_UNICODE(pVal); return PyInt_FromLong(ord); #else Py_UCS4 ord = PyUnicode_ReadChar(pVal, 0); return PyLong_FromLong(ord); #endif } return NULL; } else if (PyByteArray_Check(pVal)) { char *s = PyByteArray_AsString(pVal); if (s[1]) return NULL; #if PY_MAJOR_VERSION < 3 return PyInt_FromLong(s[0]); } else if (PyString_Check(pVal)) { char *s = PyString_AsString(pVal); if (s[1]) return NULL; return PyInt_FromLong(s[0]); #else return PyLong_FromLong(s[0]); #endif } else return NULL; }
/** * Python all * * @param t Prolog term with a previously constructed Python iterator * * @return the Python boolean `True` if all elements of the iterator are `True`, * `False` otherwise. */ static PyObject *bip_all(term_t t) { PyObject *it, *item, *v; PyObject *(*iternext)(PyObject *); int cmp; if (!PL_get_arg(1, t, t)) return NULL; v = term_to_python(t, true); it = PyObject_GetIter(v); if (it == NULL) return NULL; iternext = *Py_TYPE(it)->tp_iternext; // PyObject_Print(v, stderr, 0); for (;;) { item = iternext(it); if (item == NULL) break; cmp = PyObject_IsTrue(item); Py_DECREF(item); if (cmp < 0) { Py_DECREF(it); return NULL; } if (cmp == 0) { Py_DECREF(it); return Py_False; } } Py_DECREF(it); if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear(); else return NULL; } return Py_True; }
static foreign_t python_o(term_t tmod, term_t fname, term_t tf) { char *s; size_t len; PyObject *pO, *pModule; pModule = term_to_python(tmod, true); if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) { { return false; } } pO = PyObject_GetAttrString(pModule, s); if (pO == NULL) { { return false; } } { foreign_t rc = python_to_ptr(pO, tf); ; return 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); PyErr_Clear(); if (pF == NULL) { { return false; } } if (PL_is_atom(targs)) { pArgs = NULL; } else { if (!PL_get_name_arity(targs, &aname, &arity)) { { return false; } } if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) { /* ignore (_) */ pArgs = NULL; } else { pArgs = PyTuple_New(arity); if (!pArgs) { return false; } for (i = 0; i < arity; i++) { PyObject *pArg; if (!PL_get_arg(i + 1, targs, targ)) { return false; } pArg = term_to_python(targ, true); if (pArg == NULL) { return false; } /* pArg reference stolen here: */ PyTuple_SetItem(pArgs, i, pArg); } } } if (PL_is_atom(keywds)) { pKeywords = NULL; } else { pKeywords = term_to_python(keywds, true); } if (PyCallable_Check(pF)) { pValue = PyEval_CallObjectWithKeywords(pF, pArgs, pKeywords); // PyObject_Print(pF,stderr,0);fprintf(stderr, "\n"); // PyObject_Print(pArgs,stderr,0);fprintf(stderr, " "); // PyObject_Print(pKeywords,stderr,0);fprintf(stderr, "\n"); if (!pValue) PyErr_Print(); else Py_IncRef(pValue); } else if (pArgs == NULL) { pValue = pF; if (pF) { Py_IncRef(pValue); } } else { PyErr_Print(); { return false; } } if (pArgs) Py_DECREF(pArgs); Py_DECREF(pF); if (pValue == NULL) { return false; } out = python_to_ptr(pValue, tf); return out; }
static PyObject *bip_sum(term_t t) { PyObject *seq; PyObject *result = NULL; PyObject *temp, *item, *iter; if (!PL_get_arg(1, t, t)) return NULL; seq = term_to_python(t, true); iter = PyObject_GetIter(seq); if (iter == NULL) return NULL; if (result == NULL) { #if PY_MAJOR_VERSION < 3 result = PyInt_FromLong(0); #else result = PyLong_FromLong(0); #endif if (result == NULL) { Py_DECREF(iter); return NULL; } } else { #if PY_MAJOR_VERSION < 3 /* reject string values for 'start' parameter */ if (PyObject_TypeCheck(result, &PyBaseString_Type)) { PyErr_SetString(PyExc_TypeError, "sum() can't sum strings [use ''.join(seq) instead]"); Py_DECREF(iter); return NULL; } Py_INCREF(result); #endif } #ifndef SLOW_SUM /* Fast addition by keeping temporary sums in C instead of new Python objects. Assumes all inputs are the same type. If the assumption fails, default to the more general routine. */ #if PY_MAJOR_VERSION < 3 if (PyInt_CheckExact(result)) { long i_result = PyInt_AS_LONG(result); #else if (PyLong_CheckExact(result)) { long i_result = PyLong_AS_LONG(result); #endif Py_DECREF(result); result = NULL; while (result == NULL) { item = PyIter_Next(iter); if (item == NULL) { Py_DECREF(iter); if (PyErr_Occurred()) return NULL; #if PY_MAJOR_VERSION < 3 return PyInt_FromLong(i_result); #else return PyLong_FromLong(i_result); #endif } #if PY_MAJOR_VERSION < 3 if (PyInt_CheckExact(item)) { long b = PyInt_AS_LONG(item); #else if (PyLong_CheckExact(item)) { long b = PyLong_AS_LONG(item); #endif long x = i_result + b; if ((x ^ i_result) >= 0 || (x ^ b) >= 0) { i_result = x; Py_DECREF(item); continue; } } /* Either overflowed or is not an int. Restore real objects and process normally */ #if PY_MAJOR_VERSION < 3 result = PyInt_FromLong(i_result); #else result = PyLong_FromLong(i_result); #endif temp = PyNumber_Add(result, item); Py_DECREF(result); Py_DECREF(item); result = temp; if (result == NULL) { Py_DECREF(iter); return NULL; } } } if (PyFloat_CheckExact(result)) { double f_result = PyFloat_AS_DOUBLE(result); Py_DECREF(result); result = NULL; while (result == NULL) { item = PyIter_Next(iter); if (item == NULL) { Py_DECREF(iter); if (PyErr_Occurred()) return NULL; return PyFloat_FromDouble(f_result); } if (PyFloat_CheckExact(item)) { PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) f_result += PyFloat_AS_DOUBLE(item); PyFPE_END_PROTECT(f_result) Py_DECREF(item); continue; } #if PY_MAJOR_VERSION < 3 if (PyInt_CheckExact(item)) { PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) f_result += (double)PyInt_AS_LONG(item); PyFPE_END_PROTECT(f_result) Py_DECREF(item); continue; } #else if (PyLong_CheckExact(item)) { PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) f_result += PyLong_AsDouble(item); PyFPE_END_PROTECT(f_result) Py_DECREF(item); continue; } #endif result = PyFloat_FromDouble(f_result); temp = PyNumber_Add(result, item); Py_DECREF(result); Py_DECREF(item); result = temp; if (result == NULL) { Py_DECREF(iter); return NULL; } } #endif } for (;;) { item = PyIter_Next(iter); if (item == NULL) { /* error, or end-of-sequence */ if (PyErr_Occurred()) { Py_DECREF(result); result = NULL; } break; } /* It's tempting to use PyNumber_InPlaceAdd instead of PyNumber_Add here, to avoid quadratic running time when doing 'sum(list_of_lists, [])'. However, this would produce a change in behaviour: a snippet like empty = [] sum([[x] for x in range(10)], empty) would change the value of empty. */ temp = PyNumber_Add(result, item); Py_DECREF(result); Py_DECREF(item); result = temp; if (result == NULL) break; } Py_DECREF(iter); return result; } //@} static long get_int(term_t arg, bool eval) { long low; if (!PL_get_long(arg, &low)) { PyObject *low = term_to_python(arg, eval); if (PyLong_Check(low)) { return PyLong_AsLong(low); #if PY_MAJOR_VERSION < 3 } else if (PyInt_Check(low)) { return PyInt_AsLong(low); #endif } else { return 0; } } return low; } /* Return number of items in range/xrange (lo, hi, step). step > 0 * required. Return a value < 0 if & only if the true value is too * large to fit in a signed long. */ static long get_len_of_range(long lo, long hi, long step) { /* ------------------------------------------------------------- If lo >= hi, the range is empty. Else if n values are in the range, the last one is lo + (n-1)*step, which must be <= hi-1. Rearranging, n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so the RHS is non-negative and so truncation is the same as the floor. Letting M be the largest positive long, the worst case for the RHS numerator is hi=M, lo=-M-1, and then hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough precision to compute the RHS exactly. ---------------------------------------------------------------*/ long n = 0; if (lo < hi) { unsigned long uhi = (unsigned long)hi; unsigned long ulo = (unsigned long)lo; unsigned long diff = uhi - ulo - 1; n = (long)(diff / (unsigned long)step + 1); } return n; }
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) { PyObject *pI, *pArgs, *pOut; PyObject *env; atom_t name; char *s; int i, arity; term_t targ = PL_new_term_ref(); if ((env = py_Builtin) == NULL) { // no point in even trying { return false; } } if (PL_get_name_arity(caller, &name, &arity)) { if (!(s = PL_atom_chars(name))) { return false; } if ((pI = PyObject_GetAttrString(env, s)) == NULL) { PyErr_Print(); { return false; } } } else { // Prolog should make sure this never happens. { return false; } } pArgs = PyTuple_New(arity); for (i = 0; i < arity; i++) { PyObject *pArg; if (!PL_get_arg(i + 1, caller, targ)) { return false; } /* ignore (_) */ if (i == 0 && PL_is_variable(targ)) { pArg = Py_None; } else { pArg = term_to_python(targ, true); if (pArg == NULL) { return false; } } /* pArg reference stolen here: */ if (PyTuple_SetItem(pArgs, i, pArg)) { PyErr_Print(); { return false; } } } pOut = PyObject_CallObject(pI, pArgs); Py_DECREF(pArgs); Py_DECREF(pI); if (pOut == NULL) { PyErr_Print(); { return false; } } { foreign_t rc = python_to_ptr(pOut, out); ; return rc; } }
static foreign_t python_access(term_t obj, term_t f, term_t out) { PyObject *o = term_to_python(obj, true), *pValue, *pArgs, *pF; atom_t name; char *s; int i, arity; term_t targ = PL_new_term_ref(); if (o == NULL) { return false; } if (PL_is_atom(f)) { if (!PL_get_atom_chars(f, &s)) { return false; } if ((pValue = PyObject_GetAttrString(o, s)) == NULL) { PyErr_Print(); { return false; } } { return python_to_term(pValue, out); } } if (!PL_get_name_arity(f, &name, &arity)) { { return false; } } /* follow chains of the form a.b.c.d.e() */ while (name == ATOM_dot && arity == 2) { term_t tleft = PL_new_term_ref(); PyObject *lhs; if (!PL_get_arg(1, f, tleft)) { return false; } lhs = term_to_python(tleft, true); if ((o = PyObject_GetAttr(o, lhs)) == NULL) { PyErr_Print(); { return false; } } if (!PL_get_arg(2, f, f)) { return false; } if (!PL_get_name_arity(f, &name, &arity)) { { return false; } } } s = PL_atom_chars(name); if (!s) { return false; } if ((pF = PyObject_GetAttrString(o, s)) == NULL) { PyErr_Print(); { return false; } } pArgs = PyTuple_New(arity); for (i = 0; i < arity; i++) { PyObject *pArg; if (!PL_get_arg(i + 1, f, targ)) { return false; } /* ignore (_) */ if (i == 0 && PL_is_variable(targ)) { pArgs = Py_None; } pArg = term_to_python(targ, true); if (pArg == NULL) { return false; } /* pArg reference stolen here: */ PyTuple_SetItem(pArgs, i, pArg); } pValue = PyObject_CallObject(pF, pArgs); Py_DECREF(pArgs); Py_DECREF(pF); if (pValue == NULL) { { return false; } } { return python_to_term(pValue, out); } }
static foreign_t python_function(term_t tobj) { PyObject *obj = term_to_python(tobj, true); foreign_t rc = PyFunction_Check(obj); return rc; }