diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2014-09-29 18:24:33 -0700 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-10-30 11:52:59 -0400 |
commit | eb4f6de980c5148ba48d4ed67f31cca27dd132a8 (patch) | |
tree | 787358de9347c8fcfd0bb5fa8c12778f90bf15d3 /tests/files | |
parent | eab3dc195eff4123460fb99d7819a7f6627e7115 (diff) | |
download | django-eb4f6de980c5148ba48d4ed67f31cca27dd132a8.tar.gz |
Fixed #8149 -- Made File.__iter__() support universal newlines.
The following are recognized as ending a line: the Unix end-of-line
convention '\n', the Windows convention '\r\n', and the old
Macintosh convention '\r'.
http://www.python.org/dev/peps/pep-0278
Thanks tchaumeny for review.
Diffstat (limited to 'tests/files')
-rw-r--r-- | tests/files/tests.py | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/tests/files/tests.py b/tests/files/tests.py index be243b2527..f2f1df3626 100644 --- a/tests/files/tests.py +++ b/tests/files/tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from io import BytesIO +from io import BytesIO, StringIO import os import gzip import tempfile @@ -72,6 +72,54 @@ class FileTests(unittest.TestCase): file = File(BytesIO(b'one\ntwo\nthree')) self.assertEqual(list(file), [b'one\n', b'two\n', b'three']) + def test_file_iteration_windows_newlines(self): + """ + #8149 - File objects with \r\n line endings should yield lines + when iterated over. + """ + f = File(BytesIO(b'one\r\ntwo\r\nthree')) + self.assertEqual(list(f), [b'one\r\n', b'two\r\n', b'three']) + + def test_file_iteration_mac_newlines(self): + """ + #8149 - File objects with \r line endings should yield lines + when iterated over. + """ + f = File(BytesIO(b'one\rtwo\rthree')) + self.assertEqual(list(f), [b'one\r', b'two\r', b'three']) + + def test_file_iteration_mixed_newlines(self): + f = File(BytesIO(b'one\rtwo\nthree\r\nfour')) + self.assertEqual(list(f), [b'one\r', b'two\n', b'three\r\n', b'four']) + + def test_file_iteration_with_unix_newline_at_chunk_boundary(self): + f = File(BytesIO(b'one\ntwo\nthree')) + # Set chunk size to create a boundary after \n: + # b'one\n... + # ^ + f.DEFAULT_CHUNK_SIZE = 4 + self.assertEqual(list(f), [b'one\n', b'two\n', b'three']) + + def test_file_iteration_with_windows_newline_at_chunk_boundary(self): + f = File(BytesIO(b'one\r\ntwo\r\nthree')) + # Set chunk size to create a boundary between \r and \n: + # b'one\r\n... + # ^ + f.DEFAULT_CHUNK_SIZE = 4 + self.assertEqual(list(f), [b'one\r\n', b'two\r\n', b'three']) + + def test_file_iteration_with_mac_newline_at_chunk_boundary(self): + f = File(BytesIO(b'one\rtwo\rthree')) + # Set chunk size to create a boundary after \r: + # b'one\r... + # ^ + f.DEFAULT_CHUNK_SIZE = 4 + self.assertEqual(list(f), [b'one\r', b'two\r', b'three']) + + def test_file_iteration_with_text(self): + f = File(StringIO('one\ntwo\nthree')) + self.assertEqual(list(f), ['one\n', 'two\n', 'three']) + class NoNameFileTestCase(unittest.TestCase): """ |