Ticket #7658: tempfile.3.diff

File tempfile.3.diff, 3.0 KB (added by axiak, 7 years ago)

Updated tempfile diff to fix AttributeError problem.

  • 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        try:
     41            super(TemporaryFile, self).__del__()
     42        except AttributeError:
     43            pass
     44
     45
     46    def read(self, *args):          return self._file.read(*args)
     47    def seek(self, offset):         return self._file.seek(offset)
     48    def write(self, s):             return self._file.write(s)
     49    def close(self):                return self._file.close()
     50    def __iter__(self):             return iter(self._file)
     51    def readlines(self, size=None): return self._file.readlines(size)
     52    def xreadlines(self):           return self._file.xreadlines()
     53
     54
     55if os.name == 'nt':
     56    NamedTemporaryFile = TemporaryFile
     57else:
     58    NamedTemporaryFile = tempfile.NamedTemporaryFile
     59
     60gettempdir = 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