Created
September 18, 2017 07:47
-
-
Save georgexsh/ede5163a294ced53c3e2369ccaa392cc to your computer and use it in GitHub Desktop.
python goto with system trace function
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
import sys | |
def j(lineno): | |
frame = sys._getframe().f_back | |
called_from = frame | |
def hook(frame, event, arg): | |
if event == 'line' and frame == called_from: | |
try: | |
frame.f_lineno = lineno | |
except ValueError as e: | |
print "jump failed:", e | |
while frame: | |
frame.f_trace = None | |
frame = frame.f_back | |
return None | |
return hook | |
while frame: | |
frame.f_trace = hook | |
frame = frame.f_back | |
sys.settrace(hook) | |
def foo(): | |
a = 1 | |
j(30) | |
a = 2 | |
print 1 | |
print 2 | |
if a == 1: | |
j(28) | |
print 4 | |
foo() |
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
2 | |
1 | |
2 | |
4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a reworking of the version above that adds labels, that uses the bytecode dissassembler rather than re-parsing a file that may have changed: