Last active
September 5, 2017 21:20
-
-
Save Upabjojr/3bbcfd9c3fb9130cdc29e786fe4ac674 to your computer and use it in GitHub Desktop.
Detect difference in script execution
This file contains hidden or 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
import sys | |
import multiprocessing | |
import collections | |
import inspect | |
def f(x): | |
print("f function entered.") | |
a = x + 10 | |
print("Value of a: ", a) | |
return 2*x | |
def g(x): | |
print("g function entered.") | |
return f(x) + f(x+1) | |
class ControlledExecution(object): | |
def __init__(self, callable_var, args, queue_to, queue_from): | |
self.queue_to = queue_to | |
self.queue_from = queue_from | |
self.callable_var = callable_var | |
self.proc = multiprocessing.Process(target=self.on_process_start, args=args) | |
def local_trace_line(self, frame, event, arg): | |
return self.trace_dispatcher | |
def local_trace_call(self, frame, event, arg): | |
return self.trace_dispatcher | |
def local_trace_return(self, frame, arg): | |
a = raw_input("Quit?") | |
if a == "y": | |
raise ValueError("quit") | |
return self.trace_dispatcher | |
def put_on_queue_from(self, frame, event, arg): | |
#print frame.f_exc_traceback.tb_lineno | |
if "/usr" not in frame.f_code.co_filename and False: | |
for i in frame.__class__.__dict__.keys(): | |
if "builtin" in i: | |
continue | |
if "globals" in i: | |
continue | |
print i, getattr(frame, i) | |
for i in frame.f_code.__class__.__dict__.keys(): | |
print "f_code\t", i, getattr(frame.f_code, i) | |
frame_dict = dict( | |
co_filename=frame.f_code.co_filename, | |
source_lines=inspect.getsourcelines(frame), | |
f_lineno=frame.f_lineno, | |
f_locals=frame.f_locals, | |
) | |
self.queue_from.put((frame_dict, event, arg)) | |
def trace_dispatcher(self, frame, event, arg): | |
cmd = self.queue_to.get() | |
self.put_on_queue_from(frame, event, arg) | |
if event == 'line': | |
return self.local_trace_line | |
if event == 'call': | |
return self.local_trace_call | |
if event == "return": | |
return self.local_trace_return | |
if event == 'exception': | |
return self.trace_dispatcher | |
if event == 'c_call': | |
return self.trace_dispatcher | |
if event == 'c_exception': | |
return self.trace_dispatcher | |
if event == 'c_return': | |
return self.trace_dispatcher | |
raise Exception("no %s" % event) | |
def on_process_start(self, *args, **kwargs): | |
sys.settrace(self.trace_dispatcher) | |
return self.callable_var(*args, **kwargs) | |
def start(self): | |
self.proc.start() | |
def start(callable_var, args): | |
q1_to = multiprocessing.Queue() | |
q2_to = multiprocessing.Queue() | |
q1_from = multiprocessing.Queue() | |
q2_from = multiprocessing.Queue() | |
p1 = ControlledExecution(callable_var=callable_var, args=args, queue_to=q1_to, queue_from=q1_from) | |
p2 = ControlledExecution(callable_var=callable_var, args=args, queue_to=q2_to, queue_from=q2_from) | |
p1.start() | |
p2.start() | |
for i in range(20): | |
q1_to.put(1) | |
q2_to.put(1) | |
frame_dict1, event1, arg1 = q1_from.get() | |
frame_dict2, event2, arg2 = q2_from.get() | |
if "/usr/" in frame_dict1["co_filename"]: #.f_code.co_filename: | |
continue | |
print "Event:\t", event1, "\targ:\t", arg1 | |
print frame_dict1["co_filename"] | |
#print frame.f_code.co_filename | |
#print "".join(inspect.getsourcelines(frame)[0]) | |
#print inspect.getsourcelines(frame)[1] | |
print frame_dict1["source_lines"] | |
print frame_dict1["f_lineno"] | |
print frame_dict1["f_locals"] | |
print frame_dict2["f_locals"] | |
#print(frame, event, arg) | |
#print(frame.f_lineno) | |
print "=="*20 | |
start(g, args=(2,)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment