diff options
-rw-r--r-- | Lib/tempfile.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 6a2730a..140eebc 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -126,11 +126,12 @@ class TemporaryFileWrapper: def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): name = mktemp(suffix) - file = open(name, mode, bufsize) - try: + 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) - except os.error: + return os.fdopen(fd, mode, bufsize) + else: # Non-unix -- can't unlink file that's still open, use wrapper + file = open(name, mode, bufsize) return TemporaryFileWrapper(file, name) - else: - return file |