Pydebug fixes

Fix various crashes and failures found using a pydebug build of Python.