def _replaceStdoutStdin(cls):
        """
        Contextmanager that replaces stdout and stdin with /dev/tty
        and resets them when it is done.
        """
        oldout, oldin = sys.stdout, sys.stdin
        sys.stdin, sys.stdout = cls._openTty()
        try:
            yield
        finally:
            sys.stdout.close()
            sys.stdin.close()
            sys.stdout, sys.stdin = oldout, oldin

        Contextmanager that replaces stdout and stdin with /dev/tty
        and resets them when it is done.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/conch/client/default.py