【发布时间】:2010-11-03 07:50:09
【问题描述】:
如果我这样做(例如)
open("/snafu/fnord")
在 Python 中(并且该文件不存在),我得到一个回溯和消息
IOError: [Errno 2] No such file or directory: '/snafu/fnord'
我想用 Python 的 C API(即嵌入在 C 程序中的 Python 解释器)获取上述字符串。我需要它作为字符串,而不是输出到控制台。
使用PyErr_Fetch()我可以得到异常的类型对象和值。对于上面的例子,值是一个元组:
(2, 'No such file or directory', '/snafu/fnord')
从PyErr_Fetch() 获得的信息到 Python 解释器显示的字符串是否有简单的方法? (不涉及自己为每种异常类型构造此类字符串。)
【问题讨论】:
标签: python exception python-c-api