summaryrefslogtreecommitdiffstats
path: root/Lib/fileinput.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-04-10 10:29:28 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-04-10 10:29:28 (GMT)
commit2116b12da59f77358cc539b90f58a3cdea43c2fd (patch)
tree693d26652b1dbf8b2ac058c5725f9726c3d14a0b /Lib/fileinput.py
parentfcbf8f3e3d46eb023dd9a3954c6f9ed9a533d427 (diff)
parent7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd (diff)
downloadcpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.zip
cpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.tar.gz
cpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.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.py46
1 files changed, 25 insertions, 21 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index 87758ad..af810d1 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
@@ -275,29 +277,31 @@ class FileInput:
def nextfile(self):
savestdout = self._savestdout
- self._savestdout = 0
+ self._savestdout = None
if savestdout:
sys.stdout = savestdout
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
+ self._output = None
+ try:
+ if output:
+ output.close()
+ finally:
+ file = self._file
+ self._file = None
+ try:
+ if file and not self._isstdin:
+ file.close()
+ finally:
+ backupfilename = self._backupfilename
+ self._backupfilename = None
+ 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: