Sep-24-2022, 12:25 PM
I'm looking at some code which is intended to be used interactively, and I don't quite understand it.
Simplified, the code looks like this:
Can somebody explain the circumstances where the fallback code will run please?
Thanks in advance.
Simplified, the code looks like this:
import sys def example(): try: import tty fd = sys.stdin.fileno() old = tty.tcgetattr(fd) tty.setcbreak(fd) getchar = lambda: sys.stdin.read(1) except (ImportError, AttributeError, io.UnsupportedOperation): # Fallback for when the terminal is not a tty. tty = None getchar = lambda: sys.stdin.readline()[:-1][:1] try: while True: c = getchar() if c in ('q', 'Q'): break finally: if tty: tty.tcsetattr(fd, tty.TCSAFLUSH, old)The code waits until the user types 'q' or 'Q'. What I don't get is under what circumstances can you have a terminal session, complete with stdin, but *not* be attached to a tty?
Can somebody explain the circumstances where the fallback code will run please?
Thanks in advance.