Created
March 25, 2019 02:19
-
-
Save tonybaloney/ee00e7a2807d82b32e80fdbf969e7482 to your computer and use it in GitHub Desktop.
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 dis | |
import traceback | |
import io | |
def t(frame, event, args): | |
frame.f_trace_opcodes=True | |
stack = traceback.extract_stack(frame) | |
pad = " "*len(stack) + "|" | |
if event == 'opcode': | |
with io.StringIO() as out: | |
dis.disco(frame.f_code, frame.f_lasti, file=out) | |
lines = out.getvalue().split('\n') | |
[print(f"{pad}{l}") for l in lines] | |
elif event == 'call': | |
print(f"{pad}Calling {frame.f_code}") | |
elif event == 'return': | |
print(f"{pad}Returning {args}") | |
elif event == 'line': | |
print(f"{pad}Changing line to {frame.f_lineno}") | |
else: | |
print(f"{pad}{frame} ({event} - {args})") | |
print(f"{pad}----------------------------------") | |
return t | |
sys.settrace(t) | |
# eval some code for demo | |
eval('"-".join([letter for letter in "hello"])') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment