def __configure_stdio(self, stdout): """Configures the standard I/O file descriptors.""" # stdin --> if self.__stdin_source is None: self.__stdin_source = DEVNULL elif (self.__stdin_source is STDIN or isinstance(self.__stdin_source, File)): pass elif self.__piped_from_process(): # Connect and execute all processes in the pipe pipe = Pipe(psys.STDIN_FILENO, output=False) self.__pipes.append(pipe) self.__stdin_source._execute(stdout=pipe, check_pipes=False) else: if type(self.__stdin_source) in (bytes, str): self.__stdin_generator = iter([self.__stdin_source]) elif isinstance(self.__stdin_source, collections.Iterator): self.__stdin_generator = self.__stdin_source elif isinstance(self.__stdin_source, collections.Iterable): self.__stdin_generator = iter(self.__stdin_source) else: raise LogicalError() pipe = Pipe(psys.STDIN_FILENO, output=False) self.__pipes.append(pipe) # stdin <-- # stdout --> if self.__stdout_target in (STDOUT, STDERR) or isinstance( self.__stdout_target, File): if stdout is not None: raise LogicalError() elif self.__stdout_target is PIPE or self.__piped_to_process(): self.__pipes.append(Pipe(psys.STDOUT_FILENO, pipe=stdout)) else: raise LogicalError() # stdout <-- # stderr --> if self.__stderr_target in (STDOUT, STDERR) or isinstance( self.__stderr_target, File): pass elif self.__stderr_target is PIPE: self.__pipes.append(Pipe(psys.STDERR_FILENO)) else: raise LogicalError()
def __init__(self, process, raw, delimiter): # The process self.__process = process # Raw or unicode string iteration self.__raw = raw # Block delimiter self.__delimiter = delimiter # Output pipe self.__pipe = None # Polling object self.__poll = None # Output data buffer. None indicates EOF. self.__data = b"" # Is the iterator closed self.__closed = False if delimiter: self.__iter = self.__iter_with_delimiter else: if raw: self.__iter = self.__iter_without_delimiter else: raise InvalidOperation( "Can't iterate over unicode data without delimiter") try: self.__pipe = Pipe(psys.STDOUT_FILENO) flags = eintr_retry(fcntl.fcntl)(self.__pipe.read, fcntl.F_GETFL) eintr_retry(fcntl.fcntl)(self.__pipe.read, fcntl.F_SETFL, flags | os.O_NONBLOCK) self.__poll = psys.poll.Poll() self.__poll.register(self.__pipe.read, self.__poll.POLLIN) except: self.close() raise