Skip to content

Instantly share code, notes, and snippets.

@georgexsh
Created September 18, 2017 07:47
Show Gist options
  • Save georgexsh/ede5163a294ced53c3e2369ccaa392cc to your computer and use it in GitHub Desktop.
Save georgexsh/ede5163a294ced53c3e2369ccaa392cc 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()
@satishgoda
Copy link

Great :) Going to study this. Thank you.

@toombs-caeman
Copy link

I added labels lol

import ast
import sys
def label(name):
    pass

def j(lineno):
    frame = sys._getframe().f_back
    called_from = frame

    if isinstance(lineno, str):
        with open(called_from.f_code.co_filename) as f:
            for node in ast.walk(ast.parse(f.read())):
                if isinstance(node, ast.Call) \
                    and isinstance(node.func, ast.Name) \
                    and node.func.id == 'label' \
                    and lineno == ast.literal_eval(node.args[0]):
                   lineno = node.lineno

    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('l1')
    label('l2')
    a = 2
    print 1
    label('l1')
    print 2
    if a == 1:
        j('l2')
    print 4


foo()

@hazkaz
Copy link

hazkaz commented May 5, 2020

could someone explain what the hook function is for and who calls it?

@ropg
Copy link

ropg commented May 5, 2020

Meh... "goto" is already taken on PyPI... :(

@jsbueno-geru
Copy link

The "goto" on pypi follows the same idea, and was made for an April first a couple years ago. It is actually "production quality" and implements labels and a comefrom statement as well as the goto.

@stuaxo
Copy link

stuaxo commented May 5, 2020

Can this at GOSUB too ?

@onequbit
Copy link

onequbit commented May 5, 2020

How about making a BASIC interpreter, and then a transpiler to convert Python code into BASIC?
Yo DAWG! Now you can throw up in your mouth while you die a little inside!

@Kyuuhachi
Copy link

I made a py3 port. This one uses relative line numbers though.

@shakna-israel
Copy link

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:

import sys
import dis

def label(name):
    pass

def j(lineno):
    frame = sys._getframe().f_back
    called_from = frame

    if isinstance(lineno, str):
        # Dissassemble our byte code to find and create our labels:
        bc = dis.Bytecode(called_from.f_code)
        labels = {}
        SET_LABEL = False
        LABEL_LINE = None
        for idx, instr in enumerate(bc):
            if instr.opname == 'LOAD_GLOBAL':
                if instr.argval == 'label':
                    SET_LABEL = True
                    LABEL_LINE = instr.starts_line
            if instr.opname == 'LOAD_CONST' and SET_LABEL:
                label_name = instr.argval
                labels[label_name] = LABEL_LINE

                SET_LABEL = False
                LABEL_LINE = None

        try:
            lineno = labels[lineno]
        except KeyError:
            raise RuntimeError("Unknown Label: {}".format(lineno))

    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('l1')
    label('l2')
    a = 2
    print(1)
    label('l1')
    print(2)
    if a == 1:
        j('l2')
    print(4)


foo()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment