Commit 3841feee authored by Baptiste Mispelon's avatar Baptiste Mispelon
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.
parent cb844497
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -104,7 +104,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
+8 −0
Original line number Diff line number Diff line
@@ -64,6 +64,14 @@ class FileTests(unittest.TestCase):
        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):
    """