How to imitate Python 3’s raise … from in Python 2?

There’s a raise_from in python-future; simply install it pip install future and import to use from future.utils import raise_from # or: from six import reraise as raise_from class FileDatabase: def __init__(self, filename): try: self.file = open(filename) except IOError as exc: raise_from(DatabaseError(‘failed to open’), exc) UPDATE The compatibility package six also supports raise_from, from version 1.9 … Read more

How to format traceback objects in Python

format_exc() is really just etype, value, tb = sys.exc_info() return ”.join(format_exception(etype, value, tb, limit)) So if you have the exception type, value, and traceback ready, it should be easy. If you have just the exception, notice that format_exception() is essentially: a_list = [‘Traceback (most recent call last):\n’] a_list = a_list + format_tb(tb, limit) where limit … Read more

Get full traceback

As mechmind answered, the stack trace consists only of frames between the site where the exception was raised and the site of the try block. If you need the full stack trace, apparently you’re out of luck. Except that it’s obviously possible to extract the stack entries from top-level to the current frame—traceback.extract_stack manages it … Read more

python: Is there a downside to using faulthandler?

This feature is very useful. Is there any particular reason it isn’t enabled by default? Does it have any negative effects on performance? The reason is that faulthandler remembers the file descriptor of stderr, usually fd 2. The problem is that fd 2 may become something else, like a socket, a pipe, an important file, … Read more

What are the python builtin __exit__ argument types?

exc_type is the exception’s class. exc_val is the exception instance. exc_tb is a traceback object, of which there is a reference in types.TracebackType. In general it should be the case that type(exc_val) is exc_type exc_val.__traceback__ is exc_tb Note that __exit__ is still invoked when there was no exception raised by the code under a context … Read more

techhipbettruvabetnorabahisbahis forumueduseduedusedueduseduseduseduedusedu