If only programming were that simple...
if(operation_failed)
make_operation_succeed(); if(operation_failed)
make_operation_succeed(); import os
if os.isatty(1):
print("stdout is a tty")
else:
print("stdout is not a tty")
It really is that simple. Substitute the 1 for 0 when checking if stdin is a tty, and 2 if checking stderr[1] There's an ioctl involved, and it can fail for other reasons than "fd is not terminal", however all the reason for the operation to fail are good reasons to not proceed with terminal specific behavior.
if (! try_thing())
if (! try_thing_again())
if (! try_thing_a_third_time())
give_up();
... where the thing is something that should basically always work, and if it doesn't there's no good reason to expect that trying again will have a different result. The programmer who wrote this thought it was defensive coding and a good practice.