Commits

Anonymous committed 2c09c31

file upload streams no longer have to provide a truncate()
method.

Comments (0)

Files changed (2)

 
 - fixed boolean check of :class:`FieldStorage`
 - url routing system properly supports unicode URL rules now.
+- file upload streams no longer have to provide a truncate()
+  method.
 
 
 Version 0.5
             else:
                 stream = StringIO()
 
-            newline_length = 0
+            buf = ''
             for line in iterator:
                 if line[:2] == '--':
                     terminator = line.rstrip()
                         line = line.decode(transfer_encoding)
                     except:
                         raise ValueError('could not base 64 decode chunk')
+                # we have something in the buffer from the last iteration.
+                # write that value to the output stream now and clear the buffer.
+                if buf:
+                    stream.write(buf)
+                    buf = ''
+
+                # If the line ends with windows CRLF we write everything except
+                # the last two bytes.  In all other cases however we write everything
+                # except the last byte.  If it was a newline, that's fine, otherwise
+                # it does not matter because we write it the last iteration.  If the
+                # loop aborts early because the end of a part was reached, the last
+                # newline is not written which is exactly what we want.
                 newline_length = line[-2:] == '\r\n' and 2 or 1
-                stream.write(line)
+                stream.write(line[:-newline_length])
+                buf = line[-newline_length:]
                 if not is_file and max_form_memory_size is not None:
                     in_memory += len(line)
                     if in_memory > max_form_memory_size:
             else:
                 raise ValueError('unexpected end of part')
 
-            # chop off the trailing line terminator and rewind
-            stream.seek(-newline_length, 1)
-            stream.truncate()
+            # rewind the stream
             stream.seek(0)
 
             if is_file: