Added error handling to check_canonical function in Python scripts.