Skip to content

Commit 91f64f6

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.13] gh-88574: Skip a spurious blank line after a literal in imaplib (GH-152751) (GH-152886)
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. (cherry picked from commit 53ff1a2) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent f7431eb commit 91f64f6

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

Lib/imaplib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,10 @@ def _get_response(self):
11521152

11531153
dat = self._get_line()
11541154

1155+
# Skip a blank line that some servers send after a literal.
1156+
if dat == b'':
1157+
dat = self._get_line()
1158+
11551159
self._append_untagged(typ, dat)
11561160

11571161
# Bracketed response information?

Lib/test/test_imaplib.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,23 @@ def test_lsub(self):
673673
self.assertEqual(typ, 'OK')
674674
self.assertEqual(server.args, ['~/Mail/', '%'])
675675

676+
def test_extra_blank_line_after_literal(self):
677+
# Some buggy servers send an extra blank line after the counted
678+
# literal data. imaplib should skip it instead of failing.
679+
class BlankLineHandler(SimpleIMAPHandler):
680+
def cmd_FETCH(self, tag, args):
681+
self._send(b'* 1 FETCH (BODY[HEADER] {13}\r\n')
682+
self._send(b'Subject: test') # 13-byte literal
683+
self._send(b'\r\n)\r\n') # stray blank line, then ')'
684+
self._send_tagged(tag, 'OK', 'FETCH completed')
685+
client, _ = self._setup(BlankLineHandler)
686+
client.login('user', 'pass')
687+
client.select()
688+
typ, data = client.fetch('1', '(BODY[HEADER])')
689+
self.assertEqual(typ, 'OK')
690+
self.assertEqual(data, [(b'1 (BODY[HEADER] {13}', b'Subject: test'),
691+
b')'])
692+
676693
def test_unselect(self):
677694
client, server = self._setup(SimpleIMAPHandler)
678695
client.login('user', 'pass')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`imaplib` no longer fails when a server sends a spurious blank line
2+
after the counted data of a literal. Such a blank line is now skipped.

0 commit comments

Comments
 (0)