From c1a09a4237f8af777560789adfef18b2e72672f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natal=20Ng=C3=A9tal?= Date: Tue, 4 Dec 2018 18:03:19 +0100 Subject: [PATCH] bpo-5430: Preservation LF, CR in IMAP4 append method. Preserve LF, CR in the append method. Co-authored-by: Ron DuPlain --- Lib/imaplib.py | 3 +-- Lib/test/test_imaplib.py | 19 +++++++++++++++++++ .../2018-12-04-18-10-11.bpo-5430.5adoEJ.rst | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-12-04-18-10-11.bpo-5430.5adoEJ.rst diff --git a/Lib/imaplib.py b/Lib/imaplib.py index dd237f7704aca0b..c5967c09a28ffd0 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -111,7 +111,6 @@ br'"') # Literal is no longer used; kept for backward compatibility. Literal = re.compile(br'.*{(?P\d+)}$', re.ASCII) -MapCRLF = re.compile(br'\r\n|\r|\n') # We no longer exclude the ']' character from the data portion of the response # code, even though it violates the RFC. Popular IMAP servers such as Gmail # allow flags with ']', and there are programs (including imaplib!) that can @@ -396,7 +395,7 @@ def append(self, mailbox, flags, date_time, message): date_time = Time2Internaldate(date_time) else: date_time = None - literal = MapCRLF.sub(CRLF, message) + literal = message if self.utf8_enabled: literal = b'UTF8 (' + literal + b')' self.literal = literal diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index a0b598d0c784092..d66904dc1dd2d29 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -22,9 +22,28 @@ CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert3.pem") CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "pycacert.pem") +class DummyIMAP(imaplib.IMAP4): + + def __init__(self): + """Skip connections, for testing.""" + self.utf8_enabled = False + + def _simple_command(self, name, *args): + """Return self.literal, for testing methods affecting literal.""" + return self.literal + class TestImaplib(unittest.TestCase): + # CR, LF in literal should not be replaced by CRLF, issue 5430 + def test_cr_lf_literal_preservation(self): + dummy_imap = DummyIMAP() + msg_string = 'one\rtwo\nthree\r\n' + result = dummy_imap.append( + None, None, None, msg_string.encode('utf-8')) + + self.assertEqual(str.encode(msg_string), result) + def test_Internaldate2tuple(self): t0 = calendar.timegm((2000, 1, 1, 0, 0, 0, -1, -1, -1)) tt = imaplib.Internaldate2tuple( diff --git a/Misc/NEWS.d/next/Library/2018-12-04-18-10-11.bpo-5430.5adoEJ.rst b/Misc/NEWS.d/next/Library/2018-12-04-18-10-11.bpo-5430.5adoEJ.rst new file mode 100644 index 000000000000000..bedfd80bfd3db72 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-04-18-10-11.bpo-5430.5adoEJ.rst @@ -0,0 +1 @@ +Preserve LF, CR in the ``imaplib.IMAP4.append`` method.