3

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.

7
  • This smells like an XY problem. How do you want to use the traceback type? Commented Dec 30, 2017 at 16:18
  • @CristiFati Edited with an explanation for why I need the type Commented Dec 30, 2017 at 16:36
  • @Axalix This is not a duplicate, as I do not need to create an instance of said type, which is evidently more difficult (see Jean-Francois's answer) Commented Dec 30, 2017 at 16:37
  • Maybe - docs.python.org/3/library/traceback.html Commented Dec 30, 2017 at 16:52
  • 1
    using the traceback type & creating a traceback object: what's the difference. Reopening anyway. Commented Dec 30, 2017 at 16:54

4 Answers 4

4

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)

Sign up to request clarification or add additional context in comments.

2 Comments

This is fine by me, as I do not need to create an instance. See edit to my question.
I repeat: This isn't a duplicate as I do not wish to create an instance...
1

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])

4 Comments

Yes, however this is not what I was asking about. I'm asking about the traceback type, not the traceback module.
Still not what OP is asking.
calling sys.exc_info while inside an exception and getting the type of the second tuple object is indeed a way to get the object, however it is very hackish and not pythonic. I rather have a more straightforward way to get the type.
@NirIzr: yes, i agree with you. That's not a pythonic way at all. but as you can see sys.exc_info()[2] object is an instance of the traceback object
1

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).

Comments

0

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

5 Comments

@NirIzr yes, this is answering your question, the traceback is defined in C code, sys.exc_info method is written in C too, it returned traceback object in C code.
sorry mate, it does not. I'm not asking about modifying the python interpreter. I wish to use features exposed to the python language.
@NirIzr hm.. sorry, I just saw your last " where this type is defined?" lol :) , didn't notice your signal requirement. But why do you want to creat Qt signal based on that? Do you want to pass full error stack to Qt? But this is not this question's scope
No worries, totally get the confusion. edited to make it clearer... I want to send a signal to a PyQt slot that will handle the exception in a different thread.
@NirIzr suggest define some error codes, and pass those codes to Qt slot. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.