SWIG C++ 到 Python:在抛出一个实例后调用终止...中止

SWIG C++ to Python: terminate called after throwing an instance of ... Aborted

我正在尝试编写一个 SWIG 模块,但我似乎无法弄清楚如何从 C++ 中捕获异常并将它们传播到 Python。这是我的代码的简化版本:

example.cpp:

#include "example.h"

Looper::Looper() {

    nframes = 0;

}

void Looper::set_nframes(int nf) {

   if (nf < 0) {
        throw LooperValueError();
   }   

   nframes = nf; 

}

int Looper::get_nframes(void) {

   return nframes;

}

example.h:

class LooperValueError {}; 

class Looper {

    private:
        int nframes;

    public:
        Looper();
        void set_nframes(int);
        int get_nframes(void);

};

example.i:

%module example
%{
#include "example.h"
%}

%include "example.h"

%exception {
    try {
        $function
    } catch (LooperValueError) {
        PyErr_SetString(PyExc_ValueError,"Looper value out of range");
        return NULL;
    }   
}

构建良好。但是然后在 Python 中,当我调用 Looper.set_nframes(-2) 时,我没有像我期望的那样得到 ValueError;相反,代码解释器崩溃了:

terminate called after throwing an instance of 'LooperValueError'
Aborted

包装器似乎没有捕获异常。我做错了什么?

%exception 的效果仅对后面的声明有效。你在 %include 之后写了 %exception,所以它实际上并没有应用到任何东西上。 (查看生成的代码以验证这一点 - 您的 try/catch 块实际上还没有完成输出)。

因此您的界面应该如下所示:

%module example
%{
#include "example.h"
%}

%exception {
    try {
        $function
    } catch (const LooperValueError&) {
        PyErr_SetString(PyExc_ValueError,"Looper value out of range");
        return NULL;
    }   
}

%include "example.h"

我调整了一个小点:通常你应该更喜欢 catch exceptions by const reference 而不是按值。