]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#11062: Fix universal newline support in Babyl._install_message()
authorPetri Lehtinen <petri@digip.org>
Thu, 16 Aug 2012 04:22:15 +0000 (07:22 +0300)
committerPetri Lehtinen <petri@digip.org>
Thu, 16 Aug 2012 04:27:01 +0000 (07:27 +0300)
When adding a message from a binary file, \r\n was translated to
\r\r\n in the message body.

Lib/mailbox.py

index 73fe7d648330e025df80c822d2369417379ac6dd..282c0553ce9f14ce3e6477226278ec1b1a4955b3 100644 (file)
@@ -1450,10 +1450,17 @@ class Babyl(_singlefileMailbox):
                     else:
                         break
             while True:
-                buffer = message.read(4096)     # Buffer size is arbitrary.
-                if not buffer:
+                line = message.readline()
+                if not line:
                     break
-                self._file.write(buffer.replace(b'\n', linesep))
+                # Universal newline support.
+                if line.endswith(b'\r\n'):
+                    line = line[:-2] + linesep
+                elif line.endswith(b'\r'):
+                    line = line[:-1] + linesep
+                elif line.endswith(b'\n'):
+                    line = line[:-1] + linesep
+                self._file.write(line)
         else:
             raise TypeError('Invalid message type: %s' % type(message))
         stop = self._file.tell()