diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-03-08 21:34:28 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-03-08 21:34:28 (GMT) |
commit | 2c311f937e872774276bb67c10b35fe1e2c307b6 (patch) | |
tree | c1e3f80ce41ac1f20241c7ef2b5710f8afb5aa85 /Lib | |
parent | b6d633ce3342513f138a7b01d1407e813cc57129 (diff) | |
download | cpython-2c311f937e872774276bb67c10b35fe1e2c307b6.zip cpython-2c311f937e872774276bb67c10b35fe1e2c307b6.tar.gz cpython-2c311f937e872774276bb67c10b35fe1e2c307b6.tar.bz2 |
Issue #15068: Avoid creating a reference loop in fileinput.
Diffstat (limited to 'Lib')
-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 f54632c..b2e2f05 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -207,7 +207,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 @@ -234,15 +233,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 1: + 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(): @@ -266,7 +265,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() @@ -290,7 +292,7 @@ class FileInput: self.nextfile() # repeat with next file - def _start_readline(self): + def _readline(self): if not self._files: return "" self._filename = self._files[0] @@ -336,7 +338,7 @@ class FileInput: 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): |