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 }When was the last time someone proved a program correct?
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.
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.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.