diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-10 10:24:41 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-10 10:24:41 (GMT) |
commit | 7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd (patch) | |
tree | a0777a3e70ae76f294fac756c684ec4e24d5df1d /Lib/fileinput.py | |
parent | 842f00e72509db50957ceb00d289b305dbc5a0a5 (diff) | |
download | cpython-7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd.zip cpython-7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd.tar.gz cpython-7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd.tar.bz2 |
Issue #23865: close() methods in multiple modules now are idempotent and more
robust at shutdown. If needs to release multiple resources, they are released
even if errors are occured.
Diffstat (limited to 'Lib/fileinput.py')
-rw-r--r-- | Lib/fileinput.py | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py index 87758ad..8af4a57 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -238,8 +238,10 @@ class FileInput: self.close() def close(self): - self.nextfile() - self._files = () + try: + self.nextfile() + finally: + self._files = () def __enter__(self): return self @@ -281,23 +283,25 @@ class FileInput: output = self._output self._output = 0 - if output: - output.close() - - file = self._file - self._file = 0 - if file and not self._isstdin: - file.close() - - backupfilename = self._backupfilename - self._backupfilename = 0 - if backupfilename and not self._backup: - try: os.unlink(backupfilename) - except OSError: pass - - self._isstdin = False - self._buffer = [] - self._bufindex = 0 + try: + if output: + output.close() + finally: + file = self._file + self._file = 0 + try: + if file and not self._isstdin: + file.close() + finally: + backupfilename = self._backupfilename + self._backupfilename = 0 + if backupfilename and not self._backup: + try: os.unlink(backupfilename) + except OSError: pass + + self._isstdin = False + self._buffer = [] + self._bufindex = 0 def readline(self): try: |