Commit 31fe8a90 authored by Victor Stinner's avatar Victor Stinner

Close #19339: telnetlib module is now using time.monotonic() when available to

compute timeout.
parent b9d65ed8
...@@ -38,6 +38,10 @@ import errno ...@@ -38,6 +38,10 @@ import errno
import sys import sys
import socket import socket
import select import select
try:
from time import monotonic as _time
except ImportError:
from time import time as _time
__all__ = ["Telnet"] __all__ = ["Telnet"]
...@@ -302,8 +306,7 @@ class Telnet: ...@@ -302,8 +306,7 @@ class Telnet:
n = len(match) n = len(match)
call_timeout = timeout call_timeout = timeout
if timeout is not None: if timeout is not None:
from time import time time_start = _time()
time_start = time()
self.process_rawq() self.process_rawq()
i = self.cookedq.find(match) i = self.cookedq.find(match)
if i < 0: if i < 0:
...@@ -316,7 +319,7 @@ class Telnet: ...@@ -316,7 +319,7 @@ class Telnet:
except select.error as e: except select.error as e:
if e.errno == errno.EINTR: if e.errno == errno.EINTR:
if timeout is not None: if timeout is not None:
elapsed = time() - time_start elapsed = _time() - time_start
call_timeout = timeout-elapsed call_timeout = timeout-elapsed
continue continue
raise raise
...@@ -327,7 +330,7 @@ class Telnet: ...@@ -327,7 +330,7 @@ class Telnet:
self.process_rawq() self.process_rawq()
i = self.cookedq.find(match, i) i = self.cookedq.find(match, i)
if timeout is not None: if timeout is not None:
elapsed = time() - time_start elapsed = _time() - time_start
if elapsed >= timeout: if elapsed >= timeout:
break break
call_timeout = timeout-elapsed call_timeout = timeout-elapsed
...@@ -356,8 +359,7 @@ class Telnet: ...@@ -356,8 +359,7 @@ class Telnet:
s_args = s_reply s_args = s_reply
if timeout is not None: if timeout is not None:
s_args = s_args + (timeout,) s_args = s_args + (timeout,)
from time import time time_start = _time()
time_start = time()
while not self.eof and select.select(*s_args) == s_reply: while not self.eof and select.select(*s_args) == s_reply:
i = max(0, len(self.cookedq)-n) i = max(0, len(self.cookedq)-n)
self.fill_rawq() self.fill_rawq()
...@@ -369,7 +371,7 @@ class Telnet: ...@@ -369,7 +371,7 @@ class Telnet:
self.cookedq = self.cookedq[i:] self.cookedq = self.cookedq[i:]
return buf return buf
if timeout is not None: if timeout is not None:
elapsed = time() - time_start elapsed = _time() - time_start
if elapsed >= timeout: if elapsed >= timeout:
break break
s_args = s_reply + (timeout-elapsed,) s_args = s_reply + (timeout-elapsed,)
...@@ -665,8 +667,7 @@ class Telnet: ...@@ -665,8 +667,7 @@ class Telnet:
expect_list[i] = re.compile(expect_list[i]) expect_list[i] = re.compile(expect_list[i])
call_timeout = timeout call_timeout = timeout
if timeout is not None: if timeout is not None:
from time import time time_start = _time()
time_start = time()
self.process_rawq() self.process_rawq()
m = None m = None
for i in indices: for i in indices:
...@@ -686,7 +687,7 @@ class Telnet: ...@@ -686,7 +687,7 @@ class Telnet:
except select.error as e: except select.error as e:
if e.errno == errno.EINTR: if e.errno == errno.EINTR:
if timeout is not None: if timeout is not None:
elapsed = time() - time_start elapsed = _time() - time_start
call_timeout = timeout-elapsed call_timeout = timeout-elapsed
continue continue
raise raise
...@@ -702,7 +703,7 @@ class Telnet: ...@@ -702,7 +703,7 @@ class Telnet:
self.cookedq = self.cookedq[e:] self.cookedq = self.cookedq[e:]
break break
if timeout is not None: if timeout is not None:
elapsed = time() - time_start elapsed = _time() - time_start
if elapsed >= timeout: if elapsed >= timeout:
break break
call_timeout = timeout-elapsed call_timeout = timeout-elapsed
...@@ -727,8 +728,7 @@ class Telnet: ...@@ -727,8 +728,7 @@ class Telnet:
if not re: import re if not re: import re
list[i] = re.compile(list[i]) list[i] = re.compile(list[i])
if timeout is not None: if timeout is not None:
from time import time time_start = _time()
time_start = time()
while 1: while 1:
self.process_rawq() self.process_rawq()
for i in indices: for i in indices:
...@@ -741,7 +741,7 @@ class Telnet: ...@@ -741,7 +741,7 @@ class Telnet:
if self.eof: if self.eof:
break break
if timeout is not None: if timeout is not None:
elapsed = time() - time_start elapsed = _time() - time_start
if elapsed >= timeout: if elapsed >= timeout:
break break
s_args = ([self.fileno()], [], [], timeout-elapsed) s_args = ([self.fileno()], [], [], timeout-elapsed)
......
...@@ -81,6 +81,9 @@ Core and Builtins ...@@ -81,6 +81,9 @@ Core and Builtins
Library Library
------- -------
- Issue #19339: telnetlib module is now using time.monotonic() when available
to compute timeout.
- Issue #19288: Fixed the "in" operator of dbm.gnu databases for string - Issue #19288: Fixed the "in" operator of dbm.gnu databases for string
argument. Original patch by Arfrever Frehtes Taifersar Arahesis. argument. Original patch by Arfrever Frehtes Taifersar Arahesis.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment