Boost.python 中的error_already_set做什么以及如何在 Python C API 中类似地处理
What does error_already_set in Boost.python do and how to handle exceptions similarly in Python C API
我一直在做一个项目,我想删除提升依赖项并将其替换为Python C API。
我花了一些时间了解Python C API,我看到了这个 catch (error_already_set const &)
我阅读了提升文档,但它解释了它的使用位置。但是我想知道为什么需要它,以及如何使用本机Python C api实现相同的功能。
Boost 在发生 Python 错误时抛出error_already_set
。 因此,如果您看到这样的代码:
try {
bp::exec(bp::str("garbage code is garbage"));
} catch (const bp::error_already_set&) {
// your code here to examine Python traceback etc.
}
您将将其替换为:
your_ptr<PyObject> res = PyRun_String("garbage code is garbage");
if (!res) {
// your code here to examine Python traceback etc.
}
换句话说,无论您在哪里看到catch(error_already_set)
,您都可能希望使用所涉及的任何PyObject*
或其他值进行一些错误处理,以识别何时发生错误(因此您可以检查回溯,或将其转换为C++异常(。
相关文章:
- 如何将真正的字符串从python c-api转换为python脚本
- 使用 Python Extension API 包装复杂C++类
- Python to C++ Data structure API
- Python C API:使用 MSVC 尝试示例模块时的访问冲突
- xgboost C API 不会产生与 Python 相同的结果
- 如何仅使用 c/python API 将 c++ 成员函数绑定到 python?
- 如何从主线程正确释放 Python C API GIL
- Python API用于解析包含数组格式的protobuf数据的二进制文件
- Windows API "CreateWindowEx" 在 Python (3.6.8) 64 位使用的 DLL 中工作吗?
- 如何在TensorFlow C API(例如Python)中设置轴
- Valgrind 错误和内存泄漏与 Python/C API
- Python C API-具有可变的参数数量的过载函数
- 使用Python C API时的奇怪记忆行为
- Python C API:如何检查对象是否是类型的实例
- Python C API-返回功能超载中的不同类型
- 如何使用纯Python扩展API(Python3)包裹C 对象
- Python C API:如何访问公共类属性
- 在Python中使用UEYE API
- Python C API:PyDateTime_FromTimestamp会导致分段错误
- 彭博API Python 3.5.5与C++3.8.1.1.在Mac OS X上导入blpapi引用