Uncategorized

Python Debugging: Get Current Executed Function Name and Print Traceback



In Python, we can fail a test like this:

1
2
3
4
5
6
def fail_the_test(error_msg):
    print(error_msg)
    sys.stdout.flush()
    sys.stdout.close()
    # this kills the process and make it return a non-zero code
    os.kill(os.getpid(), signal.SIGKILL)
def fail_the_test(error_msg):
    print(error_msg)
    sys.stdout.flush()
    sys.stdout.close()
    # this kills the process and make it return a non-zero code
    os.kill(os.getpid(), signal.SIGKILL)

At this point, we can collect more information to help debugging.

How to Get the Name of the Current Running Function in Python?

To get the name of the currently running function in Python, you can use the inspect module. Here’s a simple example that demonstrates how to do this:

1
2
3
4
5
6
7
8
9
import inspect
 
def get_current_function_name():
    return inspect.currentframe().f_back.f_code.co_name
 
def demo_function():
    print("Inside:", get_current_function_name())
 
demo_function()
import inspect

def get_current_function_name():
    return inspect.currentframe().f_back.f_code.co_name

def demo_function():
    print("Inside:", get_current_function_name())

demo_function()

In this example, when demo_function is called, it will use get_current_function_name to find the name of the function that called it, which is demo_function in this case. The inspect.currentframe().f_back provides the frame object one level up in the stack (the caller of the current function), and f_code.co_name gives the name of that function.

How to print the current calling stack in Python?

To print the current calling stack in Python, you can use the traceback module along with the traceback.print_stack() function. This function provides a way to print the stack trace from the point where it is called. Here is an example of how you can use it:

1
2
3
4
5
6
7
8
9
10
11
12
import traceback
 
def function1():
    function2()
 
def function2():
    function3()
 
def function3():
    traceback.print_stack()  # This will print the current call stack
 
function1()
import traceback

def function1():
    function2()

def function2():
    function3()

def function3():
    traceback.print_stack()  # This will print the current call stack

function1()

In this example, when function3 is called, it uses traceback.print_stack() to print the entire call stack leading up to that point. This will include the series of function calls from function1 to function2 to function3.

For example, the above sample code prints the following tracebacks:

File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in function1
  File "<stdin>", line 2, in function2
  File "<stdin>", line 2, in function3
</module>

We can append the tracebacks to file like this:

1
2
with open('stack_trace.txt', 'a') as f:  # Open the file in append mode
    traceback.print_stack(file=f)  # Append the stack trace to the file
with open('stack_trace.txt', 'a') as f:  # Open the file in append mode
    traceback.print_stack(file=f)  # Append the stack trace to the file

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading…

503 words
Last Post: Teaching Kids Programming – Remove Duplicates from Sorted Linked List (Two Pointer Algorithm)





Source link

Leave a Reply

Your email address will not be published. Required fields are marked *