mirror of https://github.com/python/cpython
Improvement to the previous fix suggested by Thomas Bellman: if the
unlink() or fdopen() fail, close the file descriptor and re-raise the exception.
This commit is contained in:
parent
a96c2d407b
commit
2457fc2a81
|
@ -129,8 +129,12 @@ def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
# Unix -- be very careful
|
# Unix -- be very careful
|
||||||
fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
|
fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
|
||||||
|
try:
|
||||||
os.unlink(name)
|
os.unlink(name)
|
||||||
return os.fdopen(fd, mode, bufsize)
|
return os.fdopen(fd, mode, bufsize)
|
||||||
|
except:
|
||||||
|
os.close(fd)
|
||||||
|
raise
|
||||||
else:
|
else:
|
||||||
# Non-unix -- can't unlink file that's still open, use wrapper
|
# Non-unix -- can't unlink file that's still open, use wrapper
|
||||||
file = open(name, mode, bufsize)
|
file = open(name, mode, bufsize)
|
||||||
|
|
Loading…
Reference in New Issue