Fixed #24963 -- Added File.seekable() on Python 3.
This commit is contained in:
parent
3c593ba79e
commit
e93e0c03b2
|
@ -1,3 +1,6 @@
|
||||||
|
from django.utils import six
|
||||||
|
|
||||||
|
|
||||||
class FileProxyMixin(object):
|
class FileProxyMixin(object):
|
||||||
"""
|
"""
|
||||||
A mixin class used to forward file methods to an underlaying file
|
A mixin class used to forward file methods to an underlaying file
|
||||||
|
@ -24,6 +27,8 @@ class FileProxyMixin(object):
|
||||||
write = property(lambda self: self.file.write)
|
write = property(lambda self: self.file.write)
|
||||||
writelines = property(lambda self: self.file.writelines)
|
writelines = property(lambda self: self.file.writelines)
|
||||||
xreadlines = property(lambda self: self.file.xreadlines)
|
xreadlines = property(lambda self: self.file.xreadlines)
|
||||||
|
if six.PY3:
|
||||||
|
seekable = property(lambda self: self.file.seekable)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(self.file)
|
return iter(self.file)
|
||||||
|
|
|
@ -89,7 +89,12 @@ The ``File`` Class
|
||||||
the following attributes and methods of its ``file`` object:
|
the following attributes and methods of its ``file`` object:
|
||||||
``encoding``, ``fileno``, ``flush``, ``isatty``, ``newlines``,
|
``encoding``, ``fileno``, ``flush``, ``isatty``, ``newlines``,
|
||||||
``read``, ``readinto``, ``readlines``, ``seek``, ``softspace``, ``tell``,
|
``read``, ``readinto``, ``readlines``, ``seek``, ``softspace``, ``tell``,
|
||||||
``truncate``, ``writelines``, ``xreadlines``.
|
``truncate``, ``writelines``, ``xreadlines``. If you are using
|
||||||
|
Python 3, the ``seekable`` method is also available.
|
||||||
|
|
||||||
|
.. versionchanged:: 1.9
|
||||||
|
|
||||||
|
The ``seekable`` method was added.
|
||||||
|
|
||||||
.. currentmodule:: django.core.files.base
|
.. currentmodule:: django.core.files.base
|
||||||
|
|
||||||
|
|
|
@ -200,6 +200,9 @@ File Storage
|
||||||
<django.core.files.storage.Storage.get_valid_name>` is now called when
|
<django.core.files.storage.Storage.get_valid_name>` is now called when
|
||||||
the :attr:`~django.db.models.FileField.upload_to` is a callable.
|
the :attr:`~django.db.models.FileField.upload_to` is a callable.
|
||||||
|
|
||||||
|
* :class:`~django.core.files.File` now has the ``seekable()`` method when using
|
||||||
|
Python 3.
|
||||||
|
|
||||||
File Uploads
|
File Uploads
|
||||||
^^^^^^^^^^^^
|
^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -120,6 +120,19 @@ class FileTests(unittest.TestCase):
|
||||||
f = File(StringIO('one\ntwo\nthree'))
|
f = File(StringIO('one\ntwo\nthree'))
|
||||||
self.assertEqual(list(f), ['one\n', 'two\n', 'three'])
|
self.assertEqual(list(f), ['one\n', 'two\n', 'three'])
|
||||||
|
|
||||||
|
def test_seekable(self):
|
||||||
|
"""
|
||||||
|
File.seekable() should be available on Python 3.
|
||||||
|
"""
|
||||||
|
with tempfile.TemporaryFile() as temp:
|
||||||
|
temp.write(b"contents\n")
|
||||||
|
test_file = File(temp, name="something.txt")
|
||||||
|
if six.PY2:
|
||||||
|
self.assertFalse(hasattr(test_file, 'seekable'))
|
||||||
|
if six.PY3:
|
||||||
|
self.assertTrue(hasattr(test_file, 'seekable'))
|
||||||
|
self.assertTrue(test_file.seekable())
|
||||||
|
|
||||||
|
|
||||||
class NoNameFileTestCase(unittest.TestCase):
|
class NoNameFileTestCase(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue