Catch exceptions during tty check

Fixes a build break where sys.stdout is a StringIO object
with no fileno attribute.