Code

Ticket #7658: tempfile.3.diff

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

Updated tempfile diff to fix AttributeError problem.

Line 
1Index: django/core/files/uploadedfile.py
2===================================================================
3--- django/core/files/uploadedfile.py   (revision 7869)
4+++ django/core/files/uploadedfile.py   (working copy)
5@@ -3,7 +3,6 @@
6 """
7 
8 import os
9-import tempfile
10 import warnings
11 try:
12     from cStringIO import StringIO
13@@ -12,6 +11,8 @@
14 
15 from django.conf import settings
16 
17+from django.core.files import temp as tempfile
18+
19 __all__ = ('UploadedFile', 'TemporaryUploadedFile', 'InMemoryUploadedFile', 'SimpleUploadedFile')
20 
21 # Because we fooled around with it a bunch, UploadedFile has a bunch
22Index: django/core/files/temp.py
23===================================================================
24--- django/core/files/temp.py   (revision 0)
25+++ django/core/files/temp.py   (revision 0)
26@@ -0,0 +1,60 @@
27+"""
28+The temp module defines a NamedTemporaryFile
29+that can be re-opened.
30+
31+This is needed because in Windows NT, the
32+default implementation of NamedTemporaryFile
33+uses the O_TEMPORARY flag, and thus cannot
34+be reopened [1].
35+
36+1: http://mail.python.org/pipermail/python-list/2005-December/359474.html
37+"""
38+
39+import os
40+import tempfile
41+
42+__all__ = ('NamedTemporaryFile', 'gettempdir',)
43+
44+class TemporaryFile(object):
45+    """
46+    Temporary file object constructor that works
47+    in Windows and supports reopening of the
48+    temporary file in windows.
49+    """
50+    def __init__(self, mode='w+b', bufsize=-1, suffix='', prefix='', dir=None):
51+        fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
52+                                      dir=dir)
53+        self.name = name
54+        self._file = os.fdopen(fd, mode, bufsize)
55+
56+    def __del__(self):
57+        try:
58+            self._file.close()
59+        except (OSError, IOError):
60+            pass
61+        try:
62+            os.unlink(self.name)
63+        except (OSError):
64+            pass
65+
66+        try:
67+            super(TemporaryFile, self).__del__()
68+        except AttributeError:
69+            pass
70+
71+
72+    def read(self, *args):          return self._file.read(*args)
73+    def seek(self, offset):         return self._file.seek(offset)
74+    def write(self, s):             return self._file.write(s)
75+    def close(self):                return self._file.close()
76+    def __iter__(self):             return iter(self._file)
77+    def readlines(self, size=None): return self._file.readlines(size)
78+    def xreadlines(self):           return self._file.xreadlines()
79+
80+
81+if os.name == 'nt':
82+    NamedTemporaryFile = TemporaryFile
83+else:
84+    NamedTemporaryFile = tempfile.NamedTemporaryFile
85+
86+gettempdir = tempfile.gettempdir
87Index: tests/regressiontests/file_uploads/tests.py
88===================================================================
89--- tests/regressiontests/file_uploads/tests.py (revision 7869)
90+++ tests/regressiontests/file_uploads/tests.py (working copy)
91@@ -1,6 +1,6 @@
92 import os
93 import sha
94-import tempfile
95+from django.core.files import temp as tempfile
96 from django.test import TestCase, client
97 from django.utils import simplejson
98