forked from bat67/The-Python-Standard-Library-by-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_settrace_call.py
More file actions
41 lines (32 loc) · 885 Bytes
/
Copy pathsys_settrace_call.py
File metadata and controls
41 lines (32 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# encoding: utf-8
import sys
def trace_calls(frame, event, arg):
if event != 'call':
return
co = frame.f_code
func_name = co.co_name
if func_name == 'write':
# Ignore write() calls from printing
return
func_line_no = frame.f_lineno
func_filename = co.co_filename
if not func_filename.endswith('sys_settrace_call.py'):
# Ignore calls not in this module
return
caller = frame.f_back
caller_line_no = caller.f_lineno
caller_filename = caller.f_code.co_filename
print('* Call to', func_name)
print('* on line {} of {}'.format(
func_line_no, func_filename))
print('* from line {} of {}'.format(
caller_line_no, caller_filename))
return
def b():
print('inside b()\n')
def a():
print('inside a()\n')
b()
sys.settrace(trace_calls)
a()