jupyter
This commit is contained in:
@@ -7,12 +7,14 @@ 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();
|
||||
// int lvl = push_text_stack();
|
||||
PyObject *type, *val;
|
||||
// PyErr_Fetch(&type, &val, NULL);
|
||||
// PyErr_Print();
|
||||
// Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python Error %s: %s",PyUnicode_AsUTF8(PyObject_Str(type)), PyUnicode_AsUTF8(PyObject_Str(val)));
|
||||
};
|
||||
// Yap_ThrowError__(file,code,line,0, SYSTEM_ERROR_RUNTIME_PYTHON ,"Python
|
||||
// Error %s: %s",PyUnicode_AsUTF8(PyObject_Str(type)),
|
||||
// PyUnicode_AsUTF8(PyObject_Str(val)));
|
||||
};
|
||||
|
||||
static foreign_t python_len(term_t tobj, term_t tf) {
|
||||
Py_ssize_t len;
|
||||
@@ -30,7 +32,6 @@ static foreign_t python_clear_errors(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static foreign_t python_dir(term_t tobj, term_t tf) {
|
||||
PyObject *dir;
|
||||
PyObject *o;
|
||||
@@ -115,7 +116,9 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
|
||||
p = term_to_python(parent, true, NULL, true);
|
||||
// Exp
|
||||
if (!pI || !p) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false); }
|
||||
@@ -143,14 +146,18 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
pF = term_to_python(tin, true, NULL, true);
|
||||
PyErr_Clear();
|
||||
if (pF == NULL) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
}
|
||||
if (PL_is_atom(targs)) {
|
||||
pArgs = NULL;
|
||||
} else {
|
||||
|
||||
if (!PL_get_name_arity(targs, &aname, &arity)) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
}
|
||||
if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) {
|
||||
/* ignore (_) */
|
||||
@@ -277,7 +284,7 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
Py_DECREF(pI);
|
||||
if (pOut == NULL) {
|
||||
PyErr_Print();
|
||||
{ pyErrorAndReturn(false); }
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
{
|
||||
foreign_t rc = address_to_term(pOut, out);
|
||||
@@ -306,10 +313,12 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
}
|
||||
Py_INCREF(pValue);
|
||||
{ pyErrorAndReturn(python_to_term(pValue, out) ); }
|
||||
{ pyErrorAndReturn(python_to_term(pValue, out)); }
|
||||
}
|
||||
if (!PL_get_name_arity(f, &name, &arity)) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (!s) {
|
||||
@@ -342,7 +351,9 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
Py_DECREF(pArgs);
|
||||
Py_DECREF(pF);
|
||||
if (pValue == NULL) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
}
|
||||
{ pyErrorAndReturn(python_to_term(pValue, out)); }
|
||||
}
|
||||
@@ -354,7 +365,9 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
int arity;
|
||||
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
} else {
|
||||
PyObject *p;
|
||||
|
||||
@@ -363,7 +376,9 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
p = term_to_python(parent, true, NULL, true);
|
||||
// Exp
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (arity == 1 && !strcmp(s, "()")) {
|
||||
@@ -371,12 +386,16 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
}
|
||||
if (!s || !p) {
|
||||
{ pyErrorAndReturn(false); }
|
||||
{
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
} else if ((pF = PyObject_GetAttrString(p, s)) == NULL) {
|
||||
PyErr_Clear();
|
||||
{ pyErrorAndReturn(false); }
|
||||
@@ -548,43 +567,43 @@ static int python_import(term_t mname, term_t mod) {
|
||||
|
||||
char s0[MAXPATHLEN], *s = s0;
|
||||
s[0] = '\0';
|
||||
const char*sn, *as;
|
||||
const char *sn, *as = NULL;
|
||||
Term t = Deref(ARG1), sm;
|
||||
if (IsApplTerm(t)) {
|
||||
Functor f = FunctorOfTerm(t);
|
||||
if (f != Yap_MkFunctor(Yap_LookupAtom("as"),2))
|
||||
if (f != FunctorAs)
|
||||
return false;
|
||||
do_as = true;
|
||||
sm = ArgOfTerm(2,t);
|
||||
sm = ArgOfTerm(2, t);
|
||||
if (IsAtomTerm(sm))
|
||||
as = RepAtom(AtomOfTerm(sm))->StrOfAE;
|
||||
else if (IsStringTerm(sm))
|
||||
as = StringOfTerm(sm);
|
||||
else
|
||||
return false;
|
||||
t = ArgOfTerm(1,t);
|
||||
t = ArgOfTerm(1, t);
|
||||
}
|
||||
while (IsPairTerm(t)) {
|
||||
Term ti = HeadOfTerm(t);
|
||||
Term ti = HeadOfTerm(t);
|
||||
t = TailOfTerm(t);
|
||||
if (IsAtomTerm(ti))
|
||||
if (IsAtomTerm(ti))
|
||||
sn = RepAtom(AtomOfTerm(ti))->StrOfAE;
|
||||
else if (IsStringTerm(ti))
|
||||
sn = StringOfTerm(ti);
|
||||
else
|
||||
return false;
|
||||
strcat(s,sn);
|
||||
strcat(s,".");
|
||||
strcat(s, sn);
|
||||
strcat(s, ".");
|
||||
}
|
||||
sm = t;
|
||||
if (IsAtomTerm(sm))
|
||||
sn = RepAtom(AtomOfTerm(sm))->StrOfAE;
|
||||
else if (IsStringTerm(sm))
|
||||
sn = StringOfTerm(sm);
|
||||
else
|
||||
return false;
|
||||
strcat(s,sn);
|
||||
term_t t0 = python_acquire_GIL();
|
||||
if (IsAtomTerm(sm))
|
||||
sn = RepAtom(AtomOfTerm(sm))->StrOfAE;
|
||||
else if (IsStringTerm(sm))
|
||||
sn = StringOfTerm(sm);
|
||||
else
|
||||
return false;
|
||||
strcat(s, sn);
|
||||
term_t t0 = python_acquire_GIL();
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
pName = PyString_FromString(s0);
|
||||
#else
|
||||
@@ -599,16 +618,17 @@ static int python_import(term_t mname, term_t mod) {
|
||||
|
||||
Py_XDECREF(pName);
|
||||
if (pModule == NULL) {
|
||||
python_release_GIL(t0);
|
||||
python_release_GIL(t0);
|
||||
|
||||
pyErrorAndReturn(false);
|
||||
}
|
||||
{
|
||||
foreign_t rc = address_to_term(pModule, mod);
|
||||
|
||||
if (do_as && PyObject_SetAttrString(py_Main, sn, pModule) <0)
|
||||
return false;
|
||||
python_release_GIL(t0);
|
||||
if (do_as) {
|
||||
PyObject_SetAttrString(py_Main, as, pModule);
|
||||
}
|
||||
python_release_GIL(t0);
|
||||
pyErrorAndReturn(rc);
|
||||
}
|
||||
}
|
||||
@@ -674,26 +694,25 @@ term_t python_acquire_GIL(void) {
|
||||
}
|
||||
|
||||
bool python_release_GIL(term_t curBlock) {
|
||||
int gstateix;
|
||||
gstatei--;
|
||||
PL_get_integer(curBlock, &gstateix);
|
||||
PL_reset_term_refs(curBlock);
|
||||
if (gstatei != gstateix) {
|
||||
if (gstateix > gstatei) {
|
||||
fprintf(stderr, "gstateix(%d) > gstatei(%d)\n", gstateix, gstatei);
|
||||
return false;
|
||||
} else {
|
||||
fprintf(stderr, "gstateix(%d) < gstatei(%d)\n", gstateix, gstatei);
|
||||
return false;
|
||||
}
|
||||
int gstateix;
|
||||
gstatei--;
|
||||
PL_get_integer(curBlock, &gstateix);
|
||||
PL_reset_term_refs(curBlock);
|
||||
if (gstatei != gstateix) {
|
||||
if (gstateix > gstatei) {
|
||||
fprintf(stderr, "gstateix(%d) > gstatei(%d)\n", gstateix, gstatei);
|
||||
return false;
|
||||
} else {
|
||||
fprintf(stderr, "gstateix(%d) < gstatei(%d)\n", gstateix, gstatei);
|
||||
return false;
|
||||
}
|
||||
if (_threaded) {
|
||||
}
|
||||
if (_threaded) {
|
||||
PyGILState_Release(gstates[gstatei]);
|
||||
}
|
||||
pyErrorAndReturn(true);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
Reference in New Issue
Block a user