Skip to content

Commit 2ff68dd

Browse files
committed
Close #19339: telnetlib module is now using time.monotonic() when available to
compute timeout.
1 parent 3d2f68d commit 2ff68dd

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

Lib/telnetlib.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
import sys
3939
import socket
4040
import select
41+
try:
42+
from time import monotonic as _time
43+
except ImportError:
44+
from time import time as _time
4145

4246
__all__ = ["Telnet"]
4347

@@ -302,8 +306,7 @@ def _read_until_with_poll(self, match, timeout):
302306
n = len(match)
303307
call_timeout = timeout
304308
if timeout is not None:
305-
from time import time
306-
time_start = time()
309+
time_start = _time()
307310
self.process_rawq()
308311
i = self.cookedq.find(match)
309312
if i < 0:
@@ -316,7 +319,7 @@ def _read_until_with_poll(self, match, timeout):
316319
except select.error as e:
317320
if e.errno == errno.EINTR:
318321
if timeout is not None:
319-
elapsed = time() - time_start
322+
elapsed = _time() - time_start
320323
call_timeout = timeout-elapsed
321324
continue
322325
raise
@@ -327,7 +330,7 @@ def _read_until_with_poll(self, match, timeout):
327330
self.process_rawq()
328331
i = self.cookedq.find(match, i)
329332
if timeout is not None:
330-
elapsed = time() - time_start
333+
elapsed = _time() - time_start
331334
if elapsed >= timeout:
332335
break
333336
call_timeout = timeout-elapsed
@@ -356,8 +359,7 @@ def _read_until_with_select(self, match, timeout=None):
356359
s_args = s_reply
357360
if timeout is not None:
358361
s_args = s_args + (timeout,)
359-
from time import time
360-
time_start = time()
362+
time_start = _time()
361363
while not self.eof and select.select(*s_args) == s_reply:
362364
i = max(0, len(self.cookedq)-n)
363365
self.fill_rawq()
@@ -369,7 +371,7 @@ def _read_until_with_select(self, match, timeout=None):
369371
self.cookedq = self.cookedq[i:]
370372
return buf
371373
if timeout is not None:
372-
elapsed = time() - time_start
374+
elapsed = _time() - time_start
373375
if elapsed >= timeout:
374376
break
375377
s_args = s_reply + (timeout-elapsed,)
@@ -665,8 +667,7 @@ def _expect_with_poll(self, expect_list, timeout=None):
665667
expect_list[i] = re.compile(expect_list[i])
666668
call_timeout = timeout
667669
if timeout is not None:
668-
from time import time
669-
time_start = time()
670+
time_start = _time()
670671
self.process_rawq()
671672
m = None
672673
for i in indices:
@@ -686,7 +687,7 @@ def _expect_with_poll(self, expect_list, timeout=None):
686687
except select.error as e:
687688
if e.errno == errno.EINTR:
688689
if timeout is not None:
689-
elapsed = time() - time_start
690+
elapsed = _time() - time_start
690691
call_timeout = timeout-elapsed
691692
continue
692693
raise
@@ -702,7 +703,7 @@ def _expect_with_poll(self, expect_list, timeout=None):
702703
self.cookedq = self.cookedq[e:]
703704
break
704705
if timeout is not None:
705-
elapsed = time() - time_start
706+
elapsed = _time() - time_start
706707
if elapsed >= timeout:
707708
break
708709
call_timeout = timeout-elapsed
@@ -727,8 +728,7 @@ def _expect_with_select(self, list, timeout=None):
727728
if not re: import re
728729
list[i] = re.compile(list[i])
729730
if timeout is not None:
730-
from time import time
731-
time_start = time()
731+
time_start = _time()
732732
while 1:
733733
self.process_rawq()
734734
for i in indices:
@@ -741,7 +741,7 @@ def _expect_with_select(self, list, timeout=None):
741741
if self.eof:
742742
break
743743
if timeout is not None:
744-
elapsed = time() - time_start
744+
elapsed = _time() - time_start
745745
if elapsed >= timeout:
746746
break
747747
s_args = ([self.fileno()], [], [], timeout-elapsed)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ Core and Builtins
8181
Library
8282
-------
8383

84+
- Issue #19339: telnetlib module is now using time.monotonic() when available
85+
to compute timeout.
86+
8487
- Issue #19288: Fixed the "in" operator of dbm.gnu databases for string
8588
argument. Original patch by Arfrever Frehtes Taifersar Arahesis.
8689

0 commit comments

Comments
 (0)