News for hackers. Stuff that matters.
Re:Python goto implemented with system trace function (2017) by CivBase on Tuesday May 05, 2020 @11:10AM (#23080850)
This uses sys._getframe()[0], a private function which appears to be officially supported by CPython but is not guaranteed to exist in other interpreters. Needless to say, you probably shouldn't use this in any serious capacity, even if you're using CPython.

That said... I wonder what it would take to add support for labels. Jumping back to a previous location would be trivial; just create a sister function to associate the line with some unique string. But is there any way to register a label for a line that hasn't been executed?

I know CPython normally precompiles modules instead of literally interpreting them line-by-line, but I've never seen any code that interfaces with the precompiler itself. Does CPython support anything like preprocessor commands that might make it possible to support future labels?

You could easily wrap the interpreter with a custom preprocessor, but that's boring.

[0] https://docs.python.org/3/library/sys.html#sys._getframe

Re:Python goto implemented with system trace function (2017) by kzrdude on Tuesday May 05, 2020 @11:29AM (#23081072)
Python being python, can't you swap out the import machinery and change which compile function is called, alternatively call your own preprocessor before compiling?
Re:Python goto implemented with system trace function (2017) by rcxdude on Tuesday May 05, 2020 @12:40PM (#23081925)
Yup. You can basically write your own language with different syntax and get python to treat it like any other python code. Hy is a language which works this way: https://github.com/hylang/hy (see this part of their documentation for how the user can use this: https://docs.hylang.org/en/master/language/interop.html#usin...)

I think goto has been implemented in a few different ways in python. Here's another example which abuses existing syntax and edits the bytecode of the function after its compiled: https://pypi.org/project/goto-statement/

Re:Python goto implemented with system trace function (2017) by sitkack on Wednesday May 06, 2020 @01:41AM (#23088188)
Re:Python goto implemented with system trace function (2017) by tom_mellior on Tuesday May 05, 2020 @02:52PM (#23083421)
With a decorator you can parse and rewrite the function's bytecode. If it helps, I'm fairly sure you could also ask for the function's abstract syntax tree, rewrite that, and ask for it to be compiled. So you could build something, but every function using such gotos would have to be decorated with @gotos or whatever.
Re:Python goto implemented with system trace function (2017) by fezzez on Tuesday May 05, 2020 @07:21PM (#23086253)
^To add to that. I'm doing exactly that to build this tool that lets you add logging statements onto running python without any restarts.

CPython gives you all the necessary utilities to let you freely manipulate pretty much any function's bytecode.

Re:Python goto implemented with system trace function (2017) by nathancahill on Tuesday May 05, 2020 @08:00AM (#23079068)
I had to click Report Abuse on this Gist.
Re:Python goto implemented with system trace function (2017) by pjc50 on Tuesday May 05, 2020 @10:46AM (#23080550)
Now do the COME FROM function from INTERCAL.
Re:Python goto implemented with system trace function (2017) by pmiller2 on Tuesday May 05, 2020 @03:11PM (#23083637)
http://entrian.com/goto/

    from goto import comefrom
Re:Python goto implemented with system trace function (2017) by dec0dedab0de on Tuesday May 05, 2020 @01:53PM (#23082745)
I tried to read through this at least 4 times today, and my brain keeps going between "Woah thats neat!" and "WTF JUST DONT DO THAT!"
Re:Python goto implemented with system trace function (2017) by google234123 on Tuesday May 05, 2020 @08:47AM (#23079421)
goto to a line number is not the very nice... adding a new line will mess up all the previous gotos
Re:Python goto implemented with system trace function (2017) by seisvelas on Tuesday May 05, 2020 @09:47AM (#23079957)
You're getting downvotes from the people who don't know that you are 100% right: goto is usually with labels, not line number, for exactly the reason you point out.

This Gist is still super cool, though.

Re:Python goto implemented with system trace function (2017) by pjc50 on Tuesday May 05, 2020 @10:47AM (#23080574)
Further back, BASIC lets you go to a line number .. but the line numbers are explicit and not required to be consecutive. In fact you'd routinely leave holes in the numbering in order to insert an extra line without breaking it. Better systems offered a "renumber" function. Others .. didn't.
Re:Python goto implemented with system trace function (2017) by seisvelas on Tuesday May 05, 2020 @11:16AM (#23080907)
>BASIC lets you go to a line number

QBasic let you use either a line number or a label, I believe they only kept line numbers after labels were introduced for backwards compatibility.

I know I'm veering way off topic but for as much disdain as BASICs receive, I really do feel like coding up a simple game was much easier for teenage-me in QBasic than it was in Python with pygame, for whatever reason.

Re:Python goto implemented with system trace function (2017) by pjc50 on Tuesday May 05, 2020 @11:37AM (#23081173)
I think a lot of people have reported having that experience, and I think it's been deeply under-investigated by programming language researchers.
Re:Python goto implemented with system trace function (2017) by jbay808 on Tuesday May 05, 2020 @01:58PM (#23082806)
My first programming experience was QBASIC. Even if it's not a good programming pattern, I think GOTO is a great way to let a novice programmer "discover" the idea of loops and functions for themselves. Although it's error prone, it's still very intuitive for someone with no programming background.
Re:Python goto implemented with system trace function (2017) by sitkack on Wednesday May 06, 2020 @01:45AM (#23088208)
QBasic is actually a really awesome language, I am bummed I didn't embrace it like I should have. I ended up going C/C++ ... Perl ... Python.

I wasted a lot of time with C/C++, if I had immersed myself in QBasic, I would have better actualized myself.

Re:Python goto implemented with system trace function (2017) by xashor on Tuesday May 05, 2020 @11:03AM (#23080749)

  LABEL = sys._getframe().f_lineno
  print(1)
  j(LABEL)
Re:Python goto implemented with system trace function (2017) by CivBase on Tuesday May 05, 2020 @11:14AM (#23080888)
Past labels are easy, but what about future labels?

  print(1)
  goto('MYLABEL')
  print(2)
  label('MYLABEL')
  print(3)
Or better yet...

  for x in range(10):
      for y in range(10):
          if x == 5 and y == 5:
              print(x, y)
              goto('MULTIBREAK')
  
  label('MULTIBREAK')
Re:Python goto implemented with system trace function (2017) by calibwam on Tuesday May 05, 2020 @08:57AM (#23079501)
I consider this harmful.
Re:Python goto implemented with system trace function (2017) by DSingularity on Tuesday May 05, 2020 @07:09AM (#23078711)
So cute. What are some use-cases for these reflective features?
Re:Python goto implemented with system trace function (2017) by captainmuon on Tuesday May 05, 2020 @08:25AM (#23079227)
I once implemented the "command pattern" in order to have undo in my app. Basically you have to ensure that you only modify your model from command objects, and never directly from UI callbacks. To ensure this, I created a decorator that walked the stack, and asserted that the call originated in a subclass of `Command`. Then I wrapped all setters on the model with this decorator. This was really helpful during development for weeding out cases where Undo didn't work correctly. In the release, you could disable the check so there would be no performance penalty.
Re:Python goto implemented with system trace function (2017) by oefrha on Tuesday May 05, 2020 @07:36AM (#23078901)
One obvious use case invoked all the time: tracebacks are generated through introspection.

https://github.com/python/cpython/blob/3.8/Lib/traceback.py

Re:Python goto implemented with system trace function (2017) by riazrizvi on Tuesday May 05, 2020 @09:25AM (#23079776)
Prototyping condensed state machines. Probably not the code you'd leave in the release version, but can provide an elegant form of experimentation.
Re:Python goto implemented with system trace function (2017) by amelius on Tuesday May 05, 2020 @07:18AM (#23078765)
Transpiled code?
Re:Python goto implemented with system trace function (2017) by varelaz on Tuesday May 05, 2020 @02:44PM (#23083327)
It's fairly easy to implement goto using exceptions. At least it will be all pythonic (I don't think it will become less ugly). So I don't know why this implementation has to be considered at all.

How to do this: create your ExceptionGoTo class which will be handled on high level function and add callback as exception parameter, in catch you can call it.

I'm not saying that it will be exactly goto to certain line, but logically it will behave as you suppose to use goto.

Re:Python goto implemented with system trace function (2017) by josh_fyi on Tuesday May 05, 2020 @01:19PM (#23082378)
Github has a "Report abuse" button. I really need to press it :-)