Commit 0554d83f authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #15068: Avoid creating a reference loop in fileinput.

parent 4a6f4825
......@@ -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
......@@ -245,6 +244,7 @@ class FileInput:
return self
def __next__(self):
while True:
line = self._readline()
if line:
self._filelineno += 1
......@@ -252,8 +252,7 @@ class FileInput:
if not self._file:
raise StopIteration
self.nextfile()
# Recursive call
return self.__next__()
# repeat with next file
def __getitem__(self, i):
if i != self.lineno():
......@@ -277,7 +276,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()
......@@ -301,7 +303,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''
......@@ -356,7 +358,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):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment