Opened 16 years ago

Closed 10 years ago

#8149 closed Cleanup/optimization (fixed)

UploadedFile doesn't iterate over lines with \r line endings

Reported by: Tai Lee Owned by: nobody
Component: File uploads/storage Version: dev
Severity: Normal Keywords: UploadedFile
Cc: real.human@…, timograham@…, jon.dufresne@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

UploadedFile.__iter__ loops over chunks and converts each chunk to a StringIO object and then iterates over that. Iterating over StringIO objects only splits on \n, and I'm not sure why we need StringIO at all. Couldn't we just do for line in chunk.splitlines() (which does split on \r and \n).

Attachments (2)

UploadedFile-r8225.diff (593 bytes ) - added by Tai Lee 16 years ago.
fix iteration of UploadedFile objects on newlines.
UploadedFile-r8226.diff (711 bytes ) - added by Tai Lee 16 years ago.
Updated for r8654 and fixed regular expression to not match empty strings.

Download all attachments as: .zip

Change History (20)

by Tai Lee, 16 years ago

Attachment: UploadedFile-r8225.diff added

fix iteration of UploadedFile objects on newlines.

comment:1 by Tai Lee, 16 years ago

Has patch: set

comment:2 by Tai Lee, 16 years ago

Cc: real.human@… added

comment:3 by Tai Lee, 16 years ago

Looks like using splitlines will strip the line endings and break binary uploads. Here's a new patch that iterates through a call to re.findall with a simple regular expression instead of a StringIO object.

comment:4 by Tai Lee, 16 years ago

Here's a use-case for this change. Upload a CSV file (which can have \r or \r\n line endings), and pass the UploadedFile object to csv.reader and you get Error: newline inside string when you try to iterate over it. When working with real files on the local file system you can open them with universal newline support, which is not available with StringIO or cStringIO objects.

comment:5 by Eric Holscher, 16 years ago

Triage Stage: UnreviewedAccepted

by Tai Lee, 16 years ago

Attachment: UploadedFile-r8226.diff added

Updated for r8654 and fixed regular expression to not match empty strings.

comment:6 by Malcolm Tredinnick, 16 years ago

Resolution: wontfix
Status: newclosed

Universal newline handling only makes sense for files generated/stored on the local system, where the newline convention is known. The UploadedFile class, pretty much by definition, is for data that has come from, and been generated on, a remote system that we know nothing about. So trying to use the appropriate line-ending handling is an unattainable goal for that situation. #8656 is the ticket to make sure this is documented.

comment:7 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

comment:8 by Tai Lee, 12 years ago

Component: FormsFile uploads/storage
Easy pickings: unset
Resolution: wontfix
Severity: Normal
Status: closedreopened
Type: Uncategorized
UI/UX: unset

I'm re-opening this ticket because I have found a much simpler implementation, and this is still a problem for anyone who needs to parse uploaded text files line-by-line (e.g. CSV) when they can't know what line terminators end-users are using in their files. Excel 2011 on OS X is at least one commonly used program that still saves CSV data with \r line terminators by default.

Originally, there was a discussion between Malcolm and Jacob on IRC and the strongest objection at the time to fixing this was the ugly regular expression in my previous patch. This was also during a busy time, and it was decided that this should be a documentation issue.

I don't think the explanation given above when this ticket was wontfixed actually makes much sense. Uploaded file data which has come from and been generated on a remote system that we know nothing about is precisely the reason why we *need* universal newline support when iterating those files line-by-line.

I'm not sure how I missed it before, but when I said that we couldn't just use splitlines() because it consumed the line terminators, it seems I was wrong. splitlines() has a keepends argument that will actually do exactly what we need without any ugly regular expressions. Perhaps this wasn't the case back then, when we still had to support Python 2.3 and 2.4. I couldn't find confirmation in the Python 2.3 and 2.4 docs, but I did confirm that splitlines(True) works in Python 2.5 which is now our minimum version number.

I've fixed this in a branch on GitHub, and opened a pull request. All tests pass under OS X Lion with Python 2.7.2 and SQLite.

https://github.com/thirstydigital/django/tree/tickets/8149-universal-newline-uploads

https://github.com/django/django/pull/320

comment:9 by Claude Paroz, 12 years ago

Needs tests: set

This iter method appears to be untested.

Version 0, edited 12 years ago by Claude Paroz (next)

comment:10 by Tai Lee, 12 years ago

Sorry. Don't know why I thought this didn't need tests. New pull request with test.

https://github.com/django/django/pull/323

comment:11 by Tai Lee, 12 years ago

Updated versionchanged note to 1.6. Any feedback?

comment:12 by Aymeric Augustin, 12 years ago

Type: UncategorizedCleanup/optimization

comment:13 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:14 by Tim Graham, 11 years ago

Cc: timograham@… added
Needs tests: unset
Patch needs improvement: set

The test doesn't pass on Python 3.

comment:15 by Jon Dufresne, 10 years ago

Cc: jon.dufresne@… added

I've been running into this issue as well. Most notably when parsing uploaded files as CSV from Mac.

I added a submitted a pull request that works with Python 2 and 3: https://github.com/django/django/pull/3291

comment:16 by Jon Dufresne, 10 years ago

Patch needs improvement: unset

Sorry in advance if this is incorrect usage of this flag.

I am removing "Patch needs improvement" as my latest pull request does work with Python 3 and there are no outstanding requests from a review.

comment:17 by Tim Graham, 10 years ago

Triage Stage: AcceptedReady for checkin

@jdufresne - that's correct usage. In fact, since tchaumeny reviewed it, he could mark the ticket as "ready for checkin".

comment:18 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In eb4f6de980c5148ba48d4ed67f31cca27dd132a8:

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.

Note: See TracTickets for help on using tickets.
Back to Top