Skip to content

Commit 2e3e956

Browse files
author
Forest
committed
imaplib: idle: use deadline idiom when iterating
This simplifies the code, and avoids idle duration drift from time spent processing each iteration.
1 parent 59e0c6a commit 2e3e956

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

Lib/imaplib.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,7 @@ def __init__(self, imap, duration=None):
14191419
if 'IDLE' not in imap.capabilities:
14201420
raise imap.error("Server does not support IMAP4 IDLE")
14211421
self._duration = duration
1422+
self._deadline = None
14221423
self._imap = imap
14231424
self._tag = None
14241425
self._sock_timeout = None
@@ -1459,6 +1460,9 @@ def __enter__(self):
14591460
if self._sock_timeout is not None:
14601461
imap.sock.settimeout(None) # Socket timeout would break IDLE
14611462

1463+
if self._duration is not None:
1464+
self._deadline = time.monotonic() + self._duration
1465+
14621466
self._old_state = imap.state
14631467
imap.state = 'IDLING'
14641468

@@ -1582,24 +1586,17 @@ def burst(self, interval=0.1):
15821586
except StopIteration:
15831587
return
15841588

1585-
start = time.monotonic()
1586-
15871589
while response := self._pop(interval, None):
15881590
yield response
15891591

1590-
if self._duration is not None:
1591-
elapsed = time.monotonic() - start
1592-
self._duration = max(self._duration - elapsed, 0)
1593-
15941592
def __next__(self):
15951593
imap = self._imap
1596-
start = time.monotonic()
1597-
1598-
typ, data = self._pop(self._duration)
15991594

1600-
if self._duration is not None:
1601-
elapsed = time.monotonic() - start
1602-
self._duration = max(self._duration - elapsed, 0)
1595+
if self._duration is None:
1596+
timeout = None
1597+
else:
1598+
timeout = self._deadline - time.monotonic()
1599+
typ, data = self._pop(timeout)
16031600

16041601
if not typ:
16051602
if __debug__ and imap.debug >= 4:

0 commit comments

Comments
 (0)