Ticket #14749: enter_exit_for_file.diff

File enter_exit_for_file.diff, 1.3 KB (added by Florian Apolloner, 14 years ago)
  • django/core/files/base.py

    diff --git a/django/core/files/base.py b/django/core/files/base.py
    index f9e3f10..dc07399 100644
    a b class File(FileProxyMixin):  
    101101        if buffer_ is not None:
    102102            yield buffer_
    103103
     104    def __enter__(self):
     105        return self
     106
     107    def __exit__(self, type, value, traceback):
     108        self.close()
     109
    104110    def open(self, mode=None):
    105111        if not self.closed:
    106112            self.seek(0)
  • tests/regressiontests/file_storage/tests.py

    diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
    index b90bf0e..1e6d703 100644
    a b class FileStorageTests(unittest.TestCase):  
    111111        self.storage.delete('storage_test')
    112112        self.failIf(self.storage.exists('storage_test'))
    113113
     114    def test_file_context_managers(self):
     115        """
     116        Using __enter__ and __exit__ works as expected.
     117        """
     118        orig_file = tempfile.TemporaryFile()
     119        with File(orig_file) as f:
     120            self.assertTrue(isinstance(f, File))
     121            self.assertFalse(f.closed)
     122        self.assertTrue(f.closed)
     123
    114124    def test_file_accessed_time(self):
    115125        """
    116126        File storage returns a Datetime object for the last accessed time of
Back to Top