mirror of https://github.com/django/django.git
[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 3841feee86
from master.
This commit is contained in:
parent
e56ce87bd8
commit
12da6902e9
|
@ -103,7 +103,7 @@ class File(FileProxyMixin):
|
||||||
|
|
||||||
# If this is the end of a line, yield
|
# If this is the end of a line, yield
|
||||||
# otherwise, wait for the next round
|
# otherwise, wait for the next round
|
||||||
if line[-1] in ('\n', '\r'):
|
if line[-1:] in (b'\n', b'\r'):
|
||||||
yield line
|
yield line
|
||||||
else:
|
else:
|
||||||
buffer_ = line
|
buffer_ = line
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from io import BytesIO
|
||||||
import os
|
import os
|
||||||
import gzip
|
import gzip
|
||||||
import shutil
|
import shutil
|
||||||
|
@ -164,6 +165,14 @@ class FileTests(unittest.TestCase):
|
||||||
self.assertFalse(hasattr(file, 'mode'))
|
self.assertFalse(hasattr(file, 'mode'))
|
||||||
g = gzip.GzipFile(fileobj=file)
|
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):
|
class FileMoveSafeTests(unittest.TestCase):
|
||||||
def test_file_move_overwrite(self):
|
def test_file_move_overwrite(self):
|
||||||
|
|
Loading…
Reference in New Issue