Check for the existence of sys.stderr.isatty before calling it.

Only the `write` method is guaranteed to exist on sys.stderr and sys.stdout.

Closes #920.