Ticket #13809: 13809-1.diff

File 13809-1.diff, 1.9 KB (added by Claude Paroz, 12 years ago)

Allow FielField instance to be open in writable mode

  • django/core/files/base.py

    diff --git a/django/core/files/base.py b/django/core/files/base.py
    index 04853fa..a1b0948 100644
    a b class File(FileProxyMixin):  
    1414        if name is None:
    1515            name = getattr(file, 'name', None)
    1616        self.name = name
    17         if hasattr(file, 'mode'):
    18             self.mode = file.mode
    1917
    2018    def __str__(self):
    2119        return smart_str(self.name or '')
    class File(FileProxyMixin):  
    5654        return not self.file or self.file.closed
    5755    closed = property(_get_closed)
    5856
     57    @property
     58    def mode(self):
     59        if hasattr(self.file, 'mode'):
     60            return self.file.mode
     61        raise AttributeError("'%s' object has no attribute 'mode'" % self.__class__.__name__)
     62
    5963    def chunks(self, chunk_size=None):
    6064        """
    6165        Read the file and yield chucks of ``chunk_size`` bytes (defaults to
    class File(FileProxyMixin):  
    113117        self.close()
    114118
    115119    def open(self, mode=None):
     120        if not self.closed and mode is not None and mode != self.mode:
     121            # To be reopened in another mode
     122            self.close()
    116123        if not self.closed:
    117124            self.seek(0)
    118125        elif self.name and os.path.exists(self.name):
  • tests/modeltests/files/tests.py

    diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py
    index 3e256f7..6177d0a 100644
    a b class FileStorageTests(TestCase):  
    4949            sorted(files), ["assignment.txt", "default.txt", "django_test.txt"]
    5050        )
    5151
     52        # Test that the file can be opened in writable mode
     53        obj1.normal.open('rb+')
     54        self.assertEqual(obj1.normal.file.mode, 'rb+')
     55        obj1.normal.write(b"content")
     56        obj1.normal.close()
     57
    5258        # Files can be read in a little at a time, if necessary.
    5359        obj1.normal.open()
    5460        self.assertEqual(obj1.normal.read(3), b"con")
Back to Top