Python error handling fixes