Check isatty existence on sys.stdout

This fixes #916 , but I found another #1019 may duplicates this.