#1295 std_except.i and bug with SWIG_STD_EXCEPTIONS_AS_CLASSES

None
closed
None
6
2013-01-05
2012-12-20
Paweł Tomulik
No

Hello,

I need to use standard exceptions as classes in python and can't make it work.

With following simple module:

---- 8< ------------------------------
%module std_except_mod

%inline %{
void foo() { throw std::logic_error("Hi there"); }
%}

%import <std_string.i>
#define SWIG_STD_EXCEPTIONS_AS_CLASSES
%include <std_except.i>

void foo() { throw std::logic_error("Hi there"); }
---- 8< ------------------------------

I get a bunch of warnings when running swig on it and %exceptionclass is not applied to the standard exceptions classes being wrapped
(so, I'm not able to catch them properly in python code).

Compilation log says:

------- 8< ---------------------------------------------------------------------------
swig -c++ -python std_except_mod.i && g++ -shared -fPIC -I . -I /usr/include/python2.7 -o _std_except_mod.so std_except_mod_wrap.cxx
/usr/share/swig2.0/typemaps/std_except.swg:41: Warning 302: Identifier 'exception' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:13: Warning 302: previous definition of 'exception'.
/usr/share/swig2.0/typemaps/std_except.swg:47: Warning 302: Identifier 'bad_exception' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:19: Warning 302: previous definition of 'bad_exception'.
/usr/share/swig2.0/typemaps/std_except.swg:51: Warning 302: Identifier 'logic_error' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:23: Warning 302: previous definition of 'logic_error'.
/usr/share/swig2.0/typemaps/std_except.swg:56: Warning 302: Identifier 'domain_error' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:28: Warning 302: previous definition of 'domain_error'.
/usr/share/swig2.0/typemaps/std_except.swg:61: Warning 302: Identifier 'invalid_argument' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:33: Warning 302: previous definition of 'invalid_argument'.
/usr/share/swig2.0/typemaps/std_except.swg:66: Warning 302: Identifier 'length_error' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:38: Warning 302: previous definition of 'length_error'.
/usr/share/swig2.0/typemaps/std_except.swg:71: Warning 302: Identifier 'out_of_range' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:43: Warning 302: previous definition of 'out_of_range'.
/usr/share/swig2.0/typemaps/std_except.swg:76: Warning 302: Identifier 'runtime_error' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:48: Warning 302: previous definition of 'runtime_error'.
/usr/share/swig2.0/typemaps/std_except.swg:81: Warning 302: Identifier 'range_error' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:53: Warning 302: previous definition of 'range_error'.
/usr/share/swig2.0/typemaps/std_except.swg:86: Warning 302: Identifier 'overflow_error' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:58: Warning 302: previous definition of 'overflow_error'.
/usr/share/swig2.0/typemaps/std_except.swg:91: Warning 302: Identifier 'underflow_error' redefined (ignored),
/usr/share/swig2.0/std/std_except.i:63: Warning 302: previous definition of 'underflow_error'.
------- 8< ---------------------------------------------------------------------------

In my opinion, the problem sits in 'typemap/std_except.swg'. I'll post a patch proposition soon.

Simple test in python interpreter reveals that the exception classes are not handled properly:

ptomulik@barakus:$ python
Python 2.7.3 (default, Sep 9 2012, 17:41:34)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from std_except_mod import *
>>> try: foo()
... except exception: pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
SystemError: 'finally' pops bad exception

IHMO, the exception should be caught above.

Attached is a mini-project to replicate the bug and possible patch.
After patching, the project compiles without warnings and exceptions
seem to be caughr correctly.

Discussion

  • Paweł Tomulik
    Paweł Tomulik
    2012-12-20

    • assigned_to: nobody --> wsfulton
    • priority: 5 --> 4
     
  • Paweł Tomulik
    Paweł Tomulik
    2012-12-20

    As always, I have to make a mistake :)

    the code of the test file std_except_mod.i reads as (note "throw" specifications):

    ---- 8< ------------------------------
    %module std_except_mod
    //
    %{
    #include <exception>
    #include <stdexcept>
    void foo() throw(std::logic_error) { throw std::logic_error("Hi there"); }
    %}
    //
    %include <std_string.i>
    #define SWIG_STD_EXCEPTIONS_AS_CLASSES
    %include <std_except.i>
    //
    void foo() throw(std::logic_error){ throw std::logic_error("Hi there"); }
    ---- 8< ------------------------------

     
  • Paweł Tomulik
    Paweł Tomulik
    2012-12-20

    • priority: 4 --> 6
     
  • William Fulton
    William Fulton
    2013-01-05

    • status: open --> closed
    • milestone: -->
     
  • William Fulton
    William Fulton
    2013-01-05

    Thanks, applied your Github patch for swig-2.0.10.