summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBaptiste Mispelon <bmispelon@gmail.com>2014-02-20 19:13:25 +0100
committerBaptiste Mispelon <bmispelon@gmail.com>2014-02-20 23:59:51 +0100
commit12da6902e9cd8de2bc9eeef08c850a6c79aaa7cc (patch)
tree3065865ad7e911c35df643ffd05f297c5b0249f0
parente56ce87bd8cb85a14d34e7bdfac4b55fac95faea (diff)
[1.6.x] Fixed #22107 -- Fixed django.core.files.File object iteration.
Due to a mixup between text and bytes, iteration over a File instance was broken under Python 3. Thanks to trac user pdewacht for the report and patch. Backport of 3841feee86cae65165f120db7a5d80ffc76dd520 from master.
-rw-r--r--django/core/files/base.py2
-rw-r--r--tests/files/tests.py9
2 files changed, 10 insertions, 1 deletions
diff --git a/django/core/files/base.py b/django/core/files/base.py
index 2d10100019..f07b2b4584 100644
--- a/django/core/files/base.py
+++ b/django/core/files/base.py
@@ -103,7 +103,7 @@ class File(FileProxyMixin):
# If this is the end of a line, yield
# otherwise, wait for the next round
- if line[-1] in ('\n', '\r'):
+ if line[-1:] in (b'\n', b'\r'):
yield line
else:
buffer_ = line
diff --git a/tests/files/tests.py b/tests/files/tests.py
index daf5a30e9d..2766ed1630 100644
--- a/tests/files/tests.py
+++ b/tests/files/tests.py
@@ -1,5 +1,6 @@
from __future__ import absolute_import
+from io import BytesIO
import os
import gzip
import shutil
@@ -164,6 +165,14 @@ class FileTests(unittest.TestCase):
self.assertFalse(hasattr(file, 'mode'))
g = gzip.GzipFile(fileobj=file)
+ def test_file_iteration(self):
+ """
+ File objects should yield lines when iterated over.
+ Refs #22107.
+ """
+ file = File(BytesIO(b'one\ntwo\nthree'))
+ self.assertEqual(list(file), [b'one\n', b'two\n', b'three'])
+
class FileMoveSafeTests(unittest.TestCase):
def test_file_move_overwrite(self):