forked from nat/natalieee.net
add blog posts: dollcode, python-goto
This commit is contained in:
27
www/src/data/thoughts/python-goto/goto-def.py
Normal file
27
www/src/data/thoughts/python-goto/goto-def.py
Normal file
@ -0,0 +1,27 @@
|
||||
_goto = type('goto', (object,), dict(__and__=lambda _, other: (_goto(other.line))))
|
||||
|
||||
goto = _goto()
|
||||
|
||||
def _goto(lineno):
|
||||
frame = sys._getframe().f_back.f_back
|
||||
called_from = frame
|
||||
|
||||
def hook(frame, event, _):
|
||||
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
|
||||
|
||||
# it doesn't think we actually need to set the hook for each frame
|
||||
# while frame:
|
||||
# frame.f_trace = hook
|
||||
# frame = frame.f_back
|
||||
|
||||
called_from.f_trace = hook
|
||||
sys.settrace(hook)
|
Reference in New Issue
Block a user