Skip to main content

A function decorator, that rewrites the bytecode, to enable goto in Python

Project description

Installation

make sure you are using python 3.9

pip install -U goto-label

NOTE: support for python >=3.10 is very unstable, please run the test before using.

Usage

a simple example:

from goto import with_goto
from goto import goto, label  # optional, for linter purpose

@with_goto
def x():
    goto .end
    print("this will not print")
    label .end
    print("this will print")
  • use label .NAME to define a label.
  • use goto .NAME to goto into a label.

Limitation

@with_goto
def x():
    try:
        pass
    finally:
        label .a

here we define the label "a" once. but if you run the code:

SyntaxError: ambiguous label name: 'a'. at line 10

it's not a bug, but why is it?

let's try to disassemble the code without modifying it:

  7           0 SETUP_FINALLY           10 (to 12)

  8           2 POP_BLOCK

 10           4 LOAD_GLOBAL              0 (label)
              6 LOAD_ATTR                1 (a)
              8 POP_TOP
             10 JUMP_FORWARD             8 (to 20)
        >>   12 LOAD_GLOBAL              0 (label)
             14 LOAD_ATTR                1 (a)
             16 POP_TOP
             18 RERAISE
        >>   20 LOAD_CONST               0 (None)
             22 RETURN_VALUE

because the finally block is copy pasted by python.
look at the instructions, there are two definitions of label "a".

Thanks

this project was inspired by snoack/python-goto .
since the project seems to have been discontinued, I created this.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

goto-label-0.1.2.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

goto_label-0.1.2-py3-none-any.whl (6.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page