From ba9f06e0c2381f6506a92e9c0553b2f475975a18 Mon Sep 17 00:00:00 2001 From: Ken Dreyer Date: Tue, 9 May 2023 16:40:14 -0400 Subject: [PATCH] support urllib3 newer DEFAULT_TIMEOUT urllib3 1.x used to accept socket._GLOBAL_DEFAULT_TIMEOUT as a sentinal object to mean "no configured timeout". In urllib3 2.x, urllib3 uses its own _DEFAULT_TIMEOUT sentinal object, and it rejects socket._GLOBAL_DEFAULT_TIMEOUT. Assign our own DEFAULT_TIMEOUT constant to the newer object if it exists, and fall back to the old behavior on old urllib3 versions. Co-authored-by: Vsevolod Fedorov Closes-Bug: #2018567 Change-Id: Ic626ba0e8ed79eec3a63ffab6cc02f91aa545ab1 --- jenkins/__init__.py | 6 ++++-- tests/test_jenkins.py | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/jenkins/__init__.py b/jenkins/__init__.py index 7ebdbea..3f9c209 100755 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -58,6 +58,7 @@ import warnings import multi_key_dict import requests import requests.exceptions as req_exc +import urllib3.util.timeout from requests.packages.urllib3.exceptions import InsecureRequestWarning from six.moves.http_client import BadStatusLine from six.moves.urllib.error import URLError @@ -91,6 +92,7 @@ LAUNCHER_COMMAND = 'hudson.slaves.CommandLauncher' LAUNCHER_JNLP = 'hudson.slaves.JNLPLauncher' LAUNCHER_WINDOWS_SERVICE = 'hudson.os.windows.ManagedWindowsServiceLauncher' DEFAULT_HEADERS = {'Content-Type': 'text/xml; charset=utf-8'} +DEFAULT_TIMEOUT = getattr(urllib3.util.timeout, '_DEFAULT_TIMEOUT', socket._GLOBAL_DEFAULT_TIMEOUT) # REST Endpoints INFO = 'api/json' @@ -300,7 +302,7 @@ class Jenkins(object): _timeout_warning_issued = False def __init__(self, url, username=None, password=None, - timeout=socket._GLOBAL_DEFAULT_TIMEOUT): + timeout=DEFAULT_TIMEOUT): '''Create handle to Jenkins instance. All methods will raise :class:`JenkinsException` on failure. @@ -2247,7 +2249,7 @@ class Jenkins(object): raise ValueError("Timeout must be >= 0 not %d" % timeout) if (not self._timeout_warning_issued and - self.timeout != socket._GLOBAL_DEFAULT_TIMEOUT and + self.timeout != DEFAULT_TIMEOUT and timeout < self.timeout): warnings.warn("Requested timeout to wait for jenkins to resume " "normal operations is less than configured " diff --git a/tests/test_jenkins.py b/tests/test_jenkins.py index 2403ccc..5d7195b 100644 --- a/tests/test_jenkins.py +++ b/tests/test_jenkins.py @@ -1,5 +1,4 @@ import json -import socket from mock import patch import six @@ -68,7 +67,7 @@ class JenkinsConstructorTest(JenkinsTestBase): def test_default_timeout(self): j = jenkins.Jenkins('{0}'.format(self.base_url)) - self.assertEqual(j.timeout, socket._GLOBAL_DEFAULT_TIMEOUT) + self.assertEqual(j.timeout, jenkins.DEFAULT_TIMEOUT) def test_custom_timeout(self): j = jenkins.Jenkins('{0}'.format(self.base_url), timeout=300)