boost python异常处理,Boost.python中的error_already_set做什么,以及如何在Python C API中类似地处理异常...

I have been working on a project where I want to remove the boost dependencies and replace it with the Python C API.

I spent some time understanding the Python C API and I saw this

catch (error_already_set const &)

I read the boost docs but it explains where it is used. But I want to know why it is needed and how can I achieve the same functionality using the native Python C api.

解决方案

Boost throws error_already_set when a Python error has occurred. So if you see code like this:

try {

bp::exec(bp::str("garbage code is garbage"));

} catch (const bp::error_already_set&) {

// your code here to examine Python traceback etc.

}

you'll replace it with:

your_ptr res = PyRun_String("garbage code is garbage");

if (!res) {

// your code here to examine Python traceback etc.

}

In other words, wherever you see catch(error_already_set), there you will likely want to do some error handling using whatever PyObject* or other value is involved to recognize when an error has occurred (and therefore you can examine the traceback, or convert it into a C++ exception).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值