Last active
April 29, 2021 02:14
-
-
Save paoloo/2b8211803aeff4d4f13f362f403d4e1b to your computer and use it in GitHub Desktop.
python3 version of the bizarre goto in python
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 goto(lineno: int) -> None: | |
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(f'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) |
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
from goto import goto | |
def gogo(): | |
a = 1 | |
goto(9) | |
a = 2 | |
print("1") | |
print("2") | |
if a == 1: | |
goto(7) | |
print("4") | |
if __name__=='__main__': | |
gogo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment