diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-03-08 21:36:25 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-03-08 21:36:25 (GMT) |
commit | aadaa3614e9aab723b3563801dfc700c0dae46e3 (patch) | |
tree | e3d86f19c81a8ee2d6bd29aa13c753753aeb754a /Lib/fileinput.py | |
parent | 39f57caf3a3831822279028e1dfe8f14a223e868 (diff) | |
parent | 0554d83f0ff7e9da5c021b57820b26a330c09129 (diff) | |
download | cpython-aadaa3614e9aab723b3563801dfc700c0dae46e3.zip cpython-aadaa3614e9aab723b3563801dfc700c0dae46e3.tar.gz cpython-aadaa3614e9aab723b3563801dfc700c0dae46e3.tar.bz2 |
Issue #15068: Avoid creating a reference loop in fileinput.
Diffstat (limited to 'Lib/fileinput.py')
-rw-r--r-- | Lib/fileinput.py | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py index 4286156..1e19d24 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -209,7 +209,6 @@ class FileInput: self._startlineno = 0 self._filelineno = 0 self._file = None - self._readline = self._start_readline self._isstdin = False self._backupfilename = None # restrict mode argument to reading modes @@ -247,15 +246,15 @@ class FileInput: return self def __next__(self): - line = self._readline() - if line: - self._filelineno += 1 - return line - if not self._file: - raise StopIteration - self.nextfile() - # Recursive call - return self.__next__() + while True: + line = self._readline() + if line: + self._filelineno += 1 + return line + if not self._file: + raise StopIteration + self.nextfile() + # repeat with next file def __getitem__(self, i): if i != self.lineno(): @@ -279,7 +278,10 @@ class FileInput: finally: file = self._file self._file = None - self._readline = self._start_readline + try: + del self._readline # restore FileInput._readline + except AttributeError: + pass try: if file and not self._isstdin: file.close() @@ -303,7 +305,7 @@ class FileInput: self.nextfile() # repeat with next file - def _start_readline(self): + def _readline(self): if not self._files: if 'b' in self._mode: return b'' @@ -358,7 +360,7 @@ class FileInput: self._file = self._openhook(self._filename, self._mode) else: self._file = open(self._filename, self._mode) - self._readline = self._file.readline + self._readline = self._file.readline # hide FileInput._readline return self._readline() def filename(self): |