Skip to content

Commit

Permalink
[1.6.x] Fixed #22107 -- Fixed django.core.files.File object iteration.
Browse files Browse the repository at this point in the history
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 3841fee from master.
  • Loading branch information
bmispelon committed Feb 20, 2014
1 parent e56ce87 commit 12da690
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/core/files/base.py
Expand Up @@ -103,7 +103,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
Expand Down
9 changes: 9 additions & 0 deletions tests/files/tests.py
@@ -1,5 +1,6 @@
from __future__ import absolute_import

from io import BytesIO
import os
import gzip
import shutil
Expand Down Expand Up @@ -164,6 +165,14 @@ def test_file_mode(self):
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):
Expand Down

0 comments on commit 12da690

Please sign in to comment.