GCC 4.5、boost 和 throw_error_already_set

发布于 2024-11-10 00:27:49 字数 3378 浏览 3 评论 0原文

使用Python 2.2.3,Boost 1.46和这个简单的扩展模块:

#include <Python.h>
#include <boost/python.hpp>

using namespace boost::python ;
using namespace boost;

class PyTest
{
public:
    PyTest();
};

PyTest::PyTest()
{
    PyErr_SetString(PyExc_RuntimeError, "FOO");
    throw_error_already_set();
}

BOOST_PYTHON_MODULE(testmod)
{
    class_<PyTest>("Test", init<>())
        ;
}

如果我构建Boost,那么这个模块,使用GCC 3.4.5(XP SP3上的Mingw),一切正常:

>>> import testmod
>>> testmod.Test()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
RuntimeError: FOO

除了编译器之外不改变任何东西(GCC 4.5.2) )给出了这个:

>>> import testmod
>>> testmod.Test()

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

所以我启动 gdb 并在中止时中断。在我看来,error_already_set 异常没有被任何人捕获:

(gdb) bt
#0  0x77c36bb3 in msvcrt!abort () from C:\WINDOWS\system32\msvcrt.dll
#1  0x69acfdb2 in testmod!_Unwind_SetGR () from C:\Python22\DLLs\testmod.pyd
#2  0x69b2a45d in testmod!_ZNK5boost6python7objects23caller_py_function_implINS0
_6detail6callerIPFvP7_objectENS0_21default_call_policiesENS_3mpl7vector2IvS6_EEE
EE9signatureEv () from C:\Python22\DLLs\testmod.pyd
#3  0x6e9544a3 in libgcc_s_dw2-1!__trunctfxf2 ()
   from C:\WINDOWS\system32\libgcc_s_dw2-1.dll
#4  0x6e954877 in libgcc_s_dw2-1!_Unwind_RaiseException ()
   from C:\WINDOWS\system32\libgcc_s_dw2-1.dll
#5  0x6fcbc6e2 in libstdc++-6!__cxa_throw ()
   from C:\WINDOWS\system32\libstdc++-6.dll
#6  0x63455d4c in boost::python::throw_error_already_set() ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#7  0x69ac12de in testmod!_ZN6PyTestC2Ev () from C:\Python22\DLLs\testmod.pyd
#8  0x69adbbf1 in testmod!_ZN5boost6python7objects11make_holderILi0EE5applyINS1_
12value_holderI6PyTestEENS_3mpl7vector0IN4mpl_2naEEEE7executeEP7_object ()
   from C:\Python22\DLLs\testmod.pyd
#9  0x69adbde9 in testmod!_ZN5boost6python7objects23caller_py_function_implINS0_
6detail6callerIPFvP7_objectENS0_21default_call_policiesENS_3mpl7vector2IvS6_EEEE
EclES6_S6_ () from C:\Python22\DLLs\testmod.pyd
#10 0x63450031 in boost::python::objects::function::call(_object*, _object*) con
st () from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#11 0x634501fb in boost::detail::function::void_function_ref_invoker0<boost::pyt
hon::objects::(anonymous namespace)::bind_return, void>::invoke(boost::detail::f
unction::function_buffer&) ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#12 0x63455e5a in boost::python::handle_exception_impl(boost::function0<void>)
    () from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#13 0x634512be in function_call ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#14 0x1e006ccc in python22!PyObject_Call ()
   from C:\WINDOWS\system32\python22.dll
#15 0x0089d138 in ?? ()
#16 0x63469420 in boost::python::docstring_options::show_py_signatures_ ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#17 0x0084d0f0 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

有趣的是,在我的实际用例中,该类包装了多个方法;对于其中一些,error_already_set 机制有效;对于其他一些人来说则不然……C++ 大师对此有什么提示吗?

Using Python 2.2.3, Boost 1.46 and this trivial extension module:

#include <Python.h>
#include <boost/python.hpp>

using namespace boost::python ;
using namespace boost;

class PyTest
{
public:
    PyTest();
};

PyTest::PyTest()
{
    PyErr_SetString(PyExc_RuntimeError, "FOO");
    throw_error_already_set();
}

BOOST_PYTHON_MODULE(testmod)
{
    class_<PyTest>("Test", init<>())
        ;
}

If I build Boost, then this module, using GCC 3.4.5 (Mingw on XP SP3), everything works well:

>>> import testmod
>>> testmod.Test()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
RuntimeError: FOO

Not changing anything but the compiler (GCC 4.5.2) gives this:

>>> import testmod
>>> testmod.Test()

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

So I launch gdb and put a break on abort. Seems to me like the error_already_set exception isn't catched by anybody:

(gdb) bt
#0  0x77c36bb3 in msvcrt!abort () from C:\WINDOWS\system32\msvcrt.dll
#1  0x69acfdb2 in testmod!_Unwind_SetGR () from C:\Python22\DLLs\testmod.pyd
#2  0x69b2a45d in testmod!_ZNK5boost6python7objects23caller_py_function_implINS0
_6detail6callerIPFvP7_objectENS0_21default_call_policiesENS_3mpl7vector2IvS6_EEE
EE9signatureEv () from C:\Python22\DLLs\testmod.pyd
#3  0x6e9544a3 in libgcc_s_dw2-1!__trunctfxf2 ()
   from C:\WINDOWS\system32\libgcc_s_dw2-1.dll
#4  0x6e954877 in libgcc_s_dw2-1!_Unwind_RaiseException ()
   from C:\WINDOWS\system32\libgcc_s_dw2-1.dll
#5  0x6fcbc6e2 in libstdc++-6!__cxa_throw ()
   from C:\WINDOWS\system32\libstdc++-6.dll
#6  0x63455d4c in boost::python::throw_error_already_set() ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#7  0x69ac12de in testmod!_ZN6PyTestC2Ev () from C:\Python22\DLLs\testmod.pyd
#8  0x69adbbf1 in testmod!_ZN5boost6python7objects11make_holderILi0EE5applyINS1_
12value_holderI6PyTestEENS_3mpl7vector0IN4mpl_2naEEEE7executeEP7_object ()
   from C:\Python22\DLLs\testmod.pyd
#9  0x69adbde9 in testmod!_ZN5boost6python7objects23caller_py_function_implINS0_
6detail6callerIPFvP7_objectENS0_21default_call_policiesENS_3mpl7vector2IvS6_EEEE
EclES6_S6_ () from C:\Python22\DLLs\testmod.pyd
#10 0x63450031 in boost::python::objects::function::call(_object*, _object*) con
st () from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#11 0x634501fb in boost::detail::function::void_function_ref_invoker0<boost::pyt
hon::objects::(anonymous namespace)::bind_return, void>::invoke(boost::detail::f
unction::function_buffer&) ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#12 0x63455e5a in boost::python::handle_exception_impl(boost::function0<void>)
    () from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#13 0x634512be in function_call ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#14 0x1e006ccc in python22!PyObject_Call ()
   from C:\WINDOWS\system32\python22.dll
#15 0x0089d138 in ?? ()
#16 0x63469420 in boost::python::docstring_options::show_py_signatures_ ()
   from C:\WINDOWS\system32\libboost_python-mgw45-mt-1_46_1.dll
#17 0x0084d0f0 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

The funny thing is that in my actual use case, the class has several methods wrapped; for some of them the error_already_set mechanism works; for some others it doesn't... Does a C++ guru have any hint about this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文