Ticket #7658: tempfile.diff

File tempfile.diff, 2.9 KB (added by Michael Axiak, 16 years ago)

New temp module for Windows support

  • django/core/files/uploadedfile.py

     
    33"""
    44
    55import os
    6 import tempfile
    76import warnings
    87try:
    98    from cStringIO import StringIO
     
    1211
    1312from django.conf import settings
    1413
     14from django.core.files import temp as tempfile
     15
    1516__all__ = ('UploadedFile', 'TemporaryUploadedFile', 'InMemoryUploadedFile', 'SimpleUploadedFile')
    1617
    1718# Because we fooled around with it a bunch, UploadedFile has a bunch
  • django/core/files/temp.py

     
     1"""
     2The temp module defines a NamedTemporaryFile
     3that can be re-opened.
     4
     5This is needed because in Windows NT, the
     6default implementation of NamedTemporaryFile
     7uses the O_TEMPORARY flag, and thus cannot
     8be reopened [1].
     9
     101: http://mail.python.org/pipermail/python-list/2005-December/359474.html
     11"""
     12
     13import os
     14import tempfile
     15
     16__all__ = ('NamedTemporaryFile', 'gettempdir',)
     17
     18class TemporaryFile(object):
     19    """
     20    Temporary file object constructor that works
     21    in Windows and supports reopening of the
     22    temporary file in windows.
     23    """
     24    def __init__(self, mode='w+b', bufsize=-1, suffix='', prefix='', dir=None):
     25        fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
     26                                      dir=dir)
     27        self.name = name
     28        self._file = os.fdopen(fd, mode, bufsize)
     29
     30    def __del__(self):
     31        try:
     32            self._file.close()
     33        except (OSError, IOError):
     34            pass
     35        try:
     36            os.unlink(self.name)
     37        except (OSError):
     38            pass
     39
     40        super(TemporaryFile, self).__del__()
     41
     42
     43    def read(self, *args):          return self._file.read(*args)
     44    def seek(self, offset):         return self._file.seek(offset)
     45    def write(self, s):             return self._file.write(s)
     46    def close(self):                return self._file.close()
     47    def __iter__(self):             return iter(self._file)
     48    def readlines(self, size=None): return self._file.readlines(size)
     49    def xreadlines(self):           return self._file.xreadlines()
     50
     51
     52if os.name == 'nt':
     53    NamedTemporaryFile = TemporaryFile
     54else:
     55    NamedTemporaryFile = tempfile.NamedTemporaryFile
     56
     57gettempdir = tempfile.gettempdir
  • tests/regressiontests/file_uploads/tests.py

     
    11import os
    22import sha
    3 import tempfile
     3from django.core.files import temp as tempfile
    44from django.test import TestCase, client
    55from django.utils import simplejson
    66
Back to Top