NVS*_*ash 4 c++ python exception-handling python-c-api boost-python
我一直在进行一个项目,在该项目中我想删除boost依赖项并将其替换为Python C API。
我花了一些时间了解Python C API,并且看到了
catch (error_already_set const &)
我阅读了boost文档,但是它解释了它的使用位置。但是我想知道为什么需要它,以及如何使用本机Python C api实现相同的功能。
error_already_set发生Python错误时,Boost引发。因此,如果您看到这样的代码:
try {
bp::exec(bp::str("garbage code is garbage"));
} catch (const bp::error_already_set&) {
// your code here to examine Python traceback etc.
}
Run Code Online (Sandbox Code Playgroud)
您将其替换为:
your_ptr<PyObject> res = PyRun_String("garbage code is garbage");
if (!res) {
// your code here to examine Python traceback etc.
}
Run Code Online (Sandbox Code Playgroud)
换句话说,无论您在哪里看到catch(error_already_set),您都可能希望使用PyObject*涉及的任何值或其他值来进行一些错误处理,以识别何时发生错误(因此您可以检查回溯,或将其转换为C ++异常)。
| 归档时间: |
|
| 查看次数: |
2321 次 |
| 最近记录: |