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
Release history Release notifications | RSS feed
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)
Built Distribution
Close
Hashes for goto_label-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28b9f89d309930d741fedc7fd5694518b68947121643c0bae58b00ef58b93c8f |
|
MD5 | b14dd48515317dc58ea81ad3f556e21d |
|
BLAKE2b-256 | b539e5de82a320b19ad9abc863d8b7c89c4ee1cc3a088376822a5c3535cd0c01 |