Git Product home page Git Product logo

goto-python's Introduction

Installation

Compatible with python 3.9 or newer.
tested with python 3.9.

pip install goto-label
  • goto-python only work with python 3.9, idk about 3.10.
  • use at your own risk, the author of this module is not responsible if there is a problem with your app.

Features

  • does not add unnecessary NOP instructions to the code.
  • automatically add push/pop block instructions if necessary.
    for example, if you jump out of for block, it automatically pop the iterator from the stack (as break does).

Limitations

  • can't jump into with, for, except, and finally block. but can jump out of it.

Syntax

  • goto .name jump to name.
  • label .name define label name.

Usage

1. as a decorator of a function

from goto import with_goto

@with_goto
def example():
  goto .end
  print("this will not print")
  label .end
  print("this will print")

example()

# output:
# this will print

2. patching code object

from goto import patch
import dis

# make code object
codestring = "goto .lbl; label .lbl"
code = compile(codestring, "<string>", "exec")

# patch code object
newcode = patch(code)

print("original:")
dis.dis(code)
print("modified:")
dis.dis(newcode)

# original:
#  1       0 LOAD_NAME            0 (goto)
#          2 LOAD_ATTR            1 (lbl)
#          4 POP_TOP
#          6 LOAD_NAME            2 (label)
#          8 LOAD_ATTR            1 (lbl)
#         10 POP_TOP
#         12 LOAD_CONST           0 (None)
#         14 RETURN_VALUE
# modified:
#  1       0 JUMP_ABSOLUTE        2
#    >>    2 LOAD_CONST           0 (None)
#          4 RETURN_VALUE

Examples of good gotos in python (IMO)

labeled break/continue

for _ in ...:
  for _ in ...:
    if should_break:
      goto .br  # break outer loop
label .br
for _ in ...:
  for _ in ...:
    if should_continue:
      goto .con  # continue outer loop
  label .con

this module brandtbucher/hax, can do the same job as goto-python does, but it requires you to have basic knowledge of python bytecode.

goto-python's People

Contributors

alimsk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

goto-python's Issues

Support for Python 3.10?

Is there a timeline on support for Python 3.10?

I have found this module to be extremely useful for quick porting of old and poorly coded Fortran to python, but I am stuck with Python 3.9, would be great to see support for >= 3.10

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.