mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
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.
This commit is contained in:
commit
2116b12da5
27 changed files with 315 additions and 209 deletions
|
@ -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()
|
||||
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
|
||||
|
||||
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._isstdin = False
|
||||
self._buffer = []
|
||||
self._bufindex = 0
|
||||
|
||||
def readline(self):
|
||||
try:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue