简体   繁体   中英

How does Python's fileinput accept input

I was wondering how fileinput.input() accepts interactive input.

I looked at the code at - https://github.com/python/cpython/blob/master/Lib/fileinput.py

And this is how the internal line reader looks:

def _readline(self):
    if not self._files:
        if 'b' in self._mode:
            return b''
        else:
            return ''
    self._filename = self._files[0]
    self._files = self._files[1:]
    self._startlineno = self.lineno()
    self._filelineno = 0
    self._file = None
    self._isstdin = False
    self._backupfilename = 0
    if self._filename == '-':
        self._filename = '<stdin>'
        if 'b' in self._mode:
            self._file = getattr(sys.stdin, 'buffer', sys.stdin)
        else:
            self._file = sys.stdin
        self._isstdin = True
    else:
        if self._inplace:
            self._backupfilename = (
                os.fspath(self._filename) + (self._backup or ".bak"))
            try:
                os.unlink(self._backupfilename)
            except OSError:
                pass
            # The next few lines may raise OSError
            os.rename(self._filename, self._backupfilename)
            self._file = open(self._backupfilename, self._mode)
            try:
                perm = os.fstat(self._file.fileno()).st_mode
            except OSError:
                self._output = open(self._filename, "w")
            else:
                mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
                if hasattr(os, 'O_BINARY'):
                    mode |= os.O_BINARY

                fd = os.open(self._filename, mode, perm)
                self._output = os.fdopen(fd, "w")
                try:
                    if hasattr(os, 'chmod'):
                        os.chmod(self._filename, perm)
                except OSError:
                    pass
            self._savestdout = sys.stdout
            sys.stdout = self._output
        else:
            # This may raise OSError
            if self._openhook:
                self._file = self._openhook(self._filename, self._mode)
            else:
                self._file = open(self._filename, self._mode)
    self._readline = self._file.readline  # hide FileInput._readline
    return self._readline()

I'm not sure what to make of it, and am curious to know how interactive input is actually being processed here, as I see no calls to the builtin input() .

What actually generates the user prompt in fileinput.input() ?

It makes call to the sys.stdin.readline method when no arguments is supplied. When no argument is supplied the _readline method looks like this.

def _readline(self):
        if self._filename == '-': # when no `files` parameter is supplied `self._filename` will be `-`
                self._file = sys.stdin
        self._readline = self._file.readline  # hide FileInput._readline
        return self._readline() # invoke the sys.stdin.readline()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM