This commit is contained in:
Vitor Santos Costa 2018-11-27 10:32:06 +00:00
parent 9dc86c735b
commit 3d8858954d
11 changed files with 22 additions and 2840 deletions

View File

@ -39,6 +39,10 @@ ${CMAKE_SOURCE_DIR}/packages/swig
${CMAKE_SOURCE_DIR}/packages/Problog/problog_examples/output
${CMAKE_SOURCE_DIR}/packages/Problog/problog_examples/queries
${CMAKE_SOURCE_DIR}/packages/myddas/sqlite3/src
${CMAKE_SOURCE_DIR}/packages/gecode/6.0.1
${CMAKE_SOURCE_DIR}/packages/gecode/6.0.0
${CMAKE_SOURCE_DIR}/packages/gecode/5.1.0
${CMAKE_SOURCE_DIR}/packages/gecode/5.0.0
${CMAKE_SOURCE_DIR}/packages/gecode/4.4.0
${CMAKE_SOURCE_DIR}/packages/gecode/4.2.1
${CMAKE_SOURCE_DIR}/packages/gecode/4.2.0
@ -52,14 +56,19 @@ ${CMAKE_SOURCE_DIR}/packages/problog
${CMAKE_SOURCE_DIR}/packages/gecode/dev
${CMAKE_SOURCE_DIR}/C/traced_absmi_insts.h
${CMAKE_SOURCE_DIR}/H/globals.h
${CMAKE_SOURCE_DIR}/OPTYap/locks_alpha_funcs.h
${CMAKE_SOURCE_DIR}/OPTYap/locks_mips_funcs.h
${CMAKE_SOURCE_DIR}/OPTYap/locks_sparc.h
${CMAKE_SOURCE_DIR}/packages/cplint
${CMAKE_SOURCE_DIR}/packages/CLPBN/examples
${CMAKE_SOURCE_DIR}/packages/CLPBN/horus
${CMAKE_SOURCE_DIR}/packages/cuda
${CMAKE_SOURCE_DIR}/packages/prosqlite
${CMAKE_SOURCE_DIR}/packages/pyswip
${CMAKE_SOURCE_DIR}/packages/python/yap_kernel
${CMAKE_SOURCE_DIR}/packages/python/swig
${CMAKE_SOURCE_DIR}/packages/yap-lbfgs/liblbfgs-1.10
${CMAKE_SOURCE_DIR}/packages/swi-minisat2
${CMAKE_SOURCE_DIR}/library/dialect/swi/os
${CMAKE_SOURCE_DIR}/*/bprolog/*
${CMAKE_SOURCE_DIR}/*/prism/*
@ -77,6 +86,7 @@ ${CMAKE_SOURCE_DIR}/*/bprolog/*
)
foreach(i ${DOCS_EXCLUDE_})
string(APPEND DOCS_EXCLUDE ${i} " ")
endforeach(i ${DOCS_EXCLUDE_})

View File

@ -1090,7 +1090,7 @@ VERBATIM_HEADERS = YES
# classes, structs, unions or interfaces.
# The default value is: YES.
ALPHABETICAL_INDEX = NO
ALPHABETICAL_INDEX = YES
# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in
# which the alphabetical index list will be split.

View File

@ -1,10 +1,14 @@
@defgroup builtins Core Built-ins
@{
[TOC]
@brief This chapter describes the core built-in predicates that control the execution of
This chapter describes the core built-in predicates that control the execution of
Prolog programs, provide fundamental functionality such as termm manipulation or arithmetic, and support interaction with external
resources,
resources.
@{
Many of the predicates described here have been standardised by the International Standard Organization.
The corresponding standartised subset of Prolog also known as ISO-Prolog.
@ -20,6 +24,8 @@ notation will be used:
+ an argument with no preceding symbol can be used in both ways.
[TOC]
@}

View File

@ -21,7 +21,7 @@ The manual is organised as follows:
+ @ref run
+ @ref builtins
+ @subpage builtins
+ @ref extensions

View File

@ -5,10 +5,7 @@ markdown_extensions:
- toc:
permalink: True
- sane_lists
use_directory_urls: false
plugins:
- search
- awesome-pages:
filename: .index
disable_auto_arrange_index: false
collapse_single_pages: true

View File

@ -1,150 +0,0 @@
#include "py4yap.h"
static foreign_t array_to_python_list(term_t addr, term_t type, term_t szt,
term_t py) {
void *src;
Py_ssize_t sz, i;
int is_float;
if (!PL_get_pointer(addr, &src) || !PL_get_bool(type, &is_float) ||
!PL_get_intptr(szt, &sz))
return false;
PyObject *list = PyList_New(sz);
if (!list)
return false;
if (is_float) {
double *v = (double *)src;
for (i = 0; i < sz; i++) {
PyObject *x = PyFloat_FromDouble(v[i]);
PyList_SET_ITEM(list, i, x);
}
} else {
YAP_Int *v = (YAP_Int *)src;
for (i = 0; i < sz; i++) {
PyObject *x = PyFloat_FromDouble(v[i]);
PyList_SET_ITEM(list, i, x);
}
}
if (PL_is_variable(py)) {
return address_to_term(list, py);
}
return assign_to_symbol(py, list);
}
static foreign_t array_to_python_tuple(term_t addr, term_t type, term_t szt,
term_t py) {
void *src;
Py_ssize_t sz, i;
int is_float;
if (!PL_get_pointer(addr, &src) || !PL_get_bool(type, &is_float) ||
!PL_get_intptr(szt, &sz))
return false;
PyObject *list = PyTuple_New(sz);
if (!list)
return false;
if (is_float) {
double *v = (double *)src;
for (i = 0; i < sz; i++) {
PyObject *x;
x = PyFloat_FromDouble(v[i]);
if (PyTuple_SetItem(list, i, x)) {
PyErr_Print();
return FALSE;
}
}
} else {
int32_t *v = (int32_t *)src;
PyObject *x;
for (i = 0; i < sz; i++) {
#if PY_MAJOR_VERSION < 3
x = PyInt_FromLong(v[i]);
#else
x = PyLong_FromLong(v[i]);
#endif
if (PyTuple_SetItem(list, i, x)) {
PyErr_Print();
return FALSE;
}
}
}
if (PL_is_variable(py)) {
return address_to_term(list, py);
}
return assign_to_symbol(py, list);
}
static foreign_t array_to_python_view(term_t addr, term_t type, term_t szt,
term_t colt, term_t py) {
void *src;
Py_ssize_t sz, rows;
int is_float;
Py_ssize_t shape[2];
if (!PL_get_pointer(addr, &src) || !PL_get_bool(type, &is_float) ||
!PL_get_intptr(szt, &sz) || !PL_get_intptr(colt, &rows))
return false;
Py_buffer buf;
buf.buf = src;
if (is_float) {
buf.len = sz * sizeof(double);
buf.itemsize = sizeof(double);
} else {
buf.len = sz * sizeof(YAP_Int);
buf.itemsize = sizeof(YAP_Int);
}
buf.readonly = false;
buf.format = NULL;
buf.ndim = 2;
buf.shape = shape;
buf.strides = NULL;
buf.suboffsets = NULL;
PyObject *o = PyMemoryView_FromBuffer(&buf);
if (!o) {
PyErr_Print();
return false;
}
if (PL_is_variable(py)) {
return address_to_term(o, py);
}
return assign_to_symbol(py, o);
}
static foreign_t prolog_list_to_python_list(term_t plist, term_t pyt, term_t tlen) {
size_t sz, i;
PyErr_Clear();
PyObject *pyl = term_to_python(pyt, true, NULL);
term_t targ = PL_new_term_ref();
if (PL_skip_list(plist, targ, &sz) <0 || ! PL_get_nil(targ)) {
pyErrorAndReturn( false, false);
}
if (!PyList_Check(pyl))
{
pyErrorAndReturn( false, false);
}
if (sz > PyList_GET_SIZE(pyl))
pyErrorAndReturn( false, false);
for (i=0; i < sz; i++) {
if (!PL_get_list(plist, targ, plist)) {
pyErrorAndReturn( false, false);
}
PyObject *t = term_to_python(targ, true, NULL);
PyList_SET_ITEM(pyl, i, t);
}
if (PL_is_variable(tlen)) {
PL_unify_int64(tlen, sz);
} else {
python_assign(tlen, PyLong_FromUnsignedLong(sz), NULL);
}
pyErrorAndReturn( true, false);
}
install_t install_pl2pl(void) {
PL_register_foreign("array_to_python_list", 4, array_to_python_list, 0);
PL_register_foreign("array_to_python_tuple", 4, array_to_python_tuple, 0);
PL_register_foreign("array_to_python_view", 5, array_to_python_view, 0);
PL_register_foreign("prolog_list_to_python_list", 3, prolog_list_to_python_list, 0);
}

View File

@ -1,214 +0,0 @@
#include "py4yap.h"
extern PyObject *py_Local, *py_Global;
PyObject *YE(term_t t, int line, const char *file, const char *code) {
YAPPy_ThrowError__(file, code, line, SYSTEM_ERROR_INTERNAL,t,"");
}
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);
}
static PyObject *s_to_python(const char *s, bool eval, PyObject *p0) {
PyObject *o;
if (eval) {
o = PythonLookup(s, p0);
/* if (!o)
return o;
*/
} else {
o = PythonLookupSpecial(s);
}
if (o) {
Py_INCREF(o);
return CHECKNULL(YAP_MkStringTerm(s), o);
} else {
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
return pobj;
}
}
/**
* obtain the object matching a certain string.
*
* @param t handle to Prolog term
* @param t whether should try to evaluate evaluables.
*
* @return a Python object descriptor or NULL if failed
*/
X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) {
char *buf = malloc(strlen(s) + 1), *child;
PyObject *p1;
while ((child = strchr(s, '.')) != NULL) {
size_t len = child - s;
strncpy(buf, s, len);
buf[len] = '\0';
p1 = s_to_python(buf, eval, p0);
Py_DecRef(p0);
Py_IncRef(p1)
s = child + 1;
p0 = p1;
}
p1 = s_to_python(s, eval, p0);
Py_DecRef(p0);
return p1;
}
/**
* term_to_python translates and evaluates from Prolog to Python
*
* @param t handle to Prolog term
* @param t whether should try to evaluate evaluables.
*
* @return a Python object descriptor or NULL if failed
*/
PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
// o≈
PyObject *o;\
YAP_Term yt = YAP_GetFromSlot(t);
// Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) {
case PL_VARIABLE: {
if (t == 0) {
YAPPy_ThrowErrorYA(SYSTEM_ERROR_INTERNAL,t,"");
t }
PyObject *out = PyTuple_New(1);
PyTuple_SET_ITEM(out, 0, PyLong_FromLong(t));
Py_IncRef(out);
rc = term_to_nametuple("v", 1, out);
Py_IncRef(rc);
};
case PL_ATOM: {
YAP_Atom at = YAP_AtomOfTerm(yt);
const char *s;
s = YAP_AtomName(at);
if (eval) {
o = PythonLookup(s, o);
/* if (!o)
return o;
*/
} else {
o = PythonLookupSpecial(s);
}
if (o) {
Py_INCREF(o);
return CHECKNULL(t, o);
}
}
case PL_STRING: {
const char *s = NULL;
if (YAP_IsAtomTerm(yt)) {
s = YAP_AtomName(YAP_AtomOfTerm(yt));
} else if (YAP_IsStringTerm(yt)) {
s = YAP_StringOfTerm(yt);
} else {
return CHECKNULL(t, NULL);
}
#if PY_MAJOR_VERSION < 3
if (proper_ascii_string(s)) {
PyObject *o = PyString_FromStringAndSize(s, strlen(s));
Py_IncRef(o)
return CHECKNULL(t, o);
} else
#endif
{
// char *p = malloc(strlen(s)+1);
//strcpy(p, s);
PyObject *pobj = PyUnicode_FromString(s);
Py_IncRef(pobj);
return CHECKNULL(t, pobj);
}
} break;
case PL_INTEGER: {
int64_t j;
if (!PL_get_int64_ex(t, &j))
return CHECKNULL(t, NULL);
#if PY_MAJOR_VERSION < 3
PyObject *o = PyInt_FromLong(j);
return CHECKNULL(t, o);
#else
PyObject *o = PyLong_FromLong(j);
return CHECKNULL(t, o);
#endif
}
case PL_FLOAT: {
PyObject *out;
double fl;
if (!PL_get_float(t, &fl))
return CHECKNULL(t, NULL);
out = PyFloat_FromDouble(fl);
return CHECKNULL(t, out);
}
default: {
term_t tail = PL_new_term_ref(), arg;
size_t len, i;
if (PL_skip_list(t, tail, &len) && PL_get_nil(tail)) {
PyObject *out, *a;
arg = tail;
out = PyList_New(len);
if (!out) {
PL_reset_term_refs(tail);
return CHECKNULL(t, Py_None);
}
for (i = 0; i < len; i++) {
if (!PL_get_list(t, arg, t)) {
YAPPy_ThrowErrorYA(SYSTEM_ERROR_INTERNAL,t,"");
}
a = term_to_python(arg, eval, o);
if (a) {
if (PyList_SetItem(out, i, a) < 0) {
YAPPy_ThrowErrorYA(SYSTEM_ERROR_INTERNAL,t,"");
}
}
}
PL_reset_term_refs(tail);
return CHECKNULL(t, out);
} else {
functor_t fun;
PyObject *rc;
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
return CHECKNULL(t, Py_None);
}
if (eval)
rc = compound_to_pyeval(t, o);
else
rc = compound_to_pytree(t, o);
PL_reset_term_refs(tail);
return rc;
}
}
}
return CHECKNULL(t, Py_None);
}
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
if (t == 0)
return NULL;
term_t yt = YAP_InitSlot(t);
o = term_to_python(yt, eval, o);
PL_reset_term_refs(yt);
return o;
}
PyObject *deref_term_to_python(term_t t) {
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
// am\n");
YAP_Term yt = YAP_GetFromSlot(t);
if (YAP_IsVarTerm(yt)) {
char s[32];
char *o = YAP_WriteBuffer(yt, s, 31, 0);
PyObject *p = PyUnicode_FromString(o);
return p;
}
return term_to_python(t, false, NULL);
}

View File

@ -1,319 +0,0 @@
#include "py4yap.h"
static foreign_t repr_term(PyObject *pVal, term_t t) {
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
PL_put_pointer(t1, pVal);
PL_cons_functor(to, FUNCTOR_pointer1, t1);
Py_INCREF(pVal);
return PL_unify(t, to);
}
foreign_t assign_to_symbol(term_t t, PyObject *e);
foreign_t assign_to_symbol(term_t t, PyObject *e) {
char *s = NULL;
if (!PL_get_atom_chars(t, &s)) {
return false;
}
PyObject *dic;
if (!lookupPySymbol(s, NULL, &dic))
dic = py_Main;
return PyObject_SetAttrString(dic, s, e) == 0;
}
foreign_t python_to_term(PyObject *pVal, term_t t) {
bool rc = true;
term_t to = PL_new_term_ref();
// fputs(" <<*** ",stderr); PyObject_Print(pVal,stderr,0);
// fputs("<<***\n",stderr);
if (pVal == Py_None) {
// fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// >>***\n",stderr);
rc = PL_unify_atom(t, ATOM_none);
// fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// >>***\n",stderr);
} else if (PyBool_Check(pVal)) {
rc = rc && PL_unify_bool(t, PyObject_IsTrue(pVal));
} else if (PyLong_Check(pVal)) {
rc = rc && PL_unify_int64(t, PyLong_AsLong(pVal));
#if PY_MAJOR_VERSION < 3
} else if (PyInt_Check(pVal)) {
rc = rc && PL_unify_int64(t, PyInt_AsLong(pVal));
#endif
} else if (PyFloat_Check(pVal)) {
rc = rc && PL_unify_float(t, PyFloat_AsDouble(pVal));
} else if (PyComplex_Check(pVal)) {
term_t t1 = PL_new_term_ref(), t2 = PL_new_term_ref();
if (!PL_put_float(t1, PyComplex_RealAsDouble(pVal)) ||
!PL_put_float(t2, PyComplex_ImagAsDouble(pVal)) ||
!PL_cons_functor(to, FUNCTOR_complex2, t1, t2)) {
rc = false;
} else {
rc = rc && PL_unify(t, to);
}
} else if (PyUnicode_Check(pVal)) {
atom_t tmp_atom;
#if PY_MAJOR_VERSION < 3
size_t sz = PyUnicode_GetSize(pVal) + 1;
wchar_t *ptr = malloc(sizeof(wchar_t) * sz);
sz = PyUnicode_AsWideChar((PyUnicodeObject *)pVal, ptr, sz - 1);
tmp_atom = PL_new_atom_wchars(sz, ptr);
free(ptr);
#else
const char *s = PyUnicode_AsUTF8(pVal);
tmp_atom = PL_new_atom(s);
#endif
rc = rc && PL_unify_atom(t, tmp_atom);
} else if (PyByteArray_Check(pVal)) {
atom_t tmp_atom = PL_new_atom(PyByteArray_AsString(pVal));
rc = rc && PL_unify_atom(t, tmp_atom);
#if PY_MAJOR_VERSION < 3
} else if (PyString_Check(pVal)) {
atom_t tmp_atom = PL_new_atom(PyString_AsString(pVal));
rc = rc && PL_unify_atom(t, tmp_atom);
#endif
} else if (PyTuple_Check(pVal)) {
Py_ssize_t i, sz = PyTuple_Size(pVal);
functor_t f;
const char *s;
if (sz == 0) {
rc = rc && PL_unify_atom(t, ATOM_brackets);
} else {
if ((s = (Py_TYPE(pVal)->tp_name))) {
if (!strcmp(s, "H")) {
pVal = PyTuple_GetItem(pVal, 0);
if (pVal == NULL) {
pVal = Py_None;
PyErr_Clear();
}
}
if (s[0] == '$') {
char *ns = malloc(strlen(s) + 5);
strcpy(ns, "__");
strcat(ns, s + 1);
strcat(ns, "__");
f = PL_new_functor(PL_new_atom(ns), sz);
} else {
f = PL_new_functor(PL_new_atom(s), sz);
}
} else {
f = PL_new_functor(ATOM_t, sz);
}
if (PL_unify_functor(t, f)) {
for (i = 0; i < sz; i++) {
if (!PL_get_arg(i + 1, t, to))
rc = false;
PyObject *p = PyTuple_GetItem(pVal, i);
if (p == NULL) {
PyErr_Clear();
p = Py_None;
}
rc = rc && python_to_term(p, to);
}
} else {
rc = false;
}
// fputs(" ||*** ",stderr); Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs("
// ||***\n",stderr);
}
} else if (PyList_Check(pVal)) {
Py_ssize_t i, sz = PyList_GET_SIZE(pVal);
if (sz == 0) {
rc = rc && PL_unify_nil(t);
} else {
for (i = 0; i < sz; i++) {
PyObject *obj;
if (!PL_unify_list(t, to, t)) {
rc = false;
break;
}
if ((obj = PyList_GetItem(pVal, i)) == NULL) {
obj = Py_None;
}
rc = rc && python_to_term(obj, to);
}
rc = rc && PL_unify_nil(t);
}
// fputs("[***] ", stderr);
// Yap_DebugPlWrite(yt); fputs("[***]\n", stderr);
} else if (PyDict_Check(pVal)) {
Py_ssize_t pos = 0;
term_t to = PL_new_term_ref(), ti = to;
int left = PyDict_Size(pVal);
PyObject *key, *value;
if (left == 0) {
rc = rc && PL_unify_atom(t, ATOM_curly_brackets);
} else {
while (PyDict_Next(pVal, &pos, &key, &value)) {
term_t tkey = PL_new_term_ref(), tval = PL_new_term_ref(), tint,
tnew = PL_new_term_ref();
/* do something interesting with the values... */
if (!python_to_term(key, tkey)) {
continue;
}
if (!python_to_term(value, tval)) {
continue;
}
/* reuse */
tint = tkey;
if (!PL_cons_functor(tint, FUNCTOR_colon2, tkey, tval)) {
rc = false;
continue;
}
if (--left) {
if (!PL_cons_functor(tint, FUNCTOR_comma2, tint, tnew))
PL_reset_term_refs(tkey);
rc = false;
}
if (!PL_unify(ti, tint)) {
rc = false;
}
ti = tnew;
PL_reset_term_refs(tkey);
}
rc = rc && PL_unify(t, to);
}
} else {
rc = rc && repr_term(pVal, t);
}
PL_reset_term_refs(to);
return rc;
}
X_API YAP_Term pythonToYAP(PyObject *pVal) {
term_t t = PL_new_term_ref();
if (pVal == NULL || !python_to_term(pVal, t)) {
PL_reset_term_refs(t);
return 0;
}
YAP_Term tt = YAP_GetFromSlot(t);
PL_reset_term_refs(t);
Py_DECREF(pVal);
return tt;
}
PyObject *py_Local, *py_Global;
/**
* assigns the Python RHS to a Prolog term LHS, ie LHS = RHS
*
* @param root Python environment
* @param t left hand side, in Prolog, may be
* - a Prolog variable, exports the term to Prolog, A <- RHS
* - Python variable A, A <- RHS
* - Python variable $A, A <- RHS
* - Python string "A", A <- RHS
* - Python array range
* @param e the right-hand side
*
* @return -1 on failure.
*
* Note that this is an auxiliary routine to the Prolog
*python_assign.
*/
bool python_assign(term_t t, PyObject *exp, PyObject *context) {
context = find_obj(context, t, false);
// Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) {
case PL_VARIABLE: {
if (context == NULL) // prevent a.V= N*N[N-1]
return python_to_term(exp, t);
}
case PL_ATOM: {
char *s = NULL;
PL_get_atom_chars(t, &s);
if (!context)
context = py_Main;
return PyObject_SetAttrString(context, s, exp) == 0;
}
case PL_STRING:
case PL_INTEGER:
case PL_FLOAT:
// domain or type erro?
return false;
default: {
term_t tail = PL_new_term_ref(), arg = PL_new_term_ref();
size_t len, i;
if (PL_skip_list(t, tail, &len) &&
PL_get_nil(tail)) { // true list
bool o = true;
if (PySequence_Check(exp) && PySequence_Length(exp) == len)
for (i = 0; i < len; i++) {
PyObject *p;
if (!PL_get_list(t, arg, t)) {
PL_reset_term_refs(tail);
o = false;
p = Py_None;
}
if ((p = PySequence_GetItem(exp, i)) == NULL)
p = Py_None;
if (!python_assign(arg, p, context)) {
PL_reset_term_refs(tail);
o = false;
}
}
PL_reset_term_refs(tail);
return o;
} else {
functor_t fun;
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
return false;
}
if (fun == FUNCTOR_sqbrackets2) {
if (!PL_get_arg(2, t, tail)) {
PL_reset_term_refs(tail);
return false;
}
PyObject *o = term_to_python(tail, true, context);
if (!PL_get_arg(2, t, tail) && !PL_get_nil(tail)) {
PL_reset_term_refs(tail);
return false;
}
if (!PL_get_arg(1, t, t)) {
PL_reset_term_refs(tail);
return false;
}
PL_reset_term_refs(tail);
PyObject *i = term_to_python(t, true, NULL);
if (!i) {
return false;
}
if (PyList_Check(i)) {
i = PyList_GetItem(i, 0);
if (i == NULL)
i = Py_None;
long int j;
if (PyList_Check(o)) {
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(i))
j = PyInt_AsLong(i);
else
#endif
if (PyLong_Check(i))
j = PyLong_AsLong(i);
else
return NULL;
return PyList_SetItem(o, j, exp) == 0;
}
if (PyDict_Check(i)) {
return PyDict_SetItem(o, i, exp) == 0;
}
return PyObject_SetAttr(o, i, exp) == 0;
}
}
}
}
}
return NULL;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,687 +0,0 @@
#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);
if (o == NULL) {
pyErrorAndReturn(false, false);
}
len = PyObject_Length(o);
pyErrorAndReturn(PL_unify_int64(tf, len), false);
}
static foreign_t python_dir(term_t tobj, term_t tf) {
PyObject *dir;
PyObject *o;
o = term_to_python(tobj, true, NULL);
if (o == NULL) {
pyErrorAndReturn(false, false);
}
dir = PyObject_Dir(o);
{
foreign_t rc = address_to_term(dir, tf);
;
pyErrorAndReturn(rc, false);
}
}
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);
if (o == NULL) {
pyErrorAndReturn(false, false);
}
if (!PySequence_Check(o)) {
pyErrorAndReturn(false, false);
}
i = term_to_python(tindex, true, NULL);
if (i == NULL) {
pyErrorAndReturn(false, 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, false);
}
}
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);
if (!o) {
python_release_GIL(lim);
pyErrorAndReturn(false, false);
}
foreign_t rc = python_to_term(o, tf);
if (rc)
PyErr_Clear();
python_release_GIL(lim);
pyErrorAndReturn(rc, false);
}
static foreign_t python_proc(term_t tobj) {
PyObject *o;
term_t lim = python_acquire_GIL();
o = term_to_python(tobj, true, NULL);
python_release_GIL(lim);
bool rc = o != NULL;
pyErrorAndReturn(rc , false);
}
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);
// got Scope.Exp
// get Scope ...
p = term_to_python(parent, true, NULL);
// Exp
if (!pI || !p) {
{ pyErrorAndReturn(false, false); }
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
PyErr_Print();
{ pyErrorAndReturn(false, false); }
}
Py_DecRef(pI);
Py_DecRef(p);
Py_INCREF(pF);
{
foreign_t rc;
rc = address_to_term(pF, tobj);
pyErrorAndReturn(rc, false);
}
}
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);
PyErr_Clear();
if (pF == NULL) {
{ pyErrorAndReturn(false, false); }
}
if (PL_is_atom(targs)) {
pArgs = NULL;
} else {
if (!PL_get_name_arity(targs, &aname, &arity)) {
{ pyErrorAndReturn(false, 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, false);
}
for (i = 0; i < arity; i++) {
PyObject *pArg;
if (!PL_get_arg(i + 1, targs, targ)) {
pyErrorAndReturn(false, false);
}
pArg = term_to_python(targ, true, NULL);
if (pArg == NULL) {
pyErrorAndReturn(false, 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);
}
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();
{ pyErrorAndReturn(false, false); }
}
if (pArgs)
Py_DECREF(pArgs);
Py_DECREF(pF);
if (pValue == NULL) {
pyErrorAndReturn(false, false);
}
out = address_to_term(pValue, tf);
pyErrorAndReturn(out, false);
}
static foreign_t assign_python(term_t exp, term_t name) {
term_t stackp = python_acquire_GIL();
PyObject *e = term_to_python(exp, true, NULL);
if (e == NULL) {
python_release_GIL(stackp);
pyErrorAndReturn(false, false);
}
bool b = python_assign(name, e, NULL);
python_release_GIL(stackp);
pyErrorAndReturn(b, false);
}
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
PyErr_Clear();
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
{ pyErrorAndReturn(false, false); }
}
if (PL_get_name_arity(caller, &name, &arity)) {
if (!(s = PL_atom_chars(name))) {
pyErrorAndReturn(false, false);
}
if ((pI = PyObject_GetAttrString(env, s)) == NULL) {
PyErr_Print();
{ pyErrorAndReturn(false, false); }
}
Py_INCREF(pI);
} else {
// Prolog should make sure this never happens.
{ pyErrorAndReturn(false, false); }
}
pArgs = PyTuple_New(arity);
DebugPrintf("Tuple %p\n", pArgs);
for (i = 0; i < arity; i++) {
PyObject *pArg;
if (!PL_get_arg(i + 1, caller, targ)) {
pyErrorAndReturn(false, false);
}
/* ignore (_) */
if (i == 0 && PL_is_variable(targ)) {
pArg = Py_None;
} else {
pArg = term_to_python(targ, true, NULL);
if (pArg == NULL) {
pyErrorAndReturn(false, false);
}
}
/* pArg reference stolen here: */
if (PyTuple_SetItem(pArgs, i, pArg)) {
PyErr_Print();
{ pyErrorAndReturn(false, false); }
}
}
pOut = PyObject_CallObject(pI, pArgs);
Py_DECREF(pArgs);
Py_DECREF(pI);
if (pOut == NULL) {
PyErr_Print();
{ pyErrorAndReturn(false, false); }
}
{
foreign_t rc = address_to_term(pOut, out);
;
pyErrorAndReturn(rc, false);
}
}
static foreign_t python_access(term_t obj, term_t f, term_t out) {
PyErr_Clear();
PyObject *o = term_to_python(obj, true, NULL), *pValue, *pArgs, *pF;
atom_t name;
char *s = NULL;
int i, arity;
term_t targ = PL_new_term_ref();
if (o == NULL) {
pyErrorAndReturn(false, false);
}
if (PL_is_atom(f)) {
if (!PL_get_atom_chars(f, &s)) {
pyErrorAndReturn(false, false);
}
if ((pValue = PyObject_GetAttrString(o, s)) == NULL) {
PyErr_Print();
{ pyErrorAndReturn(false, false); }
}
Py_INCREF(pValue);
{ pyErrorAndReturn(python_to_term(pValue, out), false); }
}
if (!PL_get_name_arity(f, &name, &arity)) {
{ pyErrorAndReturn(false, false); }
}
s = PL_atom_chars(name);
if (!s) {
pyErrorAndReturn(false, false);
}
if ((pF = PyObject_GetAttrString(o, s)) == NULL) {
DebugPrintf("Function %p\n", pArgs);
PyErr_Print();
{ pyErrorAndReturn(false, false); }
}
pArgs = PyTuple_New(arity);
DebugPrintf("Tuple %p\n", pArgs);
for (i = 0; i < arity; i++) {
PyObject *pArg;
if (!PL_get_arg(i + 1, f, targ)) {
pyErrorAndReturn(false, false);
}
/* ignore (_) */
if (i == 0 && PL_is_variable(targ)) {
pArgs = Py_None;
}
pArg = term_to_python(targ, true, NULL);
if (pArg == NULL) {
pyErrorAndReturn(false, false);
}
/* pArg reference stolen here: */
PyTuple_SetItem(pArgs, i, pArg);
}
pValue = PyObject_CallObject(pF, pArgs);
Py_DECREF(pArgs);
Py_DECREF(pF);
if (pValue == NULL) {
{ pyErrorAndReturn(false, false); }
}
{ pyErrorAndReturn(python_to_term(pValue, out), false); }
}
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)) {
{ pyErrorAndReturn(false, false); }
} else {
PyObject *p;
// got Scope.Exp
// get Scope ...
p = term_to_python(parent, true, NULL);
// Exp
if (!PL_get_name_arity(att, &name, &arity)) {
{ pyErrorAndReturn(false, false); }
}
s = PL_atom_chars(name);
if (arity == 1 && !strcmp(s, "()")) {
if (!PL_get_arg(1, att, att)) {
pyErrorAndReturn(false, false);
}
if (!PL_get_name_arity(att, &name, &arity)) {
{ pyErrorAndReturn(false, false); }
}
s = PL_atom_chars(name);
}
if (!s || !p) {
{ pyErrorAndReturn(false, false); }
} else if ((pF = PyObject_GetAttrString(p, s)) == NULL) {
PyErr_Clear();
{ pyErrorAndReturn(false, false); }
}
}
{
foreign_t rc;
rc = address_to_term(pF, tobj);
pyErrorAndReturn(rc, false);
}
}
static foreign_t python_main_module(term_t mod) {
{
foreign_t rc;
PyErr_Clear();
rc = address_to_term(py_Main, mod);
pyErrorAndReturn(rc, false);
}
}
static foreign_t python_function(term_t tobj) {
PyErr_Clear();
PyObject *obj = term_to_python(tobj, true, NULL);
foreign_t rc = PyFunction_Check(obj);
pyErrorAndReturn(rc, false);
}
foreign_t python_builtin(term_t out) {
{
foreign_t rc;
PyErr_Clear();
rc = address_to_term(py_Builtin, out);
pyErrorAndReturn(rc, false);
}
}
static foreign_t python_run_file(term_t file) {
char *s;
size_t len;
char si[256];
s = si;
PyErr_Clear();
if (PL_get_nchars(file, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
#if PY_MAJOR_VERSION < 3
PyObject *PyFileObject = PyFile_FromString(si, "r");
PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "test.py", 1);
#else
FILE *f = fopen(s, "r");
if (f == NULL) {
pyErrorAndReturn(false, false);
}
PyRun_SimpleFileEx(f, s, 1);
#endif
{
{ pyErrorAndReturn(true, false); }
}
}
{ pyErrorAndReturn(false, false); }
}
extern PyThreadState *YAP_save;
static foreign_t python_run_command(term_t cmd) {
char *s;
bool rc = false;
size_t len;
char si[256];
PyErr_Clear();
s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
PyRun_SimpleString(s);
rc = true;
}
pyErrorAndReturn(rc, false);
}
static foreign_t python_run_script(term_t cmd, term_t fun) {
char si[256], sf[256];
size_t len = 255, len1 = 255;
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs = NULL, *pValue;
char *s;
PyErr_Clear();
s = si;
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION) &&
(s = sf) != NULL &&
PL_get_nchars(fun, &len1, &s, CVT_ALL | CVT_EXCEPTION)) {
#if PY_MAJOR_VERSION < 3
pName = PyString_FromString("rbm");
#else
// asssumes UTF-8
pName = PyUnicode_FromString("rbm");
#endif
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
PyErr_Clear();
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, sf);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
pValue = PyObject_CallObject(pFunc, pArgs);
if (pValue != NULL) {
Py_DECREF(pValue);
} else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr, "Call failed\n");
{ pyErrorAndReturn(false, false); }
}
} else {
pyErrorHandler();
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", sf);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
} else {
PyErr_Print();
{ pyErrorAndReturn(false, false); }
}
{ pyErrorAndReturn(true, false); }
}
{ pyErrorAndReturn(false, false); }
}
static foreign_t python_export(term_t t, term_t pl) {
foreign_t rc = false;
PyErr_Clear();
if (PL_is_functor(t, FUNCTOR_pointer1)) {
void *ptr;
term_t targ = PL_new_term_ref();
if (!PL_get_arg(1, t, targ)) {
pyErrorAndReturn(false, false);
}
if (!PL_get_pointer(targ, &ptr)) {
pyErrorAndReturn(false, false);
}
Py_INCREF((PyObject *)ptr);
/* pyErrorAndReturn( __main__, false) */
rc = python_to_term((PyObject *)ptr, pl);
}
pyErrorAndReturn(rc, false);
}
/**
* @pred python_import(MName, Mod)
* Import a python module to the YAP environment.
*
* @param mname module name, either an atom or a sequence of atoms,
* eg os.sys
* @param mod the pointer to the Python object
* @return success?
*/
static int python_import(term_t mname, term_t mod) {
PyObject *pName;
term_t t0 = python_acquire_GIL();
term_t arg = PL_new_term_ref();
char s0[MAXPATHLEN], *s = s0;
while (true) {
size_t len;
len = (MAXPATHLEN - 1) - (s - s0);
if (PL_is_pair(mname)) {
char *sa = NULL;
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) ||
!PL_get_arg(2, mname, mname)) {
python_release_GIL(t0);
pyErrorAndReturn(false, false);
}
PL_get_atom_chars(arg, &sa);
strcpy(s, sa);
s += strlen(s);
*s++ = '.';
s[0] = '\0';
} else if (!PL_get_nchars(mname, &len, &s,
CVT_ATOM | CVT_STRING| CVT_EXCEPTION | REP_UTF8)) {
python_release_GIL(t0);
pyErrorAndReturn(false, false);
} else {
break;
}
}
#if PY_MAJOR_VERSION < 3
pName = PyString_FromString(s0);
#else
pName = PyUnicode_FromString(s0);
#endif
python_release_GIL(t0);
if (pName == NULL) {
pyErrorAndReturn(false, false);
}
PyObject *pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule == NULL) {
#if EXTRA_MESSSAGES
if (PyErr_Occurred())
PyErr_Print();
PyErr_Clear();
#endif
{ pyErrorAndReturn(false, false); }
}
{
foreign_t rc = address_to_term(pModule, mod);
pyErrorAndReturn(rc, false);
}
}
static foreign_t python_to_rhs(term_t inp, term_t t) {
PyObject *pVal;
PyErr_Clear();
pVal = term_to_python(inp, true, NULL);
if (pVal == NULL)
pyErrorAndReturn(false, false);
pyErrorAndReturn(address_to_term(pVal, t), false);
}
// static PyThreadState *_saveP = NULL;
static bool _threaded = false;
/*
static YAP_Int
p_python_ensure(term_t ptr)
{
PyGILState_STATE _tState = PyGILState_Ensure();
pyErrorAndReturn( PL_unify_int64(ptr, _tState), false);
}
static YAP_Int
p_python_release(term_t ptr)
{
PyGILState_STATE _tState;
PL_get_int64( ptr, &_tState);
PyGILState_Release( _tState );
pyErrorAndReturn( true, false);
}
*/
int _locked = 0;
PyThreadState *tstate;
static YAP_Int p_python_threaded(void) {
PyErr_Clear();
// PyEval_ReleaseThread(tstate);
// _threaded = true;
// _locked = 0;
pyErrorAndReturn(true, false);
}
static PyGILState_STATE gstate;
term_t python_acquire_GIL(void) {
term_t curSlot = PL_new_term_ref();
if (!_threaded)
pyErrorAndReturn(curSlot, false);
// extern int Yap_do_low_level_trace;
// Yap_do_low_level_trace = 1;
// f[rintf( stderr, "++%d\n", ++_locked);
// if (_locked > 0) { _locked++ ; }
// else
gstate = PyGILState_Ensure();
pyErrorAndReturn(curSlot, false);
}
bool python_release_GIL(term_t curBlock) {
PyErr_Clear();
PL_reset_term_refs(curBlock);
if (_threaded) {
PyGILState_Release(gstate);
}
pyErrorAndReturn(true, false);
}
//: prolog: release python
install_t install_pypreds(void) {
PL_register_foreign("python_builtin_eval", 3, python_builtin_eval, 0);
PL_register_foreign("python_builtin", 1, python_builtin, 0);
PL_register_foreign("python_import", 2, python_import, 0);
PL_register_foreign("python_to_rhs", 2, python_to_rhs, 0);
PL_register_foreign("python_len", 2, python_len, 0);
PL_register_foreign("python_is", 2, python_is, 0);
PL_register_foreign("python_dir", 2, python_dir, 0);
PL_register_foreign("python_apply", 4, python_apply, 0);
PL_register_foreign("python_index", 3, python_index, 0);
PL_register_foreign("python_field", 3, python_field, 0);
PL_register_foreign("python_assign", 2, assign_python, 0);
PL_register_foreign("python_export", 2, python_export, 0);
PL_register_foreign("python_function", 1, python_function, 0);
PL_register_foreign("python_slice", 4, python_slice, 0);
PL_register_foreign("python_run_file", 1, python_run_file, 0);
PL_register_foreign("python_proc", 1, python_proc, 0);
PL_register_foreign("python_run_command", 1, python_run_command, 0);
PL_register_foreign("python_run_script", 2, python_run_script, 0);
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_threaded", 0, p_python_threaded, 0);
}

View File

@ -1,222 +0,0 @@
#include "py4yap.h"
#include <VFS.h>
atom_t ATOM_true, ATOM_false, ATOM_colon, ATOM_dot, ATOM_none, ATOM_t,
ATOM_comma, ATOM_builtin, ATOM_A, ATOM_V, ATOM_self, ATOM_nil, ATOM_brackets, ATOM_curly_brackets;
functor_t FUNCTOR_dollar1, FUNCTOR_abs1, FUNCTOR_all1, FUNCTOR_any1,
FUNCTOR_bin1, FUNCTOR_brackets1, FUNCTOR_comma2, FUNCTOR_dir1,
FUNCTOR_float1, FUNCTOR_int1, FUNCTOR_iter1, FUNCTOR_iter2, FUNCTOR_long1,
FUNCTOR_len1, FUNCTOR_curly1, FUNCTOR_ord1, FUNCTOR_range1, FUNCTOR_range2,
FUNCTOR_range3, FUNCTOR_sum1, FUNCTOR_pointer1, FUNCTOR_complex2,
FUNCTOR_plus2, FUNCTOR_sub2, FUNCTOR_mul2, FUNCTOR_div2, FUNCTOR_hat2,
FUNCTOR_colon2, FUNCTOR_comma2, FUNCTOR_equal2, FUNCTOR_sqbrackets2,
FUNCTOR_dot2, FUNCTOR_brackets1;
X_API PyObject *py_Builtin;
X_API PyObject *py_Yapex;
X_API PyObject *py_Sys;
PyObject *py_Context;
PyObject *py_ModDict;
VFS_t pystream;
static void *
py_open( const char *name, const char *io_mode) {
#if HAVE_STRCASESTR
if (strcasestr(name,"//python/")== name)
name += strlen("//python/");
#else
if (strstr(name,"//python/")== name)
name += strlen("//python/");
#endif
// we assume object is already open, so there is no need to open it.
PyObject *stream = string_to_python( name, true, NULL);
if (stream == Py_None)
return NULL;
return stream;
}
static bool
py_close(int sno) {
PyObject *s = YAP_foreign_stream(sno);
PyObject* fclose = PyObject_GetAttrString(s, "close");
PyObject* rc= PyObject_CallObject(fclose, NULL);
bool v = (rc == Py_True);
return v;
}
static PyObject * pyw; // buffer
static int pyw_kind;
PyObject * pyw_data;
static int
py_put(int sno, int ch) {
PyObject *s = YAP_foreign_stream(sno);
PyUnicode_WRITE( pyw_kind, pyw_data, 0, ch );
PyObject* fput = PyObject_GetAttrString(s, "write");
PyObject_CallFunctionObjArgs(fput, pyw, NULL);
return ch;
}
static int
py_get(int sno) {
PyObject *s = YAP_foreign_stream(sno);
PyObject* fget = PyObject_GetAttrString(s, "read");
PyObject *pyr = PyObject_CallFunctionObjArgs(fget, PyLong_FromLong(1), NULL);
return PyUnicode_READ_CHAR( pyr, 0) ;
}
static int64_t py_seek(int sno, int64_t where, int how) {
PyObject *s = YAP_foreign_stream(sno);
PyObject* fseek = PyObject_GetAttrString(s, "seek");
PyObject *pyr = PyObject_CallFunctionObjArgs(fseek, PyLong_FromLong(where), PyLong_FromLong(how), NULL);
return PyLong_AsLong(pyr);
}
static void
py_flush(int sno) {
PyObject *s = YAP_foreign_stream(sno);
PyObject* flush = PyObject_GetAttrString(s, "flush");
PyObject_CallFunction( flush, NULL);
}
static bool
init_python_stream(void)
{
pyw = PyUnicode_FromString("x");
pyw_kind = PyUnicode_KIND(pyw);
pyw_data = PyUnicode_DATA(pyw);
pystream.name = "python stream";
pystream.vflags = VFS_CAN_WRITE|VFS_CAN_EXEC| VFS_CAN_SEEK|VFS_HAS_PREFIX;
pystream.prefix = "//python/";
pystream.suffix = NULL;
pystream.open = py_open;
pystream.close = py_close;
pystream.get_char = py_get;
pystream.put_char = py_put;
pystream.flush = py_flush;
pystream.seek = py_seek;
pystream.next = GLOBAL_VFS;
GLOBAL_VFS = &pystream;
// NULL;
return true;
}
X_API PyObject *py_F2P;
extern X_API bool python_in_python;
static void add_modules(void) {
py_Main = PyImport_AddModule("__main__");
Py_INCREF(py_Main);
py_Sys = PyImport_AddModule("sys");
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");
if (py_Yapex)
Py_INCREF(py_Yapex);
//py_F2P = PyObject_GetAttrString(py_Yap, "globals");
py_F2P = NULL;
init_python_stream();
}
static void install_py_constants(void) {
FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
// FUNCTOR_boolop1 = PL_new_functor(PL_new_atom("@"), 1);
ATOM_A = PL_new_atom("A");
ATOM_V = PL_new_atom("V");
ATOM_builtin = PL_new_atom("__builtin__");
ATOM_comma = PL_new_atom(",");
ATOM_colon = PL_new_atom(":");
ATOM_true = PL_new_atom("true");
ATOM_false = PL_new_atom("false");
ATOM_dot = PL_new_atom(".");
ATOM_self = PL_new_atom("self");
ATOM_nil = PL_new_atom("[]");
ATOM_brackets = PL_new_atom("()");
ATOM_curly_brackets = PL_new_atom("{}");
FUNCTOR_abs1 = PL_new_functor(PL_new_atom("abs"), 1);
FUNCTOR_all1 = PL_new_functor(PL_new_atom("all"), 1);
FUNCTOR_any1 = PL_new_functor(PL_new_atom("any"), 1);
FUNCTOR_bin1 = PL_new_functor(PL_new_atom("bin"), 1);
FUNCTOR_ord1 = PL_new_functor(PL_new_atom("ord"), 1);
FUNCTOR_int1 = PL_new_functor(PL_new_atom("int"), 1);
FUNCTOR_long1 = PL_new_functor(PL_new_atom("long"), 1);
FUNCTOR_float1 = PL_new_functor(PL_new_atom("float"), 1);
FUNCTOR_curly1 = PL_new_functor(PL_new_atom("{}"), 1);
FUNCTOR_brackets1 = PL_new_functor(PL_new_atom("()"), 1);
FUNCTOR_dollar1 = PL_new_functor(PL_new_atom("$"), 1);
FUNCTOR_pointer1 = PL_new_functor(PL_new_atom("__obj__"), 1);
FUNCTOR_dir1 = PL_new_functor(PL_new_atom("dir"), 1);
FUNCTOR_iter1 = PL_new_functor(PL_new_atom("iter"), 1);
FUNCTOR_iter2 = PL_new_functor(PL_new_atom("iter"), 2);
FUNCTOR_len1 = PL_new_functor(PL_new_atom("len"), 1);
FUNCTOR_range1 = PL_new_functor(PL_new_atom("range"), 1);
FUNCTOR_range2 = PL_new_functor(PL_new_atom("range"), 2);
FUNCTOR_range3 = PL_new_functor(PL_new_atom("range"), 3);
FUNCTOR_sum1 = PL_new_functor(PL_new_atom("sum"), 1);
FUNCTOR_complex2 = PL_new_functor(PL_new_atom("i"), 2);
FUNCTOR_plus2 = PL_new_functor(PL_new_atom("+"), 2);
FUNCTOR_sub2 = PL_new_functor(PL_new_atom("-"), 2);
FUNCTOR_mul2 = PL_new_functor(PL_new_atom("*"), 2);
FUNCTOR_div2 = PL_new_functor(PL_new_atom("/"), 2);
FUNCTOR_hat2 = PL_new_functor(PL_new_atom("^"), 2);
FUNCTOR_colon2 = PL_new_functor(PL_new_atom(":"), 2);
FUNCTOR_comma2 = PL_new_functor(PL_new_atom(","), 2);
FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
FUNCTOR_sqbrackets2 = PL_new_functor(PL_new_atom("[]"), 2);
}
foreign_t end_python(void) {
if (!python_in_python)
Py_Finalize();
return true;
}
static bool libpython_initialized = 0;
X_API bool do_init_python(void) {
// char **argv;
if (libpython_initialized)
return true;
libpython_initialized = true;
// PyGILState_STATE gstate = PyGILState_Ensure();
term_t t = PL_new_term_ref();
if (!python_in_python)
Py_Initialize();
install_py_constants();
PL_reset_term_refs(t);
install_pl2pl();
// PyGILState_Release(gstate);
add_modules();
return true;
}
#ifndef _WIN32
O_API bool init_python_dll(void);
O_API bool init_python_dll(void) {
do_init_python();
install_pypreds();
return 1;
}
#endif