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.
This commit is contained in:
Jon Dufresne 2014-09-29 18:24:33 -07:00 committed by Tim Graham
parent eab3dc195e
commit eb4f6de980
5 changed files with 108 additions and 12 deletions

View file

@ -53,6 +53,15 @@ The ``File`` Class
Iterate over the file yielding one line at a time.
.. versionchanged:: 1.8
``File`` now uses `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'``.
.. _universal newlines: http://www.python.org/dev/peps/pep-0278
.. method:: chunks([chunk_size=None])
Iterate over the file yielding "chunks" of a given size. ``chunk_size``

View file

@ -82,10 +82,15 @@ Here are some useful attributes of ``UploadedFile``:
for line in uploadedfile:
do_something_with(line)
However, *unlike* standard Python files, :class:`UploadedFile` only
understands ``\n`` (also known as "Unix-style") line endings. If you know
that you need to handle uploaded files with different line endings, you'll
need to do so in your view.
Lines are split using `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'``.
.. _universal newlines: http://www.python.org/dev/peps/pep-0278
.. versionchanged:: 1.8
Previously lines were only split on the Unix end-of-line ``'\n'``.
Subclasses of ``UploadedFile`` include:

View file

@ -659,6 +659,13 @@ Miscellaneous
* By default, :ref:`call_command <call-command>` now always skips the check
framework (unless you pass it ``skip_checks=False``).
* When iterating over lines, :class:`~django.core.files.File` now uses
`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'``.
.. _universal newlines: http://www.python.org/dev/peps/pep-0278
.. _deprecated-features-1.8:
Features deprecated in 1.8