Skip to content

Commit 9317d52

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-88574: Skip a spurious blank line after a literal in imaplib (GH-152751) (GH-152884)
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 d8493ae commit 9317d52

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

12961296
dat = self._get_line()
12971297

1298+
# Skip a blank line that some servers send after a literal.
1299+
if dat == b'':
1300+
dat = self._get_line()
1301+
12981302
self._append_untagged(typ, dat)
12991303

13001304
# Bracketed response information?

Lib/test/test_imaplib.py

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

852+
def test_extra_blank_line_after_literal(self):
853+
# Some buggy servers send an extra blank line after the counted
854+
# literal data. imaplib should skip it instead of failing.
855+
class BlankLineHandler(SimpleIMAPHandler):
856+
def cmd_FETCH(self, tag, args):
857+
self._send(b'* 1 FETCH (BODY[HEADER] {13}\r\n')
858+
self._send(b'Subject: test') # 13-byte literal
859+
self._send(b'\r\n)\r\n') # stray blank line, then ')'
860+
self._send_tagged(tag, 'OK', 'FETCH completed')
861+
client, _ = self._setup(BlankLineHandler)
862+
client.login('user', 'pass')
863+
client.select()
864+
typ, data = client.fetch('1', '(BODY[HEADER])')
865+
self.assertEqual(typ, 'OK')
866+
self.assertEqual(data, [(b'1 (BODY[HEADER] {13}', b'Subject: test'),
867+
b')'])
868+
852869
def test_unselect(self):
853870
client, server = self._setup(SimpleIMAPHandler)
854871
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)