This function decorates a function with tail call optimization. It does this by throwing an exception if it is it's own grandparent, and catching such exceptions to fake the tail call optimization.
Sometimes I wonder why a whole bunch of Scheme loving folks use Python. Then, I see rather clever things like this, and it starts to make more sense. I do wonder how much of a performance penalty something like this incurs, though.