error handling

This commit is contained in:
Vitor Santos Costa 2018-05-21 14:45:24 +01:00
parent 6e9882c369
commit b7a97a5b19
5 changed files with 168 additions and 151 deletions

View File

@ -34,6 +34,7 @@ X_API bool do_init_python(void);
YAPPredicate::YAPPredicate(Term &t, Term &tmod, CELL *&ts, const char *pname) {
Term t0 = t;
ap = nullptr;
Yap_DebugPlWriteln(t);
restart:
if (IsVarTerm(t)) {
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t0, pname);
@ -49,7 +50,8 @@ restart:
} else if (IsApplTerm(t)) {
Functor fun = FunctorOfTerm(t);
if (IsExtensionFunctor(fun)) {
throw YAPError( SOURCE(), TYPE_ERROR_CALLABLE, Yap_PredicateIndicator(t, tmod), pname);
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE,
Yap_PredicateIndicator(t, tmod), pname);
}
if (fun == FunctorModule) {
tmod = ArgOfTerm(1, t);
@ -319,7 +321,6 @@ std::vector<Term> YAPPairTerm::listToArray() {
return o;
}
YAP_tag_t YAPTerm::tag() {
Term tt = gt();
if (IsVarTerm(tt)) {
@ -487,7 +488,8 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
if (LOCAL_CommittedError != nullptr) {
std::cerr << "Exception received by " << __func__ << "( "
<< YAPError( LOCAL_CommittedError).text() << ").\n Forwarded...\n\n";
<< YAPError(LOCAL_CommittedError).text()
<< ").\n Forwarded...\n\n";
// Yap_PopTermFromDB(info->errorTerm);
// throw throw YAPError( SOURCE(), );
}
@ -508,8 +510,10 @@ bool YAPEngine::mgoal(Term t, Term tmod) {
try {
if (IsStringTerm(tmod))
tmod = MkAtomTerm(Yap_LookupAtom(StringOfTerm(tmod)));
PredEntry *ap = (new YAPPredicate(t, tmod, ts, "C++"))->ap;
if (ap == nullptr || ap->OpcodeOfPred == UNDEF_OPCODE) {
YAPPredicate *p = new YAPPredicate(t, tmod, ts, "C++");
PredEntry *ap = nullptr;
if (p == nullptr || (ap = p->ap) == nullptr ||
ap->OpcodeOfPred == UNDEF_OPCODE) {
ap = rewriteUndefEngineQuery(ap, t, tmod);
}
if (IsApplTerm(t))
@ -538,6 +542,10 @@ bool YAPEngine::mgoal(Term t, Term tmod) {
}
{
YAP_LeaveGoal(result, &q);
if (LOCAL_CommittedError != nullptr &&
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
throw YAPError(LOCAL_CommittedError);
}
// PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS();
return result;
@ -546,7 +554,13 @@ bool YAPEngine::mgoal(Term t, Term tmod) {
if (LOCAL_CommittedError != nullptr &&
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
std::cerr << "Exception received by " << __func__ << "( "
<< YAPError( LOCAL_CommittedError).text() << ").\n Forwarded...\n\n";
<< YAPError(LOCAL_CommittedError).text()
<< ").\n Forwarded...\n\n";
// free(LOCAL_CommittedError);
LOCAL_CommittedError->errorNo = YAP_NO_ERROR;
LOCAL_ActiveError->errorNo = YAP_NO_ERROR;
LOCAL_CommittedError = nullptr;
return false;
}
}
}
@ -606,7 +620,8 @@ Term YAPEngine::fun(Term t) {
bool result = (bool)YAP_EnterGoal(ap, nullptr, &q);
if (LOCAL_CommittedError != nullptr) {
std::cerr << "Exception received by " << __func__ << "( "
<< YAPError( LOCAL_CommittedError).text() << ").\n Forwarded...\n\n";
<< YAPError(LOCAL_CommittedError).text()
<< ").\n Forwarded...\n\n";
// Yap_PopTermFromDB(info->errorTerm);
// throw throw YAPError( SOURCE(), );
}
@ -912,8 +927,8 @@ PredEntry *YAPPredicate::getPred(YAPTerm &tt, CELL *&outp) {
Term m = Yap_CurrentModule(), t = tt.term();
t = Yap_StripModule(t, &m);
std::cerr << "Exception received by " << __func__ << "( "
<< tt.text() << ").\n Forwarded...\n\n";
std::cerr << "Exception received by " << __func__ << "( " << tt.text()
<< ").\n Forwarded...\n\n";
if (IsVarTerm(t) || IsNumTerm(t)) {
if (IsVarTerm(t))
@ -1002,7 +1017,10 @@ void *YAPPrologPredicate::retractClause(YAPTerm skeleton, bool all) {
std::string YAPError::text() {
char buf[256];
std::string s = "";
return "Error";
#if 0
std::stringstream s;
s << "";
if (info->errorNo == YAP_NO_ERROR)
return 0;
if (info->errorFunction) {
@ -1012,21 +1030,19 @@ std::string YAPError::text() {
s += buf;
s += ":0 in C-code";
}
return s;
if (info->prologPredLine) {
s += "\n";
s += info->prologPredFile;
s += ":";
sprintf(buf, "%ld", (long int)info->prologPredLine);
s += buf; // std::to_string(info->prologPredLine) ;
s << info->prologPredLine;
// YAPIntegerTerm(info->prologPredLine).text();
s += ":0 ";
s += info->prologPredModule;
s += ":";
s += (info->prologPredName);
s += "/";
sprintf(buf, "%ld", (long int)info->prologPredArity);
s += // std::to_string(info->prologPredArity);
buf;
s << info->prologPredArity;
}
s += " error ";
if (info->classAsText == nullptr)
@ -1041,6 +1057,7 @@ std::string YAPError::text() {
s += ".\n";
// printf("%s\n", s.c_str());
return s.c_str();
#endif
}
void YAPEngine::reSet() {
@ -1063,7 +1080,6 @@ void YAPEngine::reSet() {
RECOVER_MACHINE_REGS();
}
Term YAPEngine::top_level(std::string s) {
/// parse string s and make term with var names
/// available.

View File

@ -7,20 +7,5 @@
YAP includes a number of extensions over the original Prolog
language. Next, we discuss how to use the most important ones.
+ @ref Rational_Trees
+ @ref AttributedVariables
+ @ref DepthLimited
+ @ref Tabling
+ @ref Threads
+ @ref Profiling
+ @ref YAPArrays
+ @ref Parallelism
@}

View File

@ -28,7 +28,8 @@ static char SccsId[] = "%W% %G%";
*/
/*
* This file includes the definition of a miscellania of standard predicates *
*for yap refering to: Files and GLOBAL_Streams, Simple Input/Output,
*for yap refering to: Files and GLOBAL_1588
*ams, Simple Input/Output,
*
*/
@ -1584,7 +1585,7 @@ int Yap_OpenStream(Term tin, const char *io_mode, Term user_name,
st->file = fopen(fname, io_mode);
}
if (!st->file) {
fprintf(stderr, "trying %s\n", fname);
PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s", fname);
/* extract BACK info passed through the stream descriptor */
return -1;
@ -1632,12 +1633,10 @@ int Yap_OpenStream(Term tin, const char *io_mode, Term user_name,
if (!strchr(io_mode, 'b') && binary_file(fname)) {
UNLOCK(st->streamlock);
if (errno == ENOENT && !strchr(io_mode, 'r')) {
PlIOError(EXISTENCE_ERROR_SOURCE_SINK,
tin, "%s: %s", fname,
PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s: %s", fname,
strerror(errno));
} else {
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK,
tin, "%s: %s", fname,
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, tin, "%s: %s", fname,
strerror(errno));
}
}

View File

@ -7,8 +7,7 @@
YAP_Term TermErrStream, TermOutStream;
static int py_put(int sno, int ch)
{
static int py_put(int sno, int ch) {
// PyObject *pyw; // buffer
// int pyw_kind;
// PyObject *pyw_data;
@ -33,8 +32,7 @@ static int py_put(int sno, int ch)
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
PyUnicode_FromString(s), NULL);
python_release_GIL(g0);
if ((err = PyErr_Occurred()))
{
if ((err = PyErr_Occurred())) {
PyErr_SetString(
err,
"Error in put\n"); // %s:%s:%d!\n", __FILE__, __FUNCTION__, __LINE__);
@ -43,7 +41,8 @@ static int py_put(int sno, int ch)
}
VFS_t pystream;
static void *py_open(VFS_t *me, const char *name, const char *io_mode, int sno) {
static void *py_open(VFS_t *me, const char *name, const char *io_mode,
int sno) {
#if HAVE_STRCASESTR
if (strcasestr(name, "/python/") == name)
name += strlen("/python/");
@ -67,6 +66,9 @@ VFS_t pystream;
st->user_name = YAP_MkAtomTerm(st->name);
}
// we assume object is already open, so there is no need to open it.
if (PyCallable_Check(pystream))
st->u.private_data = PyObject_Call(pystream, PyTuple_New(0), NULL);
else
st->u.private_data = pystream;
st->vfs = me;
python_release_GIL(ctk);
@ -75,8 +77,7 @@ VFS_t pystream;
static bool py_close(int sno) {
StreamDesc *st = YAP_RepStreamFromId(sno);
if (strcmp(st->name,"sys.stdout") &&
strcmp(st->name,"sys.stderr")) {
if (strcmp(st->name, "sys.stdout") && strcmp(st->name, "sys.stderr")) {
Py_XDECREF(st->u.private_data);
}
st->u.private_data = NULL;
@ -93,14 +94,15 @@ static bool getLine(int inp) {
PyObject *prompt = PyUnicode_FromString("?- "),
*msg = PyUnicode_FromString(" **input** ");
/* window of vulnerability opened */
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(rl_instream->u.private_data,msg,prompt,NULL));
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(
rl_instream->u.private_data, msg, prompt, NULL));
python_release_GIL(ctk);
rl_instream->u.irl.ptr = rl_instream->u.irl.buf = (const unsigned char*)myrl_line;
rl_instream->u.irl.ptr = rl_instream->u.irl.buf =
(const unsigned char *)myrl_line;
myrl_line = NULL;
return true;
}
static int py_getc(int sno) {
StreamDesc *s = YAP_RepStreamFromId(sno);
int ch;
@ -151,7 +153,6 @@ static int py_peek(int sno) {
return ch;
}
static int64_t py_seek(int sno, int64_t where, int how) {
StreamDesc *g0 = YAP_RepStreamFromId(sno);
term_t s0 = python_acquire_GIL();

View File

@ -20,7 +20,6 @@
% ]
%% ).
:- [library(hacks)].
:- reexport(library(yapi)).
:- use_module(library(lists)).
:- use_module(library(maplist)).
@ -274,4 +273,21 @@ close_events( Self ) :-
fail.
close_events( _ ).
:- if( current_prolog_flag(apple, true) ).
:- putenv( 'LC_ALL', 'en_us:UTF-8').
plot_inline :-
X := self.inline_plotting,
nb_setval(inline, X ),
X = true,
!,
:= (
import( matplotlib ),
matplotlib.use( `nbagg` )
).
:- endif.
%:- ( start_low_level_trace ).