diff options
author | lars4839 <sethmichaellarson@protonmail.com> | 2018-03-22 20:53:29 -0500 |
---|---|---|
committer | lars4839 <sethmichaellarson@protonmail.com> | 2018-03-22 20:53:29 -0500 |
commit | 050ea4d5423a2c8b22e03fe34be6ba22e4cfd3fa (patch) | |
tree | 5064d8f14da60d8d78aa00cfa0deabcc1f037e22 | |
parent | d5c8c68a8df0cb864d44a5ad00cb8f2ffc60bab8 (diff) | |
download | urllib3-050ea4d5423a2c8b22e03fe34be6ba22e4cfd3fa.tar.gz |
Fix timeout kwargs in _syscall_wrapper
-rw-r--r-- | urllib3/util/selectors.py | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/urllib3/util/selectors.py b/urllib3/util/selectors.py index 5cb772f6..593737e7 100644 --- a/urllib3/util/selectors.py +++ b/urllib3/util/selectors.py @@ -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,10 +116,9 @@ 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 + kwargs["timeout"] = expires - current_time continue if errcode: raise SelectorError(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 |