Skip to content

Instantly share code, notes, and snippets.

@crisidev
Forked from georgexsh/goto.py
Created May 5, 2020 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisidev/e8db048b1b3c3b5af4d0d244ee4439ea to your computer and use it in GitHub Desktop.
Save crisidev/e8db048b1b3c3b5af4d0d244ee4439ea to your computer and use it in GitHub Desktop.
python goto with system trace function
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment