semvec/pypi-packages huggingface dataset:
gotopy 0.2.1
python goto statement
Requires Python: >=3.6

# Installation
NOTE: make sure you are using python 3.6 or above
```
pip install gotopy
```

### Supported Python Version
requires version 3.6 or above since it uses 2 bytes for each instruction.\
from the [doc](https://docs.python.org/3/library/dis.html):
> Changed in version 3.6: Use 2 bytes for each instruction. Previously the number of bytes varied by instruction.

# Usage
first, add the `with_goto` decorator to your function:
```py
from goto import with_goto

@with_goto
def nothing():
  goto .end
  print("this will not print")
  label .end
```
for basic usage:
- use `label .NAME` syntax to define a label.
- use `goto .NAME` syntax to go to a label.

there are several different usage, see below

## Syntax
`gotopy` support 3 different syntax:
Name       | Example
---------- | ------------
goto label | `goto .name`
skip lines | `goto >>3`
goto line  | `goto \|31`

the "goto label" is the most recommended and safest than others.\
use another syntax only if you really need it.

### Goto Label
example:
```py
@with_goto
def x():
  goto .end
  print("this will not print")
  label .end
```
it simply jumps to the label.

### Skip Lines
example:
```py
@with_goto
def x():
  goto >>3
  print(1)  # <- skipped
  print(2)  # <- skipped
            # <- skipped, empty line also count
  print(3)
```
this will print the number 3.

### Goto Line
number on the left is a line number.
```py
1 | import ...
2 | from ... import ...
3 |
4 |
5 | @with_goto
6 | def x():
7 |   goto |9   # jump to line 9
8 |   print(1)
9 |   print(2)
10|   goto |8   # go back to line 8
```
Note: infinite loop.

with this, you can jump to absolute line, inside the function.\
if you try to jump out the function, it will just give an error.

### why is there a `goto line` and `skip line`
well, i don't think it will be useful. but i was bored, so i made it lol.

### the patch function
<details>

the `with_goto` decorator is just a wrapper for this function,\
you can patch any code object apart from function.

to be able to use `goto line` and `skip line`,\
you need to properly include the following information in the code object:
- `co_firstlineno`
- `co_lnotab`

the both statements depend on them.
</details>

# Note
keep in mind that everything below `return` is not included in bytecode, as in the following code:
```py
>>> def x():
...     return
...     print("hello")
... 
>>> dis.dis(x)
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
>>> 
```

that's why `goto` doesn't work well in this case:
```py
@with_goto
def x():
  goto .second_return   # NameError
  return 1
  label .second_return  # <- not included in bytecode
  return 2              # <-
```
will produce the following error:
```py
NameError: label 'second_return' not defined in this function
```

# Thanks
this project was inspired by [snoack/python-goto](https://github.com/snoack/python-goto)