stack-trace

Is there a Python library that shows execution time per line?

Is there a Python library that shows execution time per line? Question: I have found this library called heartrate which shows how many times each line has been hit. I’m wondering if there is a similar library that can show the execution time of each line? Sometimes a single line can be a bottleneck of …

Total answers: 2

py.test Tracebacks: Highlight my code, fold frames of framework

py.test Tracebacks: Highlight my code, fold frames of framework Question: Tracebacks of my tests are too long since I use pytest. Pytests includes surrounding code lines and a lot of other information. I like to see this information if the traceback line (frame) is from my code. But I don’t want to see it, if …

Total answers: 3

e.printStackTrace equivalent in python

e.printStackTrace equivalent in python Question: I know that print(e) (where e is an Exception) prints the occurred exception but, I was trying to find the python equivalent of Java’s e.printStackTrace() that exactly traces the exception to what line it occurred and prints the entire trace of it. Could anyone please tell me the equivalent of …

Total answers: 5

Re-raise Python exception and preserve stack trace

Re-raise Python exception and preserve stack trace Question: I’m trying to catch an exception in a thread and re-raise it in the main thread: import threading import sys class FailingThread(threading.Thread): def run(self): try: raise ValueError(‘x’) except ValueError: self.exc_info = sys.exc_info() failingThread = FailingThread() failingThread.start() failingThread.join() print failingThread.exc_info raise failingThread.exc_info[1] This basically works and yields the …

Total answers: 2

Get exception description and stack trace which caused an exception, all as a string

Get exception description and stack trace which caused an exception, all as a string Question: How to convert a caught Exception (its description and stack trace) into a str for external use? try: method_that_can_raise_an_exception(params) except Exception as e: print(complete_exception_description(e)) Asked By: bluish || Source Answers: See the traceback module, specifically the format_exc() function. Here. import …

Total answers: 12

print python stack trace without exception being raised

print python stack trace without exception being raised Question: Something is happening with one of my class’s instance variables. I want to make the variable a property, and whenever it is accessed I want to print out the stack trace of all the code leading up to that point, so I can see where it’s …

Total answers: 3

When I catch an exception, how do I get the type, file, and line number?

When I catch an exception, how do I get the type, file, and line number? Question: Catching an exception that would print like this: Traceback (most recent call last): File “c:/tmp.py”, line 1, in <module> 4 / 0 ZeroDivisionError: integer division or modulo by zero I want to format it into: ZeroDivisonError, tmp.py, 1 Asked …

Total answers: 7

Print current call stack from a method in code

Print current call stack from a method in code Question: In Python, how can I print the current call stack from within a method (for debugging purposes). Asked By: oneself || Source Answers: Here’s an example of getting the stack via the traceback module, and printing it: import traceback def f(): g() def g(): for …

Total answers: 7

Get __name__ of calling function's module in Python

Get __name__ of calling function's module in Python Question: Suppose myapp/foo.py contains: def info(msg): caller_name = ???? print ‘[%s] %s’ % (caller_name, msg) And myapp/bar.py contains: import foo foo.info(‘Hello’) # => [myapp.bar] Hello I want caller_name to be set to the __name__ attribute of the calling functions’ module (which is ‘myapp.foo’) in this case. How …

Total answers: 4

Showing the stack trace from a running Python application

Showing the stack trace from a running Python application Question: I have this Python application that gets stuck from time to time and I can’t find out where. Is there any way to signal Python interpreter to show you the exact code that’s running? Some kind of on-the-fly stacktrace? Related questions: Print current call stack …

Total answers: 29