From: Guido van Rossum Date: Sat, 24 Oct 1998 15:02:59 +0000 (+0000) Subject: Improvement to the previous fix suggested by Thomas Bellman: if the X-Git-Tag: v1.5.2b1~266 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2457fc2a816aa7c9ca0be82a4595567204d5457e;p=python Improvement to the previous fix suggested by Thomas Bellman: if the unlink() or fdopen() fail, close the file descriptor and re-raise the exception. --- diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 140eebccf9..1f301262db 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -129,8 +129,12 @@ def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): if os.name == 'posix': # Unix -- be very careful fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700) - os.unlink(name) - return os.fdopen(fd, mode, bufsize) + try: + os.unlink(name) + return os.fdopen(fd, mode, bufsize) + except: + os.close(fd) + raise else: # Non-unix -- can't unlink file that's still open, use wrapper file = open(name, mode, bufsize)