]> granicus.if.org Git - python/commitdiff
Improvement to the previous fix suggested by Thomas Bellman: if the
authorGuido van Rossum <guido@python.org>
Sat, 24 Oct 1998 15:02:59 +0000 (15:02 +0000)
committerGuido van Rossum <guido@python.org>
Sat, 24 Oct 1998 15:02:59 +0000 (15:02 +0000)
unlink() or fdopen() fail, close the file descriptor and re-raise the
exception.

Lib/tempfile.py

index 140eebccf91313449959d1e0d142ee601fd39ba1..1f301262db8390fe3900e440a5879e46db6cb50d 100644 (file)
@@ -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)