]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-88574: Skip a spurious blank line after a literal in imaplib (GH-152751)
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 2 Jul 2026 12:55:58 +0000 (15:55 +0300)
committerGitHub <noreply@github.com>
Thu, 2 Jul 2026 12:55:58 +0000 (15:55 +0300)
Some IMAP servers send an extra blank line after the data of a literal.
imaplib mistook it for the response trailer and failed on the next
command.  Such a blank line is now skipped.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Lib/imaplib.py
Lib/test/test_imaplib.py
Misc/NEWS.d/next/Library/2026-07-01-10-00-00.gh-issue-88574.Kz3wQm.rst [new file with mode: 0644]

index 239218bc96aeb4f5ac2916305d4cca372e8e0c95..799c9dd529c7d9abf21d5f300ebb1d250d60f978 100644 (file)
@@ -1301,6 +1301,10 @@ class IMAP4:
 
                 dat = self._get_line()
 
+                # Skip a blank line that some servers send after a literal.
+                if dat == b'':
+                    dat = self._get_line()
+
             self._append_untagged(typ, dat)
 
         # Bracketed response information?
index 3a3f62ef1ea6abb35756a048622163e60d2ade9e..c8dcf95be33b6330a2764b423c7453c5a29b9555 100644 (file)
@@ -864,6 +864,23 @@ class NewIMAPTestsMixin:
         self.assertEqual(typ, 'OK')
         self.assertEqual(server.args, ['~/Mail/', '%'])
 
+    def test_extra_blank_line_after_literal(self):
+        # Some buggy servers send an extra blank line after the counted
+        # literal data.  imaplib should skip it instead of failing.
+        class BlankLineHandler(SimpleIMAPHandler):
+            def cmd_FETCH(self, tag, args):
+                self._send(b'* 1 FETCH (BODY[HEADER] {13}\r\n')
+                self._send(b'Subject: test')       # 13-byte literal
+                self._send(b'\r\n)\r\n')            # stray blank line, then ')'
+                self._send_tagged(tag, 'OK', 'FETCH completed')
+        client, _ = self._setup(BlankLineHandler)
+        client.login('user', 'pass')
+        client.select()
+        typ, data = client.fetch('1', '(BODY[HEADER])')
+        self.assertEqual(typ, 'OK')
+        self.assertEqual(data, [(b'1 (BODY[HEADER] {13}', b'Subject: test'),
+                                b')'])
+
     def test_unselect(self):
         client, server = self._setup(SimpleIMAPHandler)
         client.login('user', 'pass')
diff --git a/Misc/NEWS.d/next/Library/2026-07-01-10-00-00.gh-issue-88574.Kz3wQm.rst b/Misc/NEWS.d/next/Library/2026-07-01-10-00-00.gh-issue-88574.Kz3wQm.rst
new file mode 100644 (file)
index 0000000..8d95bbb
--- /dev/null
@@ -0,0 +1,2 @@
+:mod:`imaplib` no longer fails when a server sends a spurious blank line
+after the counted data of a literal.  Such a blank line is now skipped.