Unverified Commit 4f474607 authored by Simon Charette's avatar Simon Charette
Browse files

Fixed #26646 -- Added IOBase methods required by TextIOWrapper to File.

Thanks Tim for the review.
parent 6ab0d135
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -64,10 +64,6 @@ class File(FileProxyMixin):

    size = property(_get_size, _set_size)

    def _get_closed(self):
        return not self.file or self.file.closed
    closed = property(_get_closed)

    def chunks(self, chunk_size=None):
        """
        Read the file and yield chunks of ``chunk_size`` bytes (defaults to
+0 −9
Original line number Diff line number Diff line
@@ -58,15 +58,6 @@ 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()

+25 −5
Original line number Diff line number Diff line
from django.utils import six


class FileProxyMixin(object):
    """
    A mixin class used to forward file methods to an underlaying file
@@ -27,8 +24,31 @@ class FileProxyMixin(object):
    write = property(lambda self: self.file.write)
    writelines = property(lambda self: self.file.writelines)
    xreadlines = property(lambda self: self.file.xreadlines)
    if six.PY3:
        seekable = property(lambda self: self.file.seekable)

    @property
    def closed(self):
        return not self.file or self.file.closed

    def readable(self):
        if self.closed:
            return False
        if hasattr(self.file, 'readable'):
            return self.file.readable()
        return True

    def writable(self):
        if self.closed:
            return False
        if hasattr(self.file, 'writable'):
            return self.file.writable()
        return 'w' in getattr(self.file, 'mode', '')

    def seekable(self):
        if self.closed:
            return False
        if hasattr(self.file, 'seekable'):
            return self.file.seekable()
        return True

    def __iter__(self):
        return iter(self.file)
+7 −2
Original line number Diff line number Diff line
@@ -92,8 +92,13 @@ The ``File`` class
    the following attributes and methods of its ``file`` object:
    ``encoding``, ``fileno``, ``flush``, ``isatty``, ``newlines``,
    ``read``, ``readinto``, ``readlines``, ``seek``, ``softspace``, ``tell``,
    ``truncate``, ``writelines``, ``xreadlines``. If you are using
    Python 3, the ``seekable`` method is also available.
    ``truncate``, ``writelines``, ``xreadlines``, ``readable()``,
    ``writable()``, and ``seekable()``.

    .. versionchanged:: 1.11

        The ``readable()`` and ``writable()`` methods were added and the
        ``seekable()`` method was made available on Python 2.

.. currentmodule:: django.core.files.base

+3 −1
Original line number Diff line number Diff line
@@ -122,7 +122,9 @@ Email
File Storage
~~~~~~~~~~~~

* ...
* To make it wrappable by :class:`io.TextIOWrapper`,
  :class:`~django.core.files.File` now has the ``readable()``, ``writable()``,
  and ``seekable()`` methods.

File Uploads
~~~~~~~~~~~~
Loading