Fix NameError: name 'ch' is not defined

if we get to that part of the code (discovered when compiling file with
the warning: undeclared name not builtin: ch)
Fix many errors of stuff not declared

In cases that never execute, it seems, but that can't be compiled from
cython if not fixed.