我是靠谱客的博主 优美冬天,最近开发中收集的这篇文章主要介绍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).

最后

以上就是优美冬天为你收集整理的boost python异常处理_Boost.python中的error_already_set做什么,以及如何在Python C API中类似地处理异常...的全部内容,希望文章能够帮你解决boost python异常处理_Boost.python中的error_already_set做什么,以及如何在Python C API中类似地处理异常...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部