Permalink
Browse files

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.
  • Loading branch information...
bmispelon committed Feb 20, 2014
1 parent cb84449 commit 3841feee86cae65165f120db7a5d80ffc76dd520
Showing with 9 additions and 1 deletion.
  1. +1 −1 django/core/files/base.py
  2. +8 −0 tests/files/tests.py
@@ -104,7 +104,7 @@ def __iter__(self):
# 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
View
@@ -64,6 +64,14 @@ def test_file_mode(self):
self.assertFalse(hasattr(file, 'mode'))
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 NoNameFileTestCase(unittest.TestCase):
"""

0 comments on commit 3841fee

Please sign in to comment.