From b2f5ac16565605f20a0c4e90acc6beed5a5ac1ce Mon Sep 17 00:00:00 2001 From: Christopher Adams Date: Fri, 6 Sep 2013 14:23:50 -0400 Subject: [PATCH 1/2] Fixed #11857 -- Added missing 'closed' property on TemporaryFile class. - TemporaryFile now minimally mocks the API of the Python standard library class tempfile.NamedTemporaryFile to avoid AttributeError exceptions. - The symbol django.core.files.NamedTemporaryFile is actually assigned as a different class on different operating systems. - The bug only occurred if Django is running on Windows, hence why it was hard to diagnose. --- django/core/files/temp.py | 9 +++++++++ tests/files/tests.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/django/core/files/temp.py b/django/core/files/temp.py index b6072912945..3dcda17a09d 100644 --- a/django/core/files/temp.py +++ b/django/core/files/temp.py @@ -46,6 +46,15 @@ if os.name == 'nt': except (OSError): pass + @property + def closed(self): + """ + This attribute needs to be accessible in certain situations, + because this class is supposed to mock the API of the class + tempfile.NamedTemporaryFile in the Python standard library. + """ + return self.file.closed + def __del__(self): self.close() diff --git a/tests/files/tests.py b/tests/files/tests.py index b353c1a2134..2bc9d566d8c 100644 --- a/tests/files/tests.py +++ b/tests/files/tests.py @@ -11,6 +11,7 @@ from django.core.files import File from django.core.files.move import file_move_safe from django.core.files.base import ContentFile from django.core.files.uploadedfile import SimpleUploadedFile +from django.core.files.temp import NamedTemporaryFile from django.test import TestCase from django.utils.six import StringIO @@ -142,6 +143,20 @@ class FileTests(unittest.TestCase): self.assertTrue(f.closed) self.assertTrue(orig_file.closed) + def test_namedtemporaryfile_closes(self): + """ + The symbol django.core.files.NamedTemporaryFile is assigned as + a different class on different operating systems. In + any case, the result should minimally mock some of the API of + tempfile.NamedTemporaryFile from the Python standard library. + """ + tempfile = NamedTemporaryFile() + self.assertTrue(hasattr(tempfile, "closed")) + self.assertFalse(tempfile.closed) + + tempfile.close() + self.assertTrue(tempfile.closed) + def test_file_mode(self): # Should not set mode to None if it is not present. # See #14681, stdlib gzip module crashes if mode is set to None From 0756a4d664b3d243a70aa53567cec5d8d25e37f7 Mon Sep 17 00:00:00 2001 From: Christopher Adams Date: Fri, 6 Sep 2013 20:36:42 -0400 Subject: [PATCH 2/2] Added Christopher Adams to the AUTHORS file. - Note that 'Chris Adams' and 'Christopher Adams' are two different contributors. --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index c343eeb67bd..e5fea9969f7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -57,6 +57,7 @@ answer newbie questions, and generally made Django that much better: Gisle Aas Chris Adams + Christopher Adams Mathieu Agopian ajs alang@bright-green.com