diff options
author | Guido van Rossum <guido@python.org> | 1998-10-24 15:02:59 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-10-24 15:02:59 (GMT) |
commit | 2457fc2a816aa7c9ca0be82a4595567204d5457e (patch) | |
tree | 89bf5966fc7ffb8e388f3a53ffeaf8a9cbe6b488 /Lib/tempfile.py | |
parent | a96c2d407bbbc27c516c211e12802b9a6698f727 (diff) | |
download | cpython-2457fc2a816aa7c9ca0be82a4595567204d5457e.zip cpython-2457fc2a816aa7c9ca0be82a4595567204d5457e.tar.gz cpython-2457fc2a816aa7c9ca0be82a4595567204d5457e.tar.bz2 |
Improvement to the previous fix suggested by Thomas Bellman: if the
unlink() or fdopen() fail, close the file descriptor and re-raise the
exception.
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r-- | Lib/tempfile.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 140eebc..1f30126 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) |