[英]using the python traceback type
By calling sys.exc_info()
when an exception is handled a 3-tuple is returned containing the exception class, the exception object and the traceback.通过在处理异常时调用
sys.exc_info()
返回一个 3 元组,其中包含异常类、异常对象和回溯。
This is also evident by the documentation of sys.exc_info
: sys.exc_info
的文档也证明了这一点:
This function returns a tuple of three values that give information about the exception that is currently being handled.
此函数返回一个包含三个值的元组,这些值提供有关当前正在处理的异常的信息。 ... the values returned are
(type, value, traceback)
....返回的值是
(type, value, traceback)
。 ... traceback gets a traceback object (see the Reference Manual) which encapsulates the call stack at the point where the exception originally occurred.... traceback获取一个 traceback 对象(参见参考手册),它封装了最初发生异常的点的调用堆栈。
I want to use the type traceback which is used to create the third variable in the aforementioned exc_info
return value but can't find where it's defined.我想使用用于在上述
exc_info
返回值中创建第三个变量的类型回溯,但找不到它的定义位置。
My question is, therefore, where is the traceback
type available for python scripts?因此,我的问题是,可用于 python 脚本的
traceback
类型在哪里?
EDIT:编辑:
I would like to use the traceback
type to define a PyQt signal .我想使用
traceback
类型来定义 PyQt信号。 PyQt Signals are defined by specifying the signal name together with the types of parameters passed. PyQt 信号是通过指定信号名称和传递的参数类型来定义的。 I do not need to create an object of that type, only use it in a manner similar to an
isinstance
call.我并不需要创建一个类型的对象,只有在类似的方式使用它
isinstance
调用。
bad news, even if you can get the class of the traceback object like this:坏消息,即使您可以像这样获得回溯对象的类:
import sys
try:
raise Exception
except:
tb = sys.exc_info()[2]
print(tb.__class__)
result:结果:
<class 'traceback'>
when you try:当你尝试:
tb.__class__()
you get:你得到:
TypeError: cannot create 'traceback' instances
so the traceback type cannot be instanciated externally, probably because you'd need to access python internals to do so (and attributes are read-only even tb_lineno
so not possible to "reuse" an instance either)所以回溯类型不能在外部实例化,可能是因为你需要访问 python 内部来这样做(并且属性是只读的,即使是
tb_lineno
所以也不可能“重用”一个实例)
You can use traceback.format_exc()
or sys.exc_info()
like :您可以使用
traceback.format_exc()
或sys.exc_info()
像:
try:
raise TypeError("Error !?!")
except Exception:
print(traceback.format_exc())
# or
print(sys.exc_info()[2])
I see the tag includes python2.7
, and the question is old, so apologise for bringing this up again, but just wanted to say that in modern Python, the TracebackType is now in the types
module:我看到标签包含
python2.7
,问题很旧,所以很抱歉再次提出这个问题,但只想说在现代 Python 中,TracebackType 现在位于types
模块中:
>>> isinstance(sys.exc_info()[2], types.TracebackType))
True
Though it seems the class itself thinks it has a different name:虽然看起来类本身认为它有一个不同的名字:
>>> repr(sys.exc_info()[2].__class__)
<class 'traceback'>
What is really interesting, though not the subject of the question, was an interpretation in one of the answers, is that it is now possible (since Python 3.7) to construct a traceback manually.真正有趣的是,虽然不是问题的主题,但对其中一个答案的解释是,现在可以(自 Python 3.7 起)手动构建回溯。 This has interesting implications for accurately reflecting the reported traceback in an exception, and is particularly relevant to deferred/lazy/asynchronous exceptions.
这对于在异常中准确反映报告的回溯具有有趣的意义,并且与延迟/延迟/异步异常特别相关。 The change itself came in https://github.com/python/cpython/pull/4793 - I don't see any good docs/examples of this being done in the wild yet though.
更改本身来自https://github.com/python/cpython/pull/4793 - 尽管如此,我还没有看到任何好的文档/示例。 Please feel free to leave comments with such articles (or other StackOverflow questions which actually care about this).
请随时对此类文章(或其他真正关心此问题的 StackOverflow 问题)发表评论。
That should be defined in C code:这应该在 C 代码中定义:
Include/traceback.h包含/traceback.h
#ifndef Py_LIMITED_API
typedef struct _traceback {
PyObject_HEAD
struct _traceback *tb_next;
struct _frame *tb_frame;
int tb_lasti;
int tb_lineno;
} PyTracebackObject;
#endif
Hm.. in fact, I am not very sure what you want to do, so cannot help more..嗯..事实上,我不太确定你想做什么,所以不能帮助更多..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.