error handling
This commit is contained in:
parent
6e9882c369
commit
b7a97a5b19
92
CXX/yapi.cpp
92
CXX/yapi.cpp
@ -34,9 +34,10 @@ X_API bool do_init_python(void);
|
|||||||
YAPPredicate::YAPPredicate(Term &t, Term &tmod, CELL *&ts, const char *pname) {
|
YAPPredicate::YAPPredicate(Term &t, Term &tmod, CELL *&ts, const char *pname) {
|
||||||
Term t0 = t;
|
Term t0 = t;
|
||||||
ap = nullptr;
|
ap = nullptr;
|
||||||
|
Yap_DebugPlWriteln(t);
|
||||||
restart:
|
restart:
|
||||||
if (IsVarTerm(t)) {
|
if (IsVarTerm(t)) {
|
||||||
throw YAPError( SOURCE(), INSTANTIATION_ERROR, t0, pname);
|
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t0, pname);
|
||||||
} else if (IsAtomTerm(t)) {
|
} else if (IsAtomTerm(t)) {
|
||||||
ap = RepPredProp(Yap_GetPredPropByAtom(AtomOfTerm(t), tmod));
|
ap = RepPredProp(Yap_GetPredPropByAtom(AtomOfTerm(t), tmod));
|
||||||
ts = nullptr;
|
ts = nullptr;
|
||||||
@ -49,15 +50,16 @@ restart:
|
|||||||
} else if (IsApplTerm(t)) {
|
} else if (IsApplTerm(t)) {
|
||||||
Functor fun = FunctorOfTerm(t);
|
Functor fun = FunctorOfTerm(t);
|
||||||
if (IsExtensionFunctor(fun)) {
|
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) {
|
if (fun == FunctorModule) {
|
||||||
tmod = ArgOfTerm(1, t);
|
tmod = ArgOfTerm(1, t);
|
||||||
if (IsVarTerm(tmod)) {
|
if (IsVarTerm(tmod)) {
|
||||||
throw YAPError( SOURCE(), INSTANTIATION_ERROR, t0, pname);
|
throw YAPError(SOURCE(), INSTANTIATION_ERROR, t0, pname);
|
||||||
}
|
}
|
||||||
if (!IsAtomTerm(tmod)) {
|
if (!IsAtomTerm(tmod)) {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_ATOM, t0, pname);
|
throw YAPError(SOURCE(), TYPE_ERROR_ATOM, t0, pname);
|
||||||
}
|
}
|
||||||
t = ArgOfTerm(2, t);
|
t = ArgOfTerm(2, t);
|
||||||
goto restart;
|
goto restart;
|
||||||
@ -65,18 +67,18 @@ restart:
|
|||||||
ap = RepPredProp(Yap_GetPredPropByFunc(fun, tmod));
|
ap = RepPredProp(Yap_GetPredPropByFunc(fun, tmod));
|
||||||
ts = RepAppl(t) + 1;
|
ts = RepAppl(t) + 1;
|
||||||
} else {
|
} else {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_CALLABLE, t0, pname);
|
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t0, pname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Term YAPTerm::getArg(arity_t i) {
|
Term YAPTerm::getArg(arity_t i) {
|
||||||
BACKUP_MACHINE_REGS();
|
BACKUP_MACHINE_REGS();
|
||||||
Term tf = 0;
|
Term tf = 0;
|
||||||
Term t0 = gt();
|
Term t0 = gt();
|
||||||
|
|
||||||
if (IsApplTerm(t0)) {
|
if (IsApplTerm(t0)) {
|
||||||
if (i > ArityOfFunctor(FunctorOfTerm(t0)))
|
if (i > ArityOfFunctor(FunctorOfTerm(t0)))
|
||||||
throw YAPError( SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
|
throw YAPError(SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
|
||||||
tf = (ArgOfTerm(i, t0));
|
tf = (ArgOfTerm(i, t0));
|
||||||
} else if (IsPairTerm(t0)) {
|
} else if (IsPairTerm(t0)) {
|
||||||
if (i == 1)
|
if (i == 1)
|
||||||
@ -84,13 +86,13 @@ restart:
|
|||||||
else if (i == 2)
|
else if (i == 2)
|
||||||
tf = (TailOfTerm(t0));
|
tf = (TailOfTerm(t0));
|
||||||
else
|
else
|
||||||
throw YAPError( SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
|
throw YAPError(SOURCE(), DOMAIN_ERROR_OUT_OF_RANGE, t0, "t0.getArg()");
|
||||||
} else {
|
} else {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_COMPOUND, t0, "t0.getArg()");
|
throw YAPError(SOURCE(), TYPE_ERROR_COMPOUND, t0, "t0.getArg()");
|
||||||
}
|
}
|
||||||
RECOVER_MACHINE_REGS();
|
RECOVER_MACHINE_REGS();
|
||||||
return tf;
|
return tf;
|
||||||
}
|
}
|
||||||
|
|
||||||
YAPAtomTerm::YAPAtomTerm(char s[]) { // build string
|
YAPAtomTerm::YAPAtomTerm(char s[]) { // build string
|
||||||
BACKUP_H();
|
BACKUP_H();
|
||||||
@ -265,7 +267,7 @@ Term &YAPTerm::operator[](arity_t i) {
|
|||||||
tf = RepPair(t0) + 1;
|
tf = RepPair(t0) + 1;
|
||||||
RECOVER_MACHINE_REGS();
|
RECOVER_MACHINE_REGS();
|
||||||
} else {
|
} else {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_COMPOUND, t0, "");
|
throw YAPError(SOURCE(), TYPE_ERROR_COMPOUND, t0, "");
|
||||||
}
|
}
|
||||||
RECOVER_MACHINE_REGS();
|
RECOVER_MACHINE_REGS();
|
||||||
return *tf;
|
return *tf;
|
||||||
@ -307,7 +309,7 @@ std::vector<Term> YAPPairTerm::listToArray() {
|
|||||||
Term t1 = gt();
|
Term t1 = gt();
|
||||||
Int l = Yap_SkipList(&t1, &tailp);
|
Int l = Yap_SkipList(&t1, &tailp);
|
||||||
if (l < 0) {
|
if (l < 0) {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_LIST, (t), nullptr);
|
throw YAPError(SOURCE(), TYPE_ERROR_LIST, (t), nullptr);
|
||||||
}
|
}
|
||||||
std::vector<Term> o = std::vector<Term>(l);
|
std::vector<Term> o = std::vector<Term>(l);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -317,8 +319,7 @@ std::vector<Term> YAPPairTerm::listToArray() {
|
|||||||
t = TailOfTerm(t);
|
t = TailOfTerm(t);
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
YAP_tag_t YAPTerm::tag() {
|
YAP_tag_t YAPTerm::tag() {
|
||||||
Term tt = gt();
|
Term tt = gt();
|
||||||
@ -381,8 +382,8 @@ Term YAPListTerm::cdr() {
|
|||||||
else if (to == TermNil)
|
else if (to == TermNil)
|
||||||
return TermNil;
|
return TermNil;
|
||||||
/* error */
|
/* error */
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_LIST, to, "");
|
throw YAPError(SOURCE(), TYPE_ERROR_LIST, to, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
Term YAPListTerm::dup() {
|
Term YAPListTerm::dup() {
|
||||||
yhandle_t tn;
|
yhandle_t tn;
|
||||||
@ -431,7 +432,7 @@ Term YAPListTerm::car() {
|
|||||||
if (IsPairTerm(to))
|
if (IsPairTerm(to))
|
||||||
return (HeadOfTerm(to));
|
return (HeadOfTerm(to));
|
||||||
else {
|
else {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_LIST, to, "");
|
throw YAPError(SOURCE(), TYPE_ERROR_LIST, to, "");
|
||||||
return TermUnique;
|
return TermUnique;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -487,7 +488,8 @@ bool YAPEngine::call(YAPPredicate ap, YAPTerm ts[]) {
|
|||||||
|
|
||||||
if (LOCAL_CommittedError != nullptr) {
|
if (LOCAL_CommittedError != nullptr) {
|
||||||
std::cerr << "Exception received by " << __func__ << "( "
|
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);
|
// Yap_PopTermFromDB(info->errorTerm);
|
||||||
// throw throw YAPError( SOURCE(), );
|
// throw throw YAPError( SOURCE(), );
|
||||||
}
|
}
|
||||||
@ -508,8 +510,10 @@ bool YAPEngine::mgoal(Term t, Term tmod) {
|
|||||||
try {
|
try {
|
||||||
if (IsStringTerm(tmod))
|
if (IsStringTerm(tmod))
|
||||||
tmod = MkAtomTerm(Yap_LookupAtom(StringOfTerm(tmod)));
|
tmod = MkAtomTerm(Yap_LookupAtom(StringOfTerm(tmod)));
|
||||||
PredEntry *ap = (new YAPPredicate(t, tmod, ts, "C++"))->ap;
|
YAPPredicate *p = new YAPPredicate(t, tmod, ts, "C++");
|
||||||
if (ap == nullptr || ap->OpcodeOfPred == UNDEF_OPCODE) {
|
PredEntry *ap = nullptr;
|
||||||
|
if (p == nullptr || (ap = p->ap) == nullptr ||
|
||||||
|
ap->OpcodeOfPred == UNDEF_OPCODE) {
|
||||||
ap = rewriteUndefEngineQuery(ap, t, tmod);
|
ap = rewriteUndefEngineQuery(ap, t, tmod);
|
||||||
}
|
}
|
||||||
if (IsApplTerm(t))
|
if (IsApplTerm(t))
|
||||||
@ -534,19 +538,29 @@ bool YAPEngine::mgoal(Term t, Term tmod) {
|
|||||||
result = (bool)YAP_EnterGoal(ap, nullptr, &q);
|
result = (bool)YAP_EnterGoal(ap, nullptr, &q);
|
||||||
if (LOCAL_CommittedError != nullptr &&
|
if (LOCAL_CommittedError != nullptr &&
|
||||||
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
|
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
|
||||||
throw YAPError( LOCAL_CommittedError);
|
throw YAPError(LOCAL_CommittedError);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
YAP_LeaveGoal(result, &q);
|
YAP_LeaveGoal(result, &q);
|
||||||
|
if (LOCAL_CommittedError != nullptr &&
|
||||||
|
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
|
||||||
|
throw YAPError(LOCAL_CommittedError);
|
||||||
|
}
|
||||||
// PyEval_RestoreThread(_save);
|
// PyEval_RestoreThread(_save);
|
||||||
RECOVER_MACHINE_REGS();
|
RECOVER_MACHINE_REGS();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
} catch ( ... ) {
|
} catch (...) {
|
||||||
if (LOCAL_CommittedError != nullptr &&
|
if (LOCAL_CommittedError != nullptr &&
|
||||||
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
|
LOCAL_CommittedError->errorNo != YAP_NO_ERROR) {
|
||||||
std::cerr << "Exception received by " << __func__ << "( "
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -585,7 +599,7 @@ Term YAPEngine::fun(Term t) {
|
|||||||
name = AtomDot;
|
name = AtomDot;
|
||||||
f = FunctorDot;
|
f = FunctorDot;
|
||||||
} else {
|
} else {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
XREGS[arity + 1] = MkVarTerm();
|
XREGS[arity + 1] = MkVarTerm();
|
||||||
@ -606,7 +620,8 @@ Term YAPEngine::fun(Term t) {
|
|||||||
bool result = (bool)YAP_EnterGoal(ap, nullptr, &q);
|
bool result = (bool)YAP_EnterGoal(ap, nullptr, &q);
|
||||||
if (LOCAL_CommittedError != nullptr) {
|
if (LOCAL_CommittedError != nullptr) {
|
||||||
std::cerr << "Exception received by " << __func__ << "( "
|
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);
|
// Yap_PopTermFromDB(info->errorTerm);
|
||||||
// throw throw YAPError( SOURCE(), );
|
// throw throw YAPError( SOURCE(), );
|
||||||
}
|
}
|
||||||
@ -912,14 +927,14 @@ PredEntry *YAPPredicate::getPred(YAPTerm &tt, CELL *&outp) {
|
|||||||
Term m = Yap_CurrentModule(), t = tt.term();
|
Term m = Yap_CurrentModule(), t = tt.term();
|
||||||
t = Yap_StripModule(t, &m);
|
t = Yap_StripModule(t, &m);
|
||||||
|
|
||||||
std::cerr << "Exception received by " << __func__ << "( "
|
std::cerr << "Exception received by " << __func__ << "( " << tt.text()
|
||||||
<< tt.text() << ").\n Forwarded...\n\n";
|
<< ").\n Forwarded...\n\n";
|
||||||
|
|
||||||
if (IsVarTerm(t) || IsNumTerm(t)) {
|
if (IsVarTerm(t) || IsNumTerm(t)) {
|
||||||
if (IsVarTerm(t))
|
if (IsVarTerm(t))
|
||||||
throw YAPError( SOURCE(), INSTANTIATION_ERROR, tt.term(), 0);
|
throw YAPError(SOURCE(), INSTANTIATION_ERROR, tt.term(), 0);
|
||||||
else if (IsNumTerm(t))
|
else if (IsNumTerm(t))
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_CALLABLE, tt.term(), 0);
|
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, tt.term(), 0);
|
||||||
}
|
}
|
||||||
tt.put(t);
|
tt.put(t);
|
||||||
if (IsAtomTerm(t)) {
|
if (IsAtomTerm(t)) {
|
||||||
@ -938,7 +953,7 @@ PredEntry *YAPPredicate::getPred(YAPTerm &tt, CELL *&outp) {
|
|||||||
}
|
}
|
||||||
Functor f = FunctorOfTerm(t);
|
Functor f = FunctorOfTerm(t);
|
||||||
if (IsExtensionFunctor(f)) {
|
if (IsExtensionFunctor(f)) {
|
||||||
throw YAPError( SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
throw YAPError(SOURCE(), TYPE_ERROR_CALLABLE, t, 0);
|
||||||
} else {
|
} else {
|
||||||
ap = RepPredProp(PredPropByFunc(f, m));
|
ap = RepPredProp(PredPropByFunc(f, m));
|
||||||
outp = RepAppl(t) + 1;
|
outp = RepAppl(t) + 1;
|
||||||
@ -1002,7 +1017,10 @@ void *YAPPrologPredicate::retractClause(YAPTerm skeleton, bool all) {
|
|||||||
|
|
||||||
std::string YAPError::text() {
|
std::string YAPError::text() {
|
||||||
char buf[256];
|
char buf[256];
|
||||||
std::string s = "";
|
return "Error";
|
||||||
|
#if 0
|
||||||
|
std::stringstream s;
|
||||||
|
s << "";
|
||||||
if (info->errorNo == YAP_NO_ERROR)
|
if (info->errorNo == YAP_NO_ERROR)
|
||||||
return 0;
|
return 0;
|
||||||
if (info->errorFunction) {
|
if (info->errorFunction) {
|
||||||
@ -1012,21 +1030,19 @@ std::string YAPError::text() {
|
|||||||
s += buf;
|
s += buf;
|
||||||
s += ":0 in C-code";
|
s += ":0 in C-code";
|
||||||
}
|
}
|
||||||
|
return s;
|
||||||
if (info->prologPredLine) {
|
if (info->prologPredLine) {
|
||||||
s += "\n";
|
s += "\n";
|
||||||
s += info->prologPredFile;
|
s += info->prologPredFile;
|
||||||
s += ":";
|
s += ":";
|
||||||
sprintf(buf, "%ld", (long int)info->prologPredLine);
|
s << info->prologPredLine;
|
||||||
s += buf; // std::to_string(info->prologPredLine) ;
|
|
||||||
// YAPIntegerTerm(info->prologPredLine).text();
|
// YAPIntegerTerm(info->prologPredLine).text();
|
||||||
s += ":0 ";
|
s += ":0 ";
|
||||||
s += info->prologPredModule;
|
s += info->prologPredModule;
|
||||||
s += ":";
|
s += ":";
|
||||||
s += (info->prologPredName);
|
s += (info->prologPredName);
|
||||||
s += "/";
|
s += "/";
|
||||||
sprintf(buf, "%ld", (long int)info->prologPredArity);
|
s << info->prologPredArity;
|
||||||
s += // std::to_string(info->prologPredArity);
|
|
||||||
buf;
|
|
||||||
}
|
}
|
||||||
s += " error ";
|
s += " error ";
|
||||||
if (info->classAsText == nullptr)
|
if (info->classAsText == nullptr)
|
||||||
@ -1041,6 +1057,7 @@ std::string YAPError::text() {
|
|||||||
s += ".\n";
|
s += ".\n";
|
||||||
// printf("%s\n", s.c_str());
|
// printf("%s\n", s.c_str());
|
||||||
return s.c_str();
|
return s.c_str();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void YAPEngine::reSet() {
|
void YAPEngine::reSet() {
|
||||||
@ -1063,7 +1080,6 @@ void YAPEngine::reSet() {
|
|||||||
RECOVER_MACHINE_REGS();
|
RECOVER_MACHINE_REGS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Term YAPEngine::top_level(std::string s) {
|
Term YAPEngine::top_level(std::string s) {
|
||||||
/// parse string s and make term with var names
|
/// parse string s and make term with var names
|
||||||
/// available.
|
/// available.
|
||||||
@ -1072,7 +1088,7 @@ Term YAPEngine::top_level(std::string s) {
|
|||||||
ARG2 = tp;
|
ARG2 = tp;
|
||||||
ARG3 = MkVarTerm();
|
ARG3 = MkVarTerm();
|
||||||
if (ARG1 == 0)
|
if (ARG1 == 0)
|
||||||
throw YAPError( SOURCE(), SYNTAX_ERROR, ARG1, "in input query");
|
throw YAPError(SOURCE(), SYNTAX_ERROR, ARG1, "in input query");
|
||||||
YAPPredicate p = YAPPredicate(YAP_TopGoal());
|
YAPPredicate p = YAPPredicate(YAP_TopGoal());
|
||||||
YAPQuery *Q = new YAPQuery(p, 0);
|
YAPQuery *Q = new YAPQuery(p, 0);
|
||||||
Term ts[2];
|
Term ts[2];
|
||||||
|
@ -7,20 +7,5 @@
|
|||||||
YAP includes a number of extensions over the original Prolog
|
YAP includes a number of extensions over the original Prolog
|
||||||
language. Next, we discuss how to use the most important ones.
|
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
|
|
||||||
|
|
||||||
@}
|
@}
|
||||||
|
11
os/iopreds.c
11
os/iopreds.c
@ -28,7 +28,8 @@ static char SccsId[] = "%W% %G%";
|
|||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
* This file includes the definition of a miscellania of standard predicates *
|
* 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);
|
st->file = fopen(fname, io_mode);
|
||||||
}
|
}
|
||||||
if (!st->file) {
|
if (!st->file) {
|
||||||
|
fprintf(stderr, "trying %s\n", fname);
|
||||||
PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s", fname);
|
PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s", fname);
|
||||||
/* extract BACK info passed through the stream descriptor */
|
/* extract BACK info passed through the stream descriptor */
|
||||||
return -1;
|
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)) {
|
if (!strchr(io_mode, 'b') && binary_file(fname)) {
|
||||||
UNLOCK(st->streamlock);
|
UNLOCK(st->streamlock);
|
||||||
if (errno == ENOENT && !strchr(io_mode, 'r')) {
|
if (errno == ENOENT && !strchr(io_mode, 'r')) {
|
||||||
PlIOError(EXISTENCE_ERROR_SOURCE_SINK,
|
PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s: %s", fname,
|
||||||
tin, "%s: %s", fname,
|
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
} else {
|
} else {
|
||||||
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK,
|
PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, tin, "%s: %s", fname,
|
||||||
tin, "%s: %s", fname,
|
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,7 @@
|
|||||||
|
|
||||||
YAP_Term TermErrStream, TermOutStream;
|
YAP_Term TermErrStream, TermOutStream;
|
||||||
|
|
||||||
static int py_put(int sno, int ch)
|
static int py_put(int sno, int ch) {
|
||||||
{
|
|
||||||
// PyObject *pyw; // buffer
|
// PyObject *pyw; // buffer
|
||||||
// int pyw_kind;
|
// int pyw_kind;
|
||||||
// PyObject *pyw_data;
|
// PyObject *pyw_data;
|
||||||
@ -33,8 +32,7 @@ static int py_put(int sno, int ch)
|
|||||||
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
|
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
|
||||||
PyUnicode_FromString(s), NULL);
|
PyUnicode_FromString(s), NULL);
|
||||||
python_release_GIL(g0);
|
python_release_GIL(g0);
|
||||||
if ((err = PyErr_Occurred()))
|
if ((err = PyErr_Occurred())) {
|
||||||
{
|
|
||||||
PyErr_SetString(
|
PyErr_SetString(
|
||||||
err,
|
err,
|
||||||
"Error in put\n"); // %s:%s:%d!\n", __FILE__, __FUNCTION__, __LINE__);
|
"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;
|
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 HAVE_STRCASESTR
|
||||||
if (strcasestr(name, "/python/") == name)
|
if (strcasestr(name, "/python/") == name)
|
||||||
name += strlen("/python/");
|
name += strlen("/python/");
|
||||||
@ -59,14 +58,17 @@ VFS_t pystream;
|
|||||||
}
|
}
|
||||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||||
st->name = YAP_LookupAtom(name);
|
st->name = YAP_LookupAtom(name);
|
||||||
if (strcmp(name,"sys.stdout") == 0) {
|
if (strcmp(name, "sys.stdout") == 0) {
|
||||||
st->user_name = TermOutStream;
|
st->user_name = TermOutStream;
|
||||||
} else if(strcmp(name,"sys.stderr") == 0) {
|
} else if (strcmp(name, "sys.stderr") == 0) {
|
||||||
st->user_name = TermErrStream;
|
st->user_name = TermErrStream;
|
||||||
} else {
|
} else {
|
||||||
st->user_name = YAP_MkAtomTerm(st->name);
|
st->user_name = YAP_MkAtomTerm(st->name);
|
||||||
}
|
}
|
||||||
// we assume object is already open, so there is no need to open it.
|
// 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->u.private_data = pystream;
|
||||||
st->vfs = me;
|
st->vfs = me;
|
||||||
python_release_GIL(ctk);
|
python_release_GIL(ctk);
|
||||||
@ -75,8 +77,7 @@ VFS_t pystream;
|
|||||||
|
|
||||||
static bool py_close(int sno) {
|
static bool py_close(int sno) {
|
||||||
StreamDesc *st = YAP_RepStreamFromId(sno);
|
StreamDesc *st = YAP_RepStreamFromId(sno);
|
||||||
if (strcmp(st->name,"sys.stdout") &&
|
if (strcmp(st->name, "sys.stdout") && strcmp(st->name, "sys.stderr")) {
|
||||||
strcmp(st->name,"sys.stderr")) {
|
|
||||||
Py_XDECREF(st->u.private_data);
|
Py_XDECREF(st->u.private_data);
|
||||||
}
|
}
|
||||||
st->u.private_data = NULL;
|
st->u.private_data = NULL;
|
||||||
@ -89,18 +90,19 @@ static bool getLine(int inp) {
|
|||||||
char *myrl_line = NULL;
|
char *myrl_line = NULL;
|
||||||
StreamDesc *rl_instream = YAP_RepStreamFromId(inp);
|
StreamDesc *rl_instream = YAP_RepStreamFromId(inp);
|
||||||
term_t ctk = python_acquire_GIL();
|
term_t ctk = python_acquire_GIL();
|
||||||
fprintf(stderr,"in");
|
fprintf(stderr, "in");
|
||||||
PyObject*prompt = PyUnicode_FromString( "?- "),
|
PyObject *prompt = PyUnicode_FromString("?- "),
|
||||||
*msg = PyUnicode_FromString(" **input** ");
|
*msg = PyUnicode_FromString(" **input** ");
|
||||||
/* window of vulnerability opened */
|
/* 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);
|
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;
|
myrl_line = NULL;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int py_getc(int sno) {
|
static int py_getc(int sno) {
|
||||||
StreamDesc *s = YAP_RepStreamFromId(sno);
|
StreamDesc *s = YAP_RepStreamFromId(sno);
|
||||||
int ch;
|
int ch;
|
||||||
@ -117,7 +119,7 @@ static int py_getc(int sno) {
|
|||||||
} else {
|
} else {
|
||||||
return EOF;
|
return EOF;
|
||||||
}
|
}
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,7 +153,6 @@ static int py_peek(int sno) {
|
|||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int64_t py_seek(int sno, int64_t where, int how) {
|
static int64_t py_seek(int sno, int64_t where, int how) {
|
||||||
StreamDesc *g0 = YAP_RepStreamFromId(sno);
|
StreamDesc *g0 = YAP_RepStreamFromId(sno);
|
||||||
term_t s0 = python_acquire_GIL();
|
term_t s0 = python_acquire_GIL();
|
||||||
@ -159,7 +160,7 @@ static int64_t py_seek(int sno, int64_t where, int how) {
|
|||||||
PyObject *pyr = PyObject_CallFunctionObjArgs(fseek, PyLong_FromLong(where),
|
PyObject *pyr = PyObject_CallFunctionObjArgs(fseek, PyLong_FromLong(where),
|
||||||
PyLong_FromLong(how), NULL);
|
PyLong_FromLong(how), NULL);
|
||||||
python_release_GIL(s0);
|
python_release_GIL(s0);
|
||||||
return PyLong_AsLong(pyr);
|
return PyLong_AsLong(pyr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void py_flush(int sno) {
|
static void py_flush(int sno) {
|
||||||
|
@ -20,7 +20,6 @@
|
|||||||
% ]
|
% ]
|
||||||
%% ).
|
%% ).
|
||||||
:- [library(hacks)].
|
:- [library(hacks)].
|
||||||
|
|
||||||
:- reexport(library(yapi)).
|
:- reexport(library(yapi)).
|
||||||
:- use_module(library(lists)).
|
:- use_module(library(lists)).
|
||||||
:- use_module(library(maplist)).
|
:- use_module(library(maplist)).
|
||||||
@ -274,4 +273,21 @@ close_events( Self ) :-
|
|||||||
fail.
|
fail.
|
||||||
close_events( _ ).
|
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 ).
|
%:- ( start_low_level_trace ).
|
||||||
|
Reference in New Issue
Block a user