Remove urllib3.util.current_time, use time.monotonic instead
MODIFY
src/urllib3/util/__init__.py
src/urllib3/util/__init__.py
@@ -15,7 +15,7 @@ from .ssl_ import (
resolve_ssl_version,
ssl_wrap_socket,
)
-from .timeout import Timeout, current_time
+from .timeout import Timeout
from .url import Url, parse_url
from .wait import wait_for_read, wait_for_write
@@ -30,7 +30,6 @@ __all__ = (
"Timeout",
"Url",
"assert_fingerprint",
- "current_time",
"is_connection_dropped",
"is_fp_closed",
"parse_url",

MODIFY
src/urllib3/util/timeout.py
src/urllib3/util/timeout.py
@@ -11,10 +11,6 @@ from ..exceptions import TimeoutStateError
_Default = object()
-# Use time.monotonic if available.
-current_time = getattr(time, "monotonic", time.time)
-
-
class Timeout:
"""Timeout configuration.
@@ -193,7 +189,7 @@ class Timeout:
"""
if self._start_connect is not None:
raise TimeoutStateError("Timeout timer has already been started.")
- self._start_connect = current_time()
+ self._start_connect = time.monotonic()
return self._start_connect
def get_connect_duration(self):
@@ -208,7 +204,7 @@ class Timeout:
raise TimeoutStateError(
"Can't get connect duration for timer that has not started."
)
- return current_time() - self._start_connect
+ return time.monotonic() - self._start_connect
@property
def connect_timeout(self):

MODIFY
test/test_util.py
test/test_util.py
@@ -552,13 +552,13 @@ class TestUtil:
with pytest.raises(ValueError, match=message):
Timeout(**kwargs)
- @patch("urllib3.util.timeout.current_time")
- def test_timeout(self, current_time):
+ @patch("time.monotonic")
+ def test_timeout(self, time_monotonic):
timeout = Timeout(total=3)
# make 'no time' elapse
timeout = self._make_time_pass(
- seconds=0, timeout=timeout, time_mock=current_time
+ seconds=0, timeout=timeout, time_mock=time_monotonic
)
assert timeout.read_timeout == 3
assert timeout.connect_timeout == 3
@@ -572,14 +572,14 @@ class TestUtil:
# Connect takes 5 seconds, leaving 5 seconds for read
timeout = Timeout(total=10, read=7)
timeout = self._make_time_pass(
- seconds=5, timeout=timeout, time_mock=current_time
+ seconds=5, timeout=timeout, time_mock=time_monotonic
)
assert timeout.read_timeout == 5
# Connect takes 2 seconds, read timeout still 7 seconds
timeout = Timeout(total=10, read=7)
timeout = self._make_time_pass(
- seconds=2, timeout=timeout, time_mock=current_time
+ seconds=2, timeout=timeout, time_mock=time_monotonic
)
assert timeout.read_timeout == 7
@@ -600,9 +600,9 @@ class TestUtil:
timeout = Timeout(connect=1, read=None, total=3)
assert str(timeout) == "Timeout(connect=1, read=None, total=3)"
- @patch("urllib3.util.timeout.current_time")
- def test_timeout_elapsed(self, current_time):
- current_time.return_value = TIMEOUT_EPOCH
+ @patch("time.monotonic")
+ def test_timeout_elapsed(self, time_monotonic):
+ time_monotonic.return_value = TIMEOUT_EPOCH
timeout = Timeout(total=3)
with pytest.raises(TimeoutStateError):
timeout.get_connect_duration()
@@ -611,9 +611,9 @@ class TestUtil:
with pytest.raises(TimeoutStateError):
timeout.start_connect()
- current_time.return_value = TIMEOUT_EPOCH + 2
+ time_monotonic.return_value = TIMEOUT_EPOCH + 2
assert timeout.get_connect_duration() == 2
- current_time.return_value = TIMEOUT_EPOCH + 37
+ time_monotonic.return_value = TIMEOUT_EPOCH + 37
assert timeout.get_connect_duration() == 37
def test_is_fp_closed_object_supports_closed(self):

Remove urllib3.util.current_time, use time.monotonic instead