error handling
This commit is contained in:
parent
df8b264751
commit
029d2836a2
@ -10,10 +10,8 @@
|
|||||||
class YAPPredicate;
|
class YAPPredicate;
|
||||||
class YAPEngine;
|
class YAPEngine;
|
||||||
|
|
||||||
|
|
||||||
#define arity_t uintptr_t
|
#define arity_t uintptr_t
|
||||||
|
|
||||||
|
|
||||||
#ifdef SWIGPYTHON
|
#ifdef SWIGPYTHON
|
||||||
|
|
||||||
%typemap(out) YAPTerm {
|
%typemap(out) YAPTerm {
|
||||||
@ -21,9 +19,7 @@ class YAPEngine;
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
%extend(out) YAPTerm {
|
%extend(out) YAPTerm{YAPTerm & __getitem__(size_t i){Term t0 = $self->term();
|
||||||
YAPTerm & __getitem__ (size_t i) {
|
|
||||||
Term t0 = $self->term();
|
|
||||||
|
|
||||||
if (IsApplTerm(t0)) {
|
if (IsApplTerm(t0)) {
|
||||||
Functor f = FunctorOfTerm(t0);
|
Functor f = FunctorOfTerm(t0);
|
||||||
@ -37,7 +33,6 @@ class YAPEngine;
|
|||||||
}
|
}
|
||||||
return *new YAPTerm();
|
return *new YAPTerm();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(out) YAPIntegerTerm {
|
%typemap(out) YAPIntegerTerm {
|
||||||
@ -68,7 +63,6 @@ class YAPEngine;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// translate lists as Python Lists
|
// translate lists as Python Lists
|
||||||
// Python symbols
|
// Python symbols
|
||||||
// Everthing else let wrapped.
|
// Everthing else let wrapped.
|
||||||
@ -89,46 +83,238 @@ class YAPEngine;
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Language independent exception handler
|
||||||
|
|
||||||
%exception YAPPredicate {
|
%exception {
|
||||||
try {
|
try {
|
||||||
$action
|
$action
|
||||||
} catch (...) {
|
} catch (YAPError e) {
|
||||||
PyErr_SetString(PyExc_SyntaxError, "syntax error");
|
switch (e.getErrorClass()) {
|
||||||
return NULL;
|
case YAPC_NO_ERROR:
|
||||||
|
break;
|
||||||
|
/// bad domain, "first argument often is the predicate.
|
||||||
|
case DOMAIN_ERROR: {
|
||||||
|
yap_error_number en = e.getID();
|
||||||
|
switch (en) {
|
||||||
|
case DOMAIN_ERROR_OUT_OF_RANGE:
|
||||||
|
case DOMAIN_ERROR_NOT_LESS_THAN_ZERO:
|
||||||
|
PyErr_SetString(PyExc_IndexError, e.text());
|
||||||
|
break;
|
||||||
|
case DOMAIN_ERROR_CLOSE_OPTION:
|
||||||
|
case DOMAIN_ERROR_ENCODING:
|
||||||
|
case DOMAIN_ERROR_PROLOG_FLAG:
|
||||||
|
case DOMAIN_ERROR_ABSOLUTE_FILE_NAME_OPTION:
|
||||||
|
case DOMAIN_ERROR_READ_OPTION:
|
||||||
|
case DOMAIN_ERROR_SET_STREAM_OPTION:
|
||||||
|
PyErr_SetString(PyExc_KeyError, e.text());
|
||||||
|
break;
|
||||||
|
case DOMAIN_ERROR_FILE_ERRORS:
|
||||||
|
case DOMAIN_ERROR_FILE_TYPE:
|
||||||
|
case DOMAIN_ERROR_IO_MODE:
|
||||||
|
case DOMAIN_ERROR_SOURCE_SINK:
|
||||||
|
case DOMAIN_ERROR_STREAM_POSITION:
|
||||||
|
PyErr_SetString(PyExc_IOError, e.text());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
PyErr_SetString(PyExc_ValueError, e.text());
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
/// bad arithmetic
|
||||||
|
case EVALUATION_ERROR: {
|
||||||
|
yap_error_number en = e.getID();
|
||||||
|
switch (en) {
|
||||||
|
case EVALUATION_ERROR_FLOAT_OVERFLOW:
|
||||||
|
case EVALUATION_ERROR_INT_OVERFLOW:
|
||||||
|
PyErr_SetString(PyExc_OverflowError, e.text());
|
||||||
|
break;
|
||||||
|
case EVALUATION_ERROR_FLOAT_UNDERFLOW:
|
||||||
|
case EVALUATION_ERROR_UNDERFLOW:
|
||||||
|
case EVALUATION_ERROR_ZERO_DIVISOR:
|
||||||
|
PyErr_SetString(PyExc_ArithmeticError, e.text());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, e.text());
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
/// missing object (I/O mostly)
|
||||||
|
case EXISTENCE_ERROR:
|
||||||
|
PyErr_SetString(PyExc_NotImplementedError, e.text());
|
||||||
|
break;
|
||||||
|
/// should be bound
|
||||||
|
case INSTANTIATION_ERROR_CLASS:
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// bad access, I/O
|
||||||
|
case PERMISSION_ERROR: {
|
||||||
|
yap_error_number en = e.getID();
|
||||||
|
switch (en) {
|
||||||
|
case PERMISSION_ERROR_INPUT_BINARY_STREAM:
|
||||||
|
case PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM:
|
||||||
|
case PERMISSION_ERROR_INPUT_STREAM:
|
||||||
|
case PERMISSION_ERROR_INPUT_TEXT_STREAM:
|
||||||
|
case PERMISSION_ERROR_OPEN_SOURCE_SINK:
|
||||||
|
case PERMISSION_ERROR_OUTPUT_BINARY_STREAM:
|
||||||
|
case PERMISSION_ERROR_REPOSITION_STREAM:
|
||||||
|
case PERMISSION_ERROR_OUTPUT_STREAM:
|
||||||
|
case PERMISSION_ERROR_OUTPUT_TEXT_STREAM:
|
||||||
|
PyErr_SetString(PyExc_OverflowError, e.text());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, e.text());
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
/// something that could not be represented into a type
|
||||||
|
case REPRESENTATION_ERROR:
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// not enough ....
|
||||||
|
case RESOURCE_ERROR:
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// bad text
|
||||||
|
case SYNTAX_ERROR_CLASS:
|
||||||
|
PyErr_SetString(PyExc_SyntaxError, e.text());
|
||||||
|
break;
|
||||||
|
/// OS or internal
|
||||||
|
case SYSTEM_ERROR_CLASS:
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// bad typing
|
||||||
|
case TYPE_ERROR:
|
||||||
|
PyErr_SetString(PyExc_TypeError, e.text());
|
||||||
|
break;
|
||||||
|
/// should be unbound
|
||||||
|
case UNINSTANTIATION_ERROR_CLASS:
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// escape hatch
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
// Language independent exception handler
|
||||||
|
%include exception.i
|
||||||
|
|
||||||
|
%exception {
|
||||||
|
try {
|
||||||
|
$action
|
||||||
|
} catch (YAPError e) {
|
||||||
|
switch (e.getErrorClass()) {
|
||||||
|
case YAPC_NO_ERROR:
|
||||||
|
break;
|
||||||
|
/// bad domain, "first argument often is the predicate.
|
||||||
|
case DOMAIN_ERROR: {
|
||||||
|
yap_error_number en = e.getID();
|
||||||
|
switch (en) {
|
||||||
|
case DOMAIN_ERROR_OUT_OF_RANGE:
|
||||||
|
case DOMAIN_ERROR_NOT_LESS_THAN_ZERO:
|
||||||
|
SWIG_exception(SWIG_IndexError, e.text());
|
||||||
|
break;
|
||||||
|
case DOMAIN_ERROR_CLOSE_OPTION:
|
||||||
|
case DOMAIN_ERROR_ENCODING:
|
||||||
|
case DOMAIN_ERROR_PROLOG_FLAG:
|
||||||
|
case DOMAIN_ERROR_ABSOLUTE_FILE_NAME_OPTION:
|
||||||
|
case DOMAIN_ERROR_READ_OPTION:
|
||||||
|
case DOMAIN_ERROR_SET_STREAM_OPTION:
|
||||||
|
SWIG_exception(SWIG_AttributeError, e.text());
|
||||||
|
break;
|
||||||
|
case DOMAIN_ERROR_FILE_ERRORS:
|
||||||
|
case DOMAIN_ERROR_FILE_TYPE:
|
||||||
|
case DOMAIN_ERROR_IO_MODE:
|
||||||
|
case DOMAIN_ERROR_SOURCE_SINK:
|
||||||
|
case DOMAIN_ERROR_STREAM_POSITION:
|
||||||
|
SWIG_exception(SWIG_IOError, e.text());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
SWIG_exception(SWIG_ValueError, e.text());
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
/// bad arithmetic
|
||||||
|
case EVALUATION_ERROR: {
|
||||||
|
yap_error_number en = e.getID();
|
||||||
|
switch (en) {
|
||||||
|
case EVALUATION_ERROR_FLOAT_OVERFLOW:
|
||||||
|
case EVALUATION_ERROR_FLOAT_UNDERFLOW:
|
||||||
|
case EVALUATION_ERROR_INT_OVERFLOW:
|
||||||
|
case EVALUATION_ERROR_UNDERFLOW:
|
||||||
|
SWIG_exception(SWIG_OverflowError, e.text());
|
||||||
|
break;
|
||||||
|
case EVALUATION_ERROR_ZERO_DIVISOR:
|
||||||
|
SWIG_exception(SWIG_DivisionByZero, e.text());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
/// missing object (I/O mostly)
|
||||||
|
case EXISTENCE_ERROR:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// should be bound
|
||||||
|
case INSTANTIATION_ERROR_CLASS:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// bad access, I/O
|
||||||
|
case PERMISSION_ERROR: {
|
||||||
|
yap_error_number en = e.getID();
|
||||||
|
switch (en) {
|
||||||
|
case PERMISSION_ERROR_INPUT_BINARY_STREAM:
|
||||||
|
case PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM:
|
||||||
|
case PERMISSION_ERROR_INPUT_STREAM:
|
||||||
|
case PERMISSION_ERROR_INPUT_TEXT_STREAM:
|
||||||
|
case PERMISSION_ERROR_OPEN_SOURCE_SINK:
|
||||||
|
case PERMISSION_ERROR_OUTPUT_BINARY_STREAM:
|
||||||
|
case PERMISSION_ERROR_REPOSITION_STREAM:
|
||||||
|
case PERMISSION_ERROR_OUTPUT_STREAM:
|
||||||
|
case PERMISSION_ERROR_OUTPUT_TEXT_STREAM:
|
||||||
|
SWIG_exception(SWIG_OverflowError, e.text());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
/// something that could not be represented into a type
|
||||||
|
case REPRESENTATION_ERROR:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// not enough ....
|
||||||
|
case RESOURCE_ERROR:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// bad text
|
||||||
|
case SYNTAX_ERROR_CLASS:
|
||||||
|
SWIG_exception(SWIG_SyntaxError, e.text());
|
||||||
|
break;
|
||||||
|
/// OS or internal
|
||||||
|
case SYSTEM_ERROR_CLASS:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// bad typing
|
||||||
|
case TYPE_ERROR:
|
||||||
|
SWIG_exception(SWIG_TypeError, e.text());
|
||||||
|
break;
|
||||||
|
/// should be unbound
|
||||||
|
case UNINSTANTIATION_ERROR_CLASS:
|
||||||
|
SWIG_exception(SWIG_RuntimeError, e.text());
|
||||||
|
break;
|
||||||
|
/// escape hatch
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
%exception query {
|
|
||||||
try {
|
|
||||||
$action
|
|
||||||
}
|
|
||||||
catch (YAPError YAP_SYMTAX_ERROR) {
|
|
||||||
SWIG_exception(SWIG_SyntaxError,"Syntax Error exception");
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
SWIG_exception(SWIG_RuntimeError,"Unknown exception");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
%exception next {
|
|
||||||
try {
|
|
||||||
$action
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
SWIG_exception(SWIG_RuntimeError,"Unknown exception");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
%{
|
%{
|
||||||
/* Put header files here or function declarations like below */
|
/* Put header files here or function declarations like below */
|
||||||
|
|
||||||
#define YAP_CPP_INTERFACE 1
|
#define YAP_CPP_INTERFACE 1
|
||||||
|
|
||||||
|
|
||||||
#include "yapi.hh"
|
#include "yapi.hh"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -137,17 +323,16 @@ class YAPEngine;
|
|||||||
#define Yap_regp regcache
|
#define Yap_regp regcache
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// we cannot consult YapInterface.h, that conflicts with what we declare, though
|
// we cannot consult YapInterface.h, that conflicts with what we
|
||||||
|
// declare, though
|
||||||
// it shouldn't
|
// it shouldn't
|
||||||
}
|
}
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
|
||||||
/* turn on director wrapping Callback */
|
/* turn on director wrapping Callback */
|
||||||
%feature("director") YAPCallback;
|
%feature("director") YAPCallback;
|
||||||
|
|
||||||
|
|
||||||
// %include "yapi.hh"
|
// %include "yapi.hh"
|
||||||
|
|
||||||
%include "yapa.hh"
|
%include "yapa.hh"
|
||||||
@ -160,20 +345,6 @@ class YAPEngine;
|
|||||||
|
|
||||||
%include "yapq.hh"
|
%include "yapq.hh"
|
||||||
|
|
||||||
#ifdef SWIGJAVA
|
|
||||||
%javaexception("java.text.ParseException") YAPPredicate {
|
|
||||||
try {
|
|
||||||
$action
|
|
||||||
} catch (YAPError::SYNTAX_ERROR &e) {
|
|
||||||
jclass clazz = jenv->FindClass("java/text/ParseException");
|
|
||||||
jenv->ThrowNew(clazz, "Syntax error");
|
|
||||||
return $null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%init %{
|
%init %{
|
||||||
#ifdef SWIGPYTHON
|
#ifdef SWIGPYTHON
|
||||||
init_python();
|
init_python();
|
||||||
|
Reference in New Issue
Block a user