BUG: py3 fixes; revise scoping rules to be more broad
     record variable states from the furthermost part of the stack
     to the most recent, overwriting if shadow variables occur