Skip to content

Commit 53ff1a2

Browse files
gh-88574: Skip a spurious blank line after a literal in imaplib (GH-152751)
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>
1 parent 753f82d commit 53ff1a2

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
@@ -1301,6 +1301,10 @@ def _get_response(self, start_timeout=False):
13011301

13021302
dat = self._get_line()
13031303

1304+
# Skip a blank line that some servers send after a literal.
1305+
if dat == b'':
1306+
dat = self._get_line()
1307+
13041308
self._append_untagged(typ, dat)
13051309

13061310
# Bracketed response information?

Lib/test/test_imaplib.py

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

867+
def test_extra_blank_line_after_literal(self):
868+
# Some buggy servers send an extra blank line after the counted
869+
# literal data. imaplib should skip it instead of failing.
870+
class BlankLineHandler(SimpleIMAPHandler):
871+
def cmd_FETCH(self, tag, args):
872+
self._send(b'* 1 FETCH (BODY[HEADER] {13}\r\n')
873+
self._send(b'Subject: test') # 13-byte literal
874+
self._send(b'\r\n)\r\n') # stray blank line, then ')'
875+
self._send_tagged(tag, 'OK', 'FETCH completed')
876+
client, _ = self._setup(BlankLineHandler)
877+
client.login('user', 'pass')
878+
client.select()
879+
typ, data = client.fetch('1', '(BODY[HEADER])')
880+
self.assertEqual(typ, 'OK')
881+
self.assertEqual(data, [(b'1 (BODY[HEADER] {13}', b'Subject: test'),
882+
b')'])
883+
867884
def test_unselect(self):
868885
client, server = self._setup(SimpleIMAPHandler)
869886
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)