Fix timeout kwargs in _syscall_wrapper
@@ -11,20 +11,14 @@ import select
import socket
import sys
import time
-from collections import namedtuple
+from collections import namedtuple, Mapping
from ..packages.six import integer_types
-try:
- from collections.abc import Mapping
-except ImportError:
- from collections import Mapping
-
try:
monotonic = time.monotonic
except (AttributeError, ImportError): # Python 3.3<
monotonic = time.time
-
EVENT_READ = (1 << 0)
EVENT_WRITE = (1 << 1)
@@ -79,7 +73,15 @@ else:
""" Wrapper function for syscalls that could fail due to EINTR.
All functions should be retried if there is time left in the timeout
in accordance with PEP 475. """
+
+ if recalc_timeout and "timeout" not in kwargs:
+ raise ValueError(
+ "Timeout must be in kwargs to be recalculated")
+
+ # timeout for recalcultion must be a keyword argument
timeout = kwargs.get("timeout", None)
+
+ # Based on the timeout, determine call expiry time
if timeout is None:
expires = None
recalc_timeout = False
@@ -90,11 +92,6 @@ else:
else:
expires = monotonic() + timeout
- args = list(args)
- if recalc_timeout and "timeout" not in kwargs:
- raise ValueError(
- "Timeout must be in args or kwargs to be recalculated")
-
result = _SYSCALL_SENTINEL
while result is _SYSCALL_SENTINEL:
try:
@@ -106,7 +103,7 @@ else:
except (OSError, IOError, select.error) as e:
# select.error wasn't a subclass of OSError in the past.
errcode = None
- if hasattr(e, "errno"):
+ if hasattr(e, "errno") and e.errno is not None:
errcode = e.errno
elif hasattr(e, "args"):
errcode = e.args[0]
@@ -119,9 +116,8 @@ else:
if expires is not None:
current_time = monotonic()
if current_time > expires:
- raise OSError(errno=errno.ETIMEDOUT)
+ raise OSError(errno.ETIMEDOUT, "Connection timed out")
if recalc_timeout:
- if "timeout" in kwargs:
kwargs["timeout"] = expires - current_time
continue
if errcode:
@@ -324,7 +320,7 @@ if hasattr(select, "select"):
timeout = None if timeout is None else max(timeout, 0.0)
ready = []
r, w, _ = _syscall_wrapper(self._select, True, self._readers,
- self._writers, timeout)
+ self._writers, timeout=timeout)
r = set(r)
w = set(w)
for fd in r | w:
@@ -517,7 +513,7 @@ if hasattr(select, "kqueue"):
ready_fds = {}
kevent_list = _syscall_wrapper(self._kqueue.control, True,
- None, max_events, timeout)
+ None, max_events, timeout=timeout)
for kevent in kevent_list:
fd = kevent.ident
@@ -562,7 +558,7 @@ def _can_allocate(struct):
else:
getattr(select, struct)().close()
return True
- except (OSError, AttributeError) as e:
+ except (OSError, AttributeError):
return False
