Last active
March 23, 2017 21:47
-
-
Save mumumu/857c17f381dfa1976f7cf0694c7c8cc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import sys | |
# | |
# Note: This code works on Python2 only! | |
# | |
ignore_functions = [ | |
'write', # from print statement | |
'_remove', # from internal last cleanup | |
] | |
def trace_calls(frame, event, arg): | |
if event not in ['call', 'return']: | |
return | |
if not ignore_functions: | |
# on last cleanup, global variable may be already removed | |
return | |
co = frame.f_code | |
func_name = co.co_name | |
if func_name in ignore_functions: | |
return | |
func_line_no = frame.f_lineno | |
local_variables = frame.f_locals | |
if event == 'call': | |
func_filename = co.co_filename | |
caller = frame.f_back | |
caller_line_no = caller.f_lineno | |
caller_filename = caller.f_code.co_filename | |
print 'Call to %s on line %s of %s from line %s of %s with arg (%s)' % \ | |
(func_name, func_line_no, func_filename, | |
caller_line_no, caller_filename, local_variables) | |
return trace_calls # for return value can be monitored. | |
elif event == 'return': | |
print local_variables | |
print 'return from [%s] line %s => %s' % (func_name, func_line_no, arg) | |
return | |
class Hoge(object): | |
def __init__(self): | |
pass | |
def d(self, args): | |
return [args, args] | |
def b(): | |
def c(arg): | |
print "in c()" | |
hoge = Hoge() | |
hoge.d(arg) | |
return ['hoge', 'fuga', arg] | |
print 'in b()' | |
c(2) | |
def a(): | |
local_integers = [1, 2, 3] # noqa | |
print 'in a()' | |
b() | |
sys.settrace(trace_calls) | |
a() |
Author
mumumu
commented
Mar 23, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment