News for hackers. Stuff that matters.
Re:Goto decorator for python by rmc on Sunday March 11, 2012 @06:51PM (#3691513)
Obviously goto is a bad idea. However this code implies it's possible to add new syntax to Python from within Python. That is pretty cool.
Re:Goto decorator for python by haberman on Sunday March 11, 2012 @10:17PM (#3692076)
"Obviously" goto is bad, but to quote one of Lua's designers, continuations are much worse, and yet it's considered "cool" to support continuations. It's programmer groupthink; one very smart person writes a famous article about how goto is bad and forevermore it's "obviously" bad. Goto is sometimes useful, and like lots of things it can be abused.
Re:Goto decorator for python by swah on Monday March 12, 2012 @11:05AM (#3693756)
I can't imagine a clearer way of treating errors in C than the macros from Zed Shaw, which use goto (http://c.learncodethehardway.org/book/learn-c-the-hard-waych...):

        int test_check(char *file_name)
        34  {
        35      FILE *input = NULL; 
        36      char *block = NULL;
        37  
        38      block = malloc(100); 
        39      check_mem(block); // should work
        40  
        41      input = fopen(file_name,"r"); 
        42      check(input, "Failed to open %s.", file_name);
        43  
        44      free(block); 
        45      fclose(input);
        46      return 0;
        47  
        48  error:
        49      if(block) free(block); 
        50      if(input) fclose(input);
        51      return -1;
        52  }
Re:Goto decorator for python by haberman on Monday March 12, 2012 @12:34PM (#3694196)
Yes, the Linux kernel has used this error handling strategy for a long time (http://kerneltrap.org/node/553/2131). A variant on this is to have multiple error labels so that you uninitialize the correct set of things without the if statements (which may not be possible in cases where the resource is represented by an opaque handle).
Re:Goto decorator for python by edanm on Monday March 12, 2012 @06:52AM (#3692978)
And the reason the smart person decided Goto was bad has little relevance on programming today - he showed that Goto made it harder to prove, mathematically, that a program was correct.

When was the last time someone proved a program correct?

Re:Goto decorator for python by indiecore on Monday March 12, 2012 @07:55AM (#3693090)
Several seconds ago I would assume. Just because real computer science doesn't get talked about much here doesn't mean that it doesn't exist.

As an addendum GOTO is still bad because it is much harder to parse (as a human reading the code) than using equivalent branching control statements. That was the whole point of Goto considered harmful, it's not mathematically needed so it's not syntactically needed either.

Re:Goto decorator for python by onedognight on Sunday March 11, 2012 @07:07PM (#3691549)
There's no new syntax. Notice what the labels and gotos look like:

   label  .error
   goto   .error
They are just method accesses on the global variables "label" and "goto". The OP just looks through the compiled byte code for access to them, saving the locations of the labels before replacing them with NOPs and replacing the gotos with jumps to the labels' locations. Cute.
Re:Goto decorator for python by stryker on Sunday March 11, 2012 @07:11PM (#3691561)
I too am more interested in the bytecode hackery that's going on here than the fact that it is a goto implementation. You might be interested in byteplay (http://code.google.com/p/byteplay/), which is a module that tries to simplify this kind of process.

Two points: 1) Do not take the presented code as evidence that arbitrarily new syntax can be created. Any code that raises a SyntaxError will still be invalid.

2) I believe there is a legitimate reason to use bytecode hackery -- I've come across it many times: the slowness of Python.

For example, let's say I have three functions as follows.

def f(a): return a + 1

def g(a): return 2a

def h(a): return 2a + 1

It's clear that f(g(x)) == h(x) for all values of x. But h will run faster (marginally in this case, but try to imagine more complicated cases) because the Python interpreter will never make such an optimization. Function calls will be made, frames generated, etc. -- and that's where bytecode hackery comes in! Imagine pasting f and g together so that I never have to write h but still get the benefit of speed.

I do recognize that in many cases, it may be preferable to just use a more low-level language to optimize away such functions, but I always like to envision a pure Python solution first.

Re:Goto decorator for python by baq on Sunday March 11, 2012 @07:27PM (#3691617)
your example breaks down when objects overriding __mul__ and __add__ are passed as a. that's the most important reason python doesn't do anything with code like this. you want a jit doing specialization and inlining for you (see tracing jit in pypy or, to a lesser extent, psyco.)
Re:Goto decorator for python by anonymoushn on Sunday March 11, 2012 @07:55PM (#3691700)
For what value of __mull__ and __add__ does f(g(x)) produce a different result than h(x)? You could do this by making __mul__ or __add__ change behavior based on stack inspection, but I am having trouble thinking of another way.
Re:Goto decorator for python by alextgordon on Sunday March 11, 2012 @09:07PM (#3691898)
A backtrace would be different, for a start. If __mul__ raises an exception, it would not show as coming from g(). Makes debugging a little bit harder.
Re:Goto decorator for python by icebraining on Sunday March 11, 2012 @07:04PM (#3691544)
Yeah, although it uses implementation-dependent bytecode, so it may not work on e.g. PyPy.
Re:Goto decorator for python by sambeau on Monday March 12, 2012 @05:49AM (#3692852)
Writing an efficient lexer without goto is a complete nightmare.