boost :: python导出自定义异常

Jac*_*nds 23 c++ python exception boost-python

我目前正在使用Boost.Python为Python编写C++扩展.此扩展中的函数可能会生成一个包含错误信息的异常(除了描述发生的事件的人类可读字符串之外).我希望我可以将这个异常导出到Python,这样我就能抓住它并用额外的信息做些什么.

例如:

import my_cpp_module
try:
    my_cpp_module.my_cpp_function()
except my_cpp_module.MyCPPException, e:
    print e.my_extra_data
Run Code Online (Sandbox Code Playgroud)

不幸的是,Boost.Python似乎将所有C++异常(都是其子类std::exception)转换为RuntimeError.我意识到Boost.Python允许一个人实现自定义异常转换,但是,需要使用PyErr_SetObject一个PyObject*(对于异常的类型)和一个PyObject*(对于异常的值) - 我都不知道如何从我的Boost中获取. Python类.也许有一种方法(这将是伟大的),我还没有找到.否则有人知道如何导出自定义C++异常,以便我可以在Python中捕获它吗?

Jac*_*nds 26

解决方案是像任何普通的C++类一样创建异常类

class MyCPPException : public std::exception {...}
Run Code Online (Sandbox Code Playgroud)

诀窍是所有boost :: python :: class_实例都包含对对象类型的引用,可以通过它们的ptr()函数访问它.您可以使用boost :: python注册类,因为这样:

class_<MyCPPException> myCPPExceptionClass("MyCPPException"...);
PyObject *myCPPExceptionType=myCPPExceptionClass.ptr();
register_exception_translator<MyCPPException>(&translateFunc);
Run Code Online (Sandbox Code Playgroud)

最后,当您将C++异常转换为Python异常时,请执行以下操作:

void translate(MyCPPException const &e)
{
    PyErr_SetObject(myCPPExceptionType, boost::python::object(e).ptr());
}
Run Code Online (Sandbox Code Playgroud)

这是一个完整的工作示例:

#include <boost/python.hpp>
#include <assert.h>
#include <iostream>

class MyCPPException : public std::exception
{
private:
  std::string message;
  std::string extraData;
public:
  MyCPPException(std::string message, std::string extraData)
  {
    this->message = message;
    this->extraData = extraData;
  }
  const char *what() const throw()
  {
    return this->message.c_str();
  }
  ~MyCPPException() throw()
  {
  }
  std::string getMessage()
  {
    return this->message;
  }
  std::string getExtraData()
  {
    return this->extraData;
  }
};

void my_cpp_function(bool throwException)
{
  std::cout << "Called a C++ function." << std::endl;
  if (throwException)
    {
      throw MyCPPException("Throwing an exception as requested.",
               "This is the extra data.");
    }
}

PyObject *myCPPExceptionType = NULL;

void translateMyCPPException(MyCPPException const &e)
{
  assert(myCPPExceptionType != NULL);
  boost::python::object pythonExceptionInstance(e);
  PyErr_SetObject(myCPPExceptionType, pythonExceptionInstance.ptr());
}

BOOST_PYTHON_MODULE(my_cpp_extension)
{
  boost::python::class_<MyCPPException>
    myCPPExceptionClass("MyCPPException",
            boost::python::init<std::string, std::string>());
  myCPPExceptionClass.add_property("message", &MyCPPException::getMessage)
    .add_property("extra_data", &MyCPPException::getExtraData);
  myCPPExceptionType = myCPPExceptionClass.ptr();
  boost::python::register_exception_translator<MyCPPException>
    (&translateMyCPPException);
  boost::python::def("my_cpp_function", &my_cpp_function);
}
Run Code Online (Sandbox Code Playgroud)

以下是调用扩展的Python代码:

import my_cpp_extension
try:
    my_cpp_extension.my_cpp_function(False)
    print 'This line should be reached as no exception should be thrown.'
except my_cpp_extension.MyCPPException, e:
    print 'Message:', e.message
    print 'Extra data:',e.extra_data

try:
    my_cpp_extension.my_cpp_function(True)
    print ('This line should not be reached as an exception should have been' +
       'thrown by now.')
except my_cpp_extension.MyCPPException, e:
    print 'Message:', e.message
    print 'Extra data:',e.extra_data
Run Code Online (Sandbox Code Playgroud)