diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 497b5a60cecb083..775a623b32f6403 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -1280,6 +1280,10 @@ def _get_response(self, start_timeout=False): 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? diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index fb256fb7cbcd344..20691b4fe02b8f1 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -655,6 +655,23 @@ def cmd_LSUB(self, tag, args): self.assertEqual(typ, 'OK') self.assertEqual(data[0], b'() "." directoryA') + 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, _ = 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 index 000000000000000..8d95bbbb5a15e46 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-01-10-00-00.gh-issue-88574.Kz3wQm.rst @@ -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.