Python – sys.settrace()
Last Updated :
21 Apr, 2020
Python sys
module provides some of the powerful functions but they are complex to understand. One of which is the sys.settrace()
which is used for implementing debuggers, profilers and coverage tools. This is thread-specific and must register the trace using threading.settrace()
.
Knowing how the function works internally is really important in case you are planning to create your own debugger.
On a higher level, sys.settrace()
registers the traceback to the Python interpreter. A traceback is basically the information that is returned when an event happens in the code. You might have seen traceback
when your code has some error or an exception is raised.
The registered traceback is invoked when one of the following four events occur :
- Function is called
- Function returns
- Execution of a line
- Exception is raised
Syntax: sys.settrace(frame, event, arg.frame)
Parameters:
frame: frame is the current stack frame
event: A string which be either 'call', 'line', 'return', 'exception'
or 'opcode'
arg: Depends on the event type
Returns: Reference to the local trace function which then returns reference to itself.
Example:
Let’s create our own local trace function with line, function and call
events. These events are highlighted in the below given code.
from sys import settrace
def my_tracer(frame, event, arg = None ):
code = frame.f_code
func_name = code.co_name
line_no = frame.f_lineno
print (f"A {event} encountered in \
{func_name}() at line number {line_no} ")
return my_tracer
def fun():
return "GFG"
def check():
return fun()
settrace(my_tracer)
check()
|
Output:
A call encountered in check() at line number 30
A line encountered in check() at line number 31
A call encountered in fun() at line number 24
A line encountered in fun() at line number 25
A return encountered in fun() at line number 25
A return encountered in check() at line number 31
You might be wondering why does the local function my_code
returns itself?
The reason is hidden in the functioning of sys.settrace()
. What sys.settrace
does is that it first registers a global trace which is called whenever a frame is created which returns our local trace function my_trace
whenever any one of the above mentioned event occurs. For better understanding look at the image shown below:
If we don’t want our scope to be traced then None
should be returned but if that is not the case we might want to do the same with our local trace function then it should return None
else if there is an error then settrace(None)
is automatically called.
Note : There is a function named gettrace
available in sys
module to get the trace set by sys.settrace()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...