Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
- 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.
  • Loading branch information...
commit b2f5ac16565605f20a0c4e90acc6beed5a5ac1ce 1 parent 05e14e8
@adamsc64 adamsc64 authored
Showing with 24 additions and 0 deletions.
  1. +9 −0 django/core/files/
  2. +15 −0 tests/files/
9 django/core/files/
@@ -46,6 +46,15 @@ def close(self):
except (OSError):
+ @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):
15 tests/files/
@@ -11,6 +11,7 @@
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 @@ def test_context_manager(self):
+ 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
Please sign in to comment.
Something went wrong with that request. Please try again.