简体   繁体   中英

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.

This is also evident by the documentation of 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) . ... traceback gets a traceback object (see the Reference Manual) which encapsulates the call stack at the point where the exception originally occurred.

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.

My question is, therefore, where is the traceback type available for python scripts?

EDIT:

I would like to use the traceback type to define a PyQt signal . PyQt Signals are defined by specifying the signal name together with the types of parameters passed. I do not need to create an object of that type, only use it in a manner similar to an isinstance call.

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)

You can use traceback.format_exc() or sys.exc_info() like :

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:

>>> 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. 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. Please feel free to leave comments with such articles (or other StackOverflow questions which actually care about this).

That should be defined in C code:

Include/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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM