use exception specification instead of %exception to handle STL error checking
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7352 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
ebde3e6253
commit
03a67698a9
34 changed files with 299 additions and 849 deletions
|
|
@ -357,99 +357,6 @@ namespace swig
|
|||
|
||||
|
||||
%define %swig_container_methods(Container...)
|
||||
// __getitem__ is required to raise an IndexError for for-loops to work
|
||||
// other methods which can raise are made to throw an IndexError as well
|
||||
%exception __getitem__ {
|
||||
try { $action }
|
||||
catch (std::out_of_range& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
SWIG_CATCH_UNKNOWN
|
||||
}
|
||||
|
||||
%exception __setitem__ {
|
||||
try { $action }
|
||||
catch (std::out_of_range& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
SWIG_CATCH_UNKNOWN
|
||||
}
|
||||
|
||||
%exception __getslice__ {
|
||||
try { $action }
|
||||
catch (std::out_of_range& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
SWIG_CATCH_UNKNOWN
|
||||
}
|
||||
|
||||
|
||||
%exception __setslice__ {
|
||||
try { $action }
|
||||
catch (std::out_of_range& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
catch (std::invalid_argument& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_TypeError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
SWIG_CATCH_UNKNOWN
|
||||
}
|
||||
|
||||
%exception __delslice__ {
|
||||
try { $action }
|
||||
catch (std::out_of_range& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
SWIG_CATCH_UNKNOWN
|
||||
}
|
||||
|
||||
%exception __delitem__ {
|
||||
try { $action }
|
||||
catch (std::out_of_range& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
SWIG_CATCH_UNKNOWN
|
||||
}
|
||||
|
||||
%exception pop {
|
||||
try { $action }
|
||||
catch (std::out_of_range& e) {
|
||||
if (!PyErr_Occurred()) {
|
||||
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
|
||||
} else {
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
SWIG_CATCH_UNKNOWN
|
||||
}
|
||||
|
||||
%newobject __getslice__;
|
||||
|
||||
|
|
@ -469,7 +376,7 @@ namespace swig
|
|||
%fragment("PySequence_Base");
|
||||
|
||||
%extend {
|
||||
value_type pop() {
|
||||
value_type pop() throw (std::out_of_range) {
|
||||
if (self->size() == 0)
|
||||
throw std::out_of_range("pop from empty container");
|
||||
Sequence::value_type x = self->back();
|
||||
|
|
@ -477,19 +384,19 @@ namespace swig
|
|||
return x;
|
||||
}
|
||||
|
||||
Sequence* __getslice__(difference_type i, difference_type j) {
|
||||
Sequence* __getslice__(difference_type i, difference_type j) throw (std::out_of_range) {
|
||||
return swig::getslice(self, i, j);
|
||||
}
|
||||
|
||||
void __setslice__(difference_type i, difference_type j, const Sequence& v) {
|
||||
void __setslice__(difference_type i, difference_type j, const Sequence& v) throw (std::out_of_range, std::invalid_argument) {
|
||||
swig::setslice(self, i, j, v);
|
||||
}
|
||||
|
||||
void __delslice__(difference_type i, difference_type j) {
|
||||
void __delslice__(difference_type i, difference_type j) throw (std::out_of_range) {
|
||||
swig::delslice(self, i, j);
|
||||
}
|
||||
|
||||
void __delitem__(difference_type i) {
|
||||
void __delitem__(difference_type i) throw (std::out_of_range) {
|
||||
self->erase(swig::getpos(self,i));
|
||||
}
|
||||
}
|
||||
|
|
@ -498,11 +405,11 @@ namespace swig
|
|||
%define %swig_sequence_methods(Sequence...)
|
||||
%swig_sequence_methods_common(SWIG_arg(Sequence))
|
||||
%extend {
|
||||
const value_type& __getitem__(difference_type i) const {
|
||||
const value_type& __getitem__(difference_type i) const throw (std::out_of_range) {
|
||||
return *(swig::cgetpos(self, i));
|
||||
}
|
||||
|
||||
void __setitem__(difference_type i, const value_type& x) {
|
||||
void __setitem__(difference_type i, const value_type& x) throw (std::out_of_range) {
|
||||
*(swig::getpos(self,i)) = x;
|
||||
}
|
||||
|
||||
|
|
@ -515,11 +422,11 @@ namespace swig
|
|||
%define %swig_sequence_methods_val(Sequence...)
|
||||
%swig_sequence_methods_common(SWIG_arg(Sequence))
|
||||
%extend {
|
||||
value_type __getitem__(difference_type i) {
|
||||
value_type __getitem__(difference_type i) throw (std::out_of_range) {
|
||||
return *(swig::cgetpos(self, i));
|
||||
}
|
||||
|
||||
void __setitem__(difference_type i, value_type x) {
|
||||
void __setitem__(difference_type i, value_type x) throw (std::out_of_range) {
|
||||
*(swig::getpos(self,i)) = x;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
%include <std/std_except.i>
|
||||
%include <pystdcommon.swg>
|
||||
%include <std/std_common.i>
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,79 @@
|
|||
%include <std/std_except.i>
|
||||
%include <std_string.i>
|
||||
|
||||
%{
|
||||
#include <stdexcept>
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
/* Mark all of them as exception classes */
|
||||
%feature("exceptionclass") exception;
|
||||
%feature("exceptionclass") bad_exception;
|
||||
%feature("exceptionclass") logic_error;
|
||||
%feature("exceptionclass") domain_error;
|
||||
%feature("exceptionclass") invalid_argument;
|
||||
%feature("exceptionclass") length_error;
|
||||
%feature("exceptionclass") out_of_range;
|
||||
%feature("exceptionclass") runtime_error;
|
||||
%feature("exceptionclass") range_error;
|
||||
%feature("exceptionclass") overflow_error;
|
||||
%feature("exceptionclass") underflow_error;
|
||||
}
|
||||
|
||||
namespace std {
|
||||
struct exception
|
||||
{
|
||||
virtual ~exception() throw();
|
||||
virtual const char* what() const throw();
|
||||
};
|
||||
|
||||
struct bad_exception : exception
|
||||
{
|
||||
};
|
||||
|
||||
struct logic_error : exception
|
||||
{
|
||||
logic_error(const string& msg);
|
||||
};
|
||||
|
||||
struct domain_error : logic_error
|
||||
{
|
||||
domain_error(const string& msg);
|
||||
};
|
||||
|
||||
struct invalid_argument : logic_error
|
||||
{
|
||||
invalid_argument(const string& msg);
|
||||
};
|
||||
|
||||
struct length_error : logic_error
|
||||
{
|
||||
length_error(const string& msg);
|
||||
};
|
||||
|
||||
struct out_of_range : logic_error
|
||||
{
|
||||
out_of_range(const string& msg);
|
||||
};
|
||||
|
||||
struct runtime_error : exception
|
||||
{
|
||||
runtime_error(const string& msg);
|
||||
};
|
||||
|
||||
struct range_error : runtime_error
|
||||
{
|
||||
range_error(const string& msg);
|
||||
};
|
||||
|
||||
struct overflow_error : runtime_error
|
||||
{
|
||||
overflow_error(const string& msg);
|
||||
};
|
||||
|
||||
struct underflow_error : runtime_error
|
||||
{
|
||||
underflow_error(const string& msg);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@
|
|||
%swig_container_methods(Map)
|
||||
|
||||
%extend {
|
||||
mapped_type __getitem__(const key_type& key) const {
|
||||
mapped_type __getitem__(const key_type& key) const throw (std::out_of_range) {
|
||||
Map::const_iterator i = self->find(key);
|
||||
if (i != self->end())
|
||||
return i->second;
|
||||
|
|
@ -69,11 +69,11 @@
|
|||
throw std::out_of_range("key not found");
|
||||
}
|
||||
|
||||
void __setitem__(const key_type& key, const mapped_type& x) {
|
||||
void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
|
||||
self->insert(Map::value_type(key,x));
|
||||
}
|
||||
|
||||
void __delitem__(const key_type& key) {
|
||||
void __delitem__(const key_type& key) throw (std::out_of_range) {
|
||||
Map::iterator i = self->find(key);
|
||||
if (i != self->end())
|
||||
self->erase(i);
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@
|
|||
return self->find(x) != self->end();
|
||||
}
|
||||
|
||||
value_type __getitem__(difference_type i) const {
|
||||
value_type __getitem__(difference_type i) const throw (std::out_of_range) {
|
||||
return *(swig::cgetpos(self, i));
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue