2

使用 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 并中断 abort。在我看来,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++ 大师对此有任何暗示吗?

4

0 回答 0