def setUp(self): self.save_signals = SaveSignals() self.save_signals.save() if verbose: # just to make the test output a little more readable print() self.stdscr = curses.initscr() curses.savetty()
def setUp(self): self.isatty = True self.output = sys.__stdout__ stdout_fd = sys.__stdout__.fileno() if not sys.__stdout__.isatty(): # initstr() unconditionally uses C stdout. # If it is redirected to file or pipe, try to attach it # to terminal. # First, save a copy of the file descriptor of stdout, so it # can be restored after finishing the test. dup_fd = os.dup(stdout_fd) self.addCleanup(os.close, dup_fd) self.addCleanup(os.dup2, dup_fd, stdout_fd) if sys.__stderr__.isatty(): # If stderr is connected to terminal, use it. tmp = sys.__stderr__ self.output = sys.__stderr__ else: try: # Try to open the terminal device. tmp = open('/dev/tty', 'wb', buffering=0) except OSError: # As a fallback, use regular file to write control codes. # Some functions (like savetty) will not work, but at # least the garbage control sequences will not be mixed # with the testing report. tmp = tempfile.TemporaryFile(mode='wb', buffering=0) self.isatty = False self.addCleanup(tmp.close) self.output = None os.dup2(tmp.fileno(), stdout_fd) self.save_signals = SaveSignals() self.save_signals.save() self.addCleanup(self.save_signals.restore) if verbose and self.output is not None: # just to make the test output a little more readable sys.stderr.flush() sys.stdout.flush() print(file=self.output, flush=True) self.stdscr = curses.initscr() if self.isatty: curses.savetty() self.addCleanup(curses.endwin) self.addCleanup(curses.resetty) self.stdscr.erase()