]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#5610: use \Z not $ so we don't eat extra chars when body part ends with \r\n.
authorR. David Murray <rdmurray@bitdance.com>
Thu, 3 Jun 2010 15:43:20 +0000 (15:43 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Thu, 3 Jun 2010 15:43:20 +0000 (15:43 +0000)
If a body part ended with \r\n, feedparser, using '$' to terminate its
search for the newline, would match on the \r\n, and think that it needed
to strip two characters in order to account for the line end before the
boundary.  That made it chop one too many characters off the end of
the body part.  Using \Z makes the match correct.

Patch and test by Tony Nelson.

Lib/email/feedparser.py
Lib/email/test/test_email.py
Misc/NEWS

index afb02b32b24a2b75f2038f1bdf19c0f6af6b8253..163fadafd4ac7e26f7650b0ed71f44d37bc92c6a 100644 (file)
@@ -28,7 +28,7 @@ from email import message
 
 NLCRE = re.compile('\r\n|\r|\n')
 NLCRE_bol = re.compile('(\r\n|\r|\n)')
-NLCRE_eol = re.compile('(\r\n|\r|\n)$')
+NLCRE_eol = re.compile('(\r\n|\r|\n)\Z')
 NLCRE_crack = re.compile('(\r\n|\r|\n)')
 # RFC 2822 $3.6.8 Optional fields.  ftext is %d33-57 / %d59-126, Any character
 # except controls, SP, and ":".
index 7d0107936cc5f36dc1c087fd02b0be9d52a595f0..94eec86bde7d1d4dbb93b205157a8e457a18b3c1 100644 (file)
@@ -2610,6 +2610,24 @@ Here's the message body
         eq(headers, ['A', 'B', 'CC'])
         eq(msg.get_payload(), 'body')
 
+    def test_CRLFLF_at_end_of_part(self):
+        # issue 5610: feedparser should not eat two chars from body part ending
+        # with "\r\n\n".
+        m = (
+            "From: foo@bar.com\n"
+            "To: baz\n"
+            "Mime-Version: 1.0\n"
+            "Content-Type: multipart/mixed; boundary=BOUNDARY\n"
+            "\n"
+            "--BOUNDARY\n"
+            "Content-Type: text/plain\n"
+            "\n"
+            "body ending with CRLF newline\r\n"
+            "\n"
+            "--BOUNDARY--\n"
+          )
+        msg = email.message_from_string(m)
+        self.assertTrue(msg.get_payload(0).get_payload().endswith('\r\n'))
 
 \f
 class TestBase64(unittest.TestCase):
index 25b265b6c6e506d58b312c158733629a3a7b5096..81891c15a1012bc6e04ee92ed15f7fbf6ef32911 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,9 @@ C-API
 Library
 -------
 
+- Issue #5610: feedparser no longer eats extra characters at the end of
+  a body part if the body part ends with a \r\n.
+
 - Issue #8833: tarfile created hard link entries with a size field != 0 by
   mistake.