def _redirect_output(self, statement: Statement) -> Tuple[bool, utils.RedirectionSavedState]:
        """Handles output redirection for >, >>, and |.

        :param statement: a parsed statement from the user
        :return: A bool telling if an error occurred and a utils.RedirectionSavedState object
        """
        import io
        import subprocess

        redir_error = False

        # Initialize the saved state
        saved_state = utils.RedirectionSavedState(self.stdout, sys.stdout, self.cur_pipe_proc_reader)

        if not self.allow_redirection:
            return redir_error, saved_state

        if statement.pipe_to:
            # Create a pipe with read and write sides
            read_fd, write_fd = os.pipe()

            # Open each side of the pipe
            subproc_stdin = io.open(read_fd, 'r')
            new_stdout = io.open(write_fd, 'w')

            # We want Popen to raise an exception if it fails to open the process.  Thus we don't set shell to True.
            try:
                # Set options to not forward signals to the pipe process. If a Ctrl-C event occurs,
                # our sigint handler will forward it only to the most recent pipe process. This makes
                # sure pipe processes close in the right order (most recent first).
                if sys.platform == 'win32':
                    creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
                    start_new_session = False
                else:
                    creationflags = 0
                    start_new_session = True

                # For any stream that is a StdSim, we will use a pipe so we can capture its output
                proc = \
                    subprocess.Popen(statement.pipe_to,
                                     stdin=subproc_stdin,
                                     stdout=subprocess.PIPE if isinstance(self.stdout, utils.StdSim) else self.stdout,
                                     stderr=subprocess.PIPE if isinstance(sys.stderr, utils.StdSim) else sys.stderr,
                                     creationflags=creationflags,
                                     start_new_session=start_new_session)

                saved_state.redirecting = True
                saved_state.pipe_proc_reader = utils.ProcReader(proc, self.stdout, sys.stderr)
                sys.stdout = self.stdout = new_stdout
            except Exception as ex:
                self.perror('Failed to open pipe because - {}'.format(ex), traceback_war=False)
                subproc_stdin.close()
                new_stdout.close()
                redir_error = True

        elif statement.output:
            import tempfile
            if (not statement.output_to) and (not self.can_clip):
                self.perror("Cannot redirect to paste buffer; install 'pyperclip' and re-run to enable",
                            traceback_war=False)
                redir_error = True

            elif statement.output_to:
                # going to a file
                mode = 'w'
                # statement.output can only contain
                # REDIRECTION_APPEND or REDIRECTION_OUTPUT
                if statement.output == constants.REDIRECTION_APPEND:
                    mode = 'a'
                try:
                    new_stdout = open(statement.output_to, mode)
                    saved_state.redirecting = True
                    sys.stdout = self.stdout = new_stdout
                except OSError as ex:
                    self.perror('Failed to redirect because - {}'.format(ex), traceback_war=False)
                    redir_error = True
            else:
                # going to a paste buffer
                new_stdout = tempfile.TemporaryFile(mode="w+")
                saved_state.redirecting = True
                sys.stdout = self.stdout = new_stdout

                if statement.output == constants.REDIRECTION_APPEND:
                    self.poutput(get_paste_buffer())

        return redir_error, saved_state
Handles output redirection for >, >>, and |.

        :param statement: a parsed statement from the user
        :return: A bool telling if an error occurred and a utils.RedirectionSavedState object