Skip to content

Commit dcd0161

Browse files
author
Forest
committed
imaplib: downgrade idle-denied exception to error
This makes it easier for client code to distinguish a temporary rejection of the IDLE command from a server responding incorrectly to IDLE.
1 parent b767ab6 commit dcd0161

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Lib/imaplib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,9 @@ def __enter__(self):
14131413
# this occurs.
14141414
while resp := imap._get_response():
14151415
if imap.tagged_commands[self._tag]:
1416+
typ, data = imap.tagged_commands.pop(self._tag)
1417+
if typ == 'NO':
1418+
raise imap.error(f'idle denied: {data}')
14161419
raise imap.abort(f'unexpected status response: {resp}')
14171420

14181421
if __debug__ and imap.debug >= 4:

Lib/test/test_imaplib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ def cmd_UNSELECT(self, tag, args):
208208
self._send_tagged(tag, 'BAD', 'No mailbox selected')
209209

210210

211+
class IdleCmdDenyHandler(SimpleIMAPHandler):
212+
capabilities = 'IDLE'
213+
def cmd_IDLE(self, tag, args):
214+
self._send_tagged(tag, 'NO', 'IDLE is not allowed at this time')
215+
216+
211217
class IdleCmdHandler(SimpleIMAPHandler):
212218
capabilities = 'IDLE'
213219
def cmd_IDLE(self, tag, args):
@@ -529,6 +535,13 @@ def test_idle_capability(self):
529535
with client.idle():
530536
pass
531537

538+
def test_idle_denied(self):
539+
client, _ = self._setup(IdleCmdDenyHandler)
540+
client.login('user', 'pass')
541+
with self.assertRaises(imaplib.IMAP4.error):
542+
with client.idle() as idler:
543+
pass
544+
532545
def test_idle_iter(self):
533546
client, _ = self._setup(IdleCmdHandler)
534547
client.login('user', 'pass')

0 commit comments

Comments
 (0)