2008-07-27 06:48:51 +08:00
|
|
|
"""
|
2013-09-19 06:51:08 +08:00
|
|
|
The temp module provides a NamedTemporaryFile that can be reopened in the same
|
|
|
|
process on any platform. Most platforms use the standard Python
|
|
|
|
tempfile.NamedTemporaryFile class, but Windows users are given a custom class.
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2013-09-19 06:51:08 +08:00
|
|
|
This is needed because the Python implementation of NamedTemporaryFile uses the
|
|
|
|
O_TEMPORARY flag under Windows, which prevents the file from being reopened
|
|
|
|
if the same flag is not provided [1][2]. Note that this does not address the
|
|
|
|
more general issue of opening a file for writing and reading in multiple
|
|
|
|
processes in a manner that works across platforms.
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2017-01-22 09:02:00 +08:00
|
|
|
The custom version of NamedTemporaryFile doesn't support the same keyword
|
|
|
|
arguments available in tempfile.NamedTemporaryFile.
|
2013-09-19 06:51:08 +08:00
|
|
|
|
2016-06-16 09:20:23 +08:00
|
|
|
1: https://mail.python.org/pipermail/python-list/2005-December/336957.html
|
2013-09-19 06:51:08 +08:00
|
|
|
2: http://bugs.python.org/issue14243
|
2008-07-27 06:48:51 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2009-05-08 23:08:09 +08:00
|
|
|
from django.core.files.utils import FileProxyMixin
|
2008-07-27 06:48:51 +08:00
|
|
|
|
|
|
|
__all__ = ('NamedTemporaryFile', 'gettempdir',)
|
|
|
|
|
2013-09-19 06:51:08 +08:00
|
|
|
|
2008-07-27 06:48:51 +08:00
|
|
|
if os.name == 'nt':
|
2009-05-08 23:08:09 +08:00
|
|
|
class TemporaryFile(FileProxyMixin):
|
2008-07-27 06:48:51 +08:00
|
|
|
"""
|
2013-09-19 06:51:08 +08:00
|
|
|
Temporary file object constructor that supports reopening of the
|
|
|
|
temporary file in Windows.
|
|
|
|
|
2017-01-22 09:02:00 +08:00
|
|
|
Unlike tempfile.NamedTemporaryFile from the standard library,
|
|
|
|
__init__() doesn't support the 'delete', 'buffering', 'encoding', or
|
|
|
|
'newline' keyword arguments.
|
2008-07-27 06:48:51 +08:00
|
|
|
"""
|
2016-03-29 06:33:29 +08:00
|
|
|
def __init__(self, mode='w+b', bufsize=-1, suffix='', prefix='', dir=None):
|
2013-09-19 06:51:08 +08:00
|
|
|
fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
|
2008-07-27 06:48:51 +08:00
|
|
|
self.name = name
|
2008-08-24 01:56:02 +08:00
|
|
|
self.file = os.fdopen(fd, mode, bufsize)
|
|
|
|
self.close_called = False
|
|
|
|
|
|
|
|
# Because close can be called during shutdown
|
|
|
|
# we need to cache os.unlink and access it
|
|
|
|
# as self.unlink only
|
|
|
|
unlink = os.unlink
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if not self.close_called:
|
|
|
|
self.close_called = True
|
|
|
|
try:
|
|
|
|
self.file.close()
|
|
|
|
except (OSError, IOError):
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
self.unlink(self.name)
|
2017-09-14 09:20:29 +08:00
|
|
|
except OSError:
|
2008-08-24 01:56:02 +08:00
|
|
|
pass
|
2008-07-27 06:48:51 +08:00
|
|
|
|
|
|
|
def __del__(self):
|
2008-08-24 01:56:02 +08:00
|
|
|
self.close()
|
|
|
|
|
2014-06-12 01:21:14 +08:00
|
|
|
def __enter__(self):
|
|
|
|
self.file.__enter__()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc, value, tb):
|
|
|
|
self.file.__exit__(exc, value, tb)
|
|
|
|
|
2008-07-27 06:48:51 +08:00
|
|
|
NamedTemporaryFile = TemporaryFile
|
|
|
|
else:
|
|
|
|
NamedTemporaryFile = tempfile.NamedTemporaryFile
|
|
|
|
|
|
|
|
gettempdir = tempfile.gettempdir
|