def communicate(self, input = None):
        """Consumes the process' stdout and stderr until the it terminates.

        :param input: An optional bytes/buffer object to send to the process over stdin
        :returns: A tuple of (stdout, stderr)
        """
        stdout = []
        stderr = []
        sources = [("1", stdout, self.stdout)]
        if not self.isatty:
            # in tty mode, stdout and stderr are unified
            sources.append(("2", stderr, self.stderr))
        i = 0
        while sources:
            if input:
                chunk = input[:1000]
                self.stdin.write(chunk)
                self.stdin.flush()
                input = input[1000:]
            i = (i + 1) % len(sources)
            name, coll, pipe = sources[i]
            try:
                line = pipe.readline()
                shell_logger.debug("%s> %r", name, line)
            except EOFError:
                shell_logger.debug("%s> Nothing returned.", name)

                self.proc.poll()
                returncode = self.proc.returncode
                if returncode == 5:
                    raise IncorrectLogin("Incorrect username or password provided")
                elif returncode == 6:
                    raise HostPublicKeyUnknown("The authenticity of the host can't be established")
                msg = "No communication channel detected. Does the remote exist?"
                msgerr = "No stderr result detected. Does the remote have Bash as the default shell?"
                raise SSHCommsChannel2Error(msgerr) if name=="2" else SSHCommsError(msg)
            if not line:
                del sources[i]
            else:
                coll.append(line)
        if self.isatty:
            stdout.pop(0)  # discard first line of prompt
        try:
            self.returncode = int(stdout.pop(-1))
        except (IndexError, ValueError):
            self.returncode = "Unknown"
        self._done = True
        stdout = six.b("").join(stdout)
        stderr = six.b("").join(stderr)
        return stdout, stderr
dataset/ETHPy150Open tomerfiliba/plumbum/plumbum/machines/session.py/SessionPopen.communicate/original