python: Improve error handling.