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.
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?
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 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 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.
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 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 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 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.
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
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/
CPython gives you all the necessary utilities to let you freely manipulate pretty much any function's bytecode.
This Gist is still super cool, though.
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.
I wasted a lot of time with C/C++, if I had immersed myself in QBasic, I would have better actualized myself.
https://github.com/python/cpython/blob/3.8/Lib/traceback.py
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.