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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file goto-label-0.1.2.tar.gz.

File metadata

  • Download URL: goto-label-0.1.2.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for goto-label-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4b6a4a692a05b76ebc45a97368da3de03b51e267694707593f07d46c8e9b90dd
MD5 921f10503453f21b74f79a90cb8c1ada
BLAKE2b-256 f03f0b77b3763e1158db2a24226bca0694849c125284beae806be80158420dce

See more details on using hashes here.

File details

Details for the file goto_label-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: goto_label-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for goto_label-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 28b9f89d309930d741fedc7fd5694518b68947121643c0bae58b00ef58b93c8f
MD5 b14dd48515317dc58ea81ad3f556e21d
BLAKE2b-256 b539e5de82a320b19ad9abc863d8b7c89c4ee1cc3a088376822a5c3535cd0c01

See more details on using hashes here.

Supported by

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