Python crash fixes

Here is the attempt to fix pythons test failures. Only tested it on 64bit linux, but it doesn't crash there and doesn't leak memory.