python: fix error handling