2 The temp module provides a NamedTemporaryFile that can be re-opened on any
3 platform. Most platforms use the standard Python tempfile.TemporaryFile class,
4 but MS Windows users are given a custom class.
6 This is needed because in Windows NT, the default implementation of
7 NamedTemporaryFile uses the O_TEMPORARY flag, and thus cannot be reopened [1].
9 1: http://mail.python.org/pipermail/python-list/2005-December/359474.html
14 from django
.core
.files
.utils
import FileProxyMixin
16 __all__
= ('NamedTemporaryFile', 'gettempdir',)
19 class TemporaryFile(FileProxyMixin
):
21 Temporary file object constructor that works in Windows and supports
22 reopening of the temporary file in windows.
24 def __init__(self
, mode
='w+b', bufsize
=-1, suffix
='', prefix
='',
26 fd
, name
= tempfile
.mkstemp(suffix
=suffix
, prefix
=prefix
,
29 self
.file = os
.fdopen(fd
, mode
, bufsize
)
30 self
.close_called
= False
32 # Because close can be called during shutdown
33 # we need to cache os.unlink and access it
38 if not self
.close_called
:
39 self
.close_called
= True
42 except (OSError, IOError):
45 self
.unlink(self
.name
)
52 NamedTemporaryFile
= TemporaryFile
54 NamedTemporaryFile
= tempfile
.NamedTemporaryFile
56 gettempdir
= tempfile
.gettempdir