0

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()?

2
  • 1
    It makes call to the sys.stdin.readline method when no argument is supplied. Commented Feb 17, 2019 at 16:49
  • Thanks! Wasn't aware of that call. If you post a reply, I'll mark it as an answer Commented Feb 18, 2019 at 16:15

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.