From ba9f06e0c2381f6506a92e9c0553b2f475975a18 Mon Sep 17 00:00:00 2001
From: Ken Dreyer <kdreyer@redhat.com>
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 <vsevolod.fedorov@gmail.com>
Closes-Bug: #2018567
Change-Id: Ic626ba0e8ed79eec3a63ffab6cc02f91aa545ab1

Bug: https://bugs.launchpad.net/python-jenkins/+bug/2018567
Bug-Debian: https://bugs.debian.org/1077530

--- a/jenkins/__init__.py
+++ b/jenkins/__init__.py
@@ -58,6 +58,7 @@
 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_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'
@@ -297,7 +299,7 @@
     _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.
@@ -2146,7 +2148,7 @@
             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 "
--- 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 @@
 
     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)
