1
0
mirror of https://github.com/django/django.git synced 2025-06-03 18:49:12 +00:00

Fixed #18744 -- Updated docstring to highlight limitations of NamedTemporaryFile

- Noted that this does not allow for reading and writing the same open
file in different processes under Windows.
- Noted that the keyword arguments to NamedTemporaryFile no longer
match the Python version.
This commit is contained in:
Kevin Christopher Henry 2013-09-18 18:51:08 -04:00 committed by Tim Graham
parent 4e9f800742
commit 59a34c43a8

View File

@ -1,12 +1,19 @@
""" """
The temp module provides a NamedTemporaryFile that can be re-opened on any The temp module provides a NamedTemporaryFile that can be reopened in the same
platform. Most platforms use the standard Python tempfile.TemporaryFile class, process on any platform. Most platforms use the standard Python
but MS Windows users are given a custom class. tempfile.NamedTemporaryFile class, but Windows users are given a custom class.
This is needed because in Windows NT, the default implementation of This is needed because the Python implementation of NamedTemporaryFile uses the
NamedTemporaryFile uses the O_TEMPORARY flag, and thus cannot be reopened [1]. 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.
1: http://mail.python.org/pipermail/python-list/2005-December/359474.html Also note that the custom version of NamedTemporaryFile does not support the
full range of keyword arguments available in Python 2.6+ and 3.0+.
1: https://mail.python.org/pipermail/python-list/2005-December/336958.html
2: http://bugs.python.org/issue14243
""" """
import os import os
@ -15,16 +22,20 @@ from django.core.files.utils import FileProxyMixin
__all__ = ('NamedTemporaryFile', 'gettempdir',) __all__ = ('NamedTemporaryFile', 'gettempdir',)
if os.name == 'nt': if os.name == 'nt':
class TemporaryFile(FileProxyMixin): class TemporaryFile(FileProxyMixin):
""" """
Temporary file object constructor that works in Windows and supports Temporary file object constructor that supports reopening of the
reopening of the temporary file in windows. temporary file in Windows.
Note that __init__() does not support the 'delete' keyword argument in
Python 2.6+, or the 'delete', 'buffering', 'encoding', or 'newline'
keyword arguments in Python 3.0+.
""" """
def __init__(self, mode='w+b', bufsize=-1, suffix='', prefix='', def __init__(self, mode='w+b', bufsize=-1, suffix='', prefix='',
dir=None): dir=None):
fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
dir=dir)
self.name = name self.name = name
self.file = os.fdopen(fd, mode, bufsize) self.file = os.fdopen(fd, mode, bufsize)
self.close_called = False self.close_called = False