Skip to content

Commit

Permalink
Merge pull request #210 from release-engineering/remove-py2-condition…
Browse files Browse the repository at this point in the history
…al-code

Remove code conditionally executed when running with python2
  • Loading branch information
rohanpm authored Aug 8, 2023
2 parents 15a8935 + 5ef3ce8 commit 69804b3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 67 deletions.
6 changes: 1 addition & 5 deletions kobo/django/xmlrpc/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@

class DjangoXMLRPCDispatcher(SimpleXMLRPCDispatcher):
def __init__(self, allow_none=True, encoding=None):
if sys.version_info[:2] == (2, 4):
# doesn't support extra args in python 2.4
SimpleXMLRPCDispatcher.__init__(self)
else:
SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)

self.allow_none = allow_none
self.encoding = encoding
Expand Down
7 changes: 1 addition & 6 deletions kobo/hub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,7 @@ def _open_log(self, name):
return io.open(log_path, 'rb', LOG_BUFFER_SIZE)
elif os.path.isfile(log_path + ".gz"):
out = gzip.open(log_path + ".gz", "rb")

# GZipFile was not usable with BufferedReader
# until 2.7
if sys.version_info[0:2] >= (2, 7):
out = io.BufferedReader(out, LOG_BUFFER_SIZE)

out = io.BufferedReader(out, LOG_BUFFER_SIZE)
return out
else:
raise Exception('Cannot find log %s' % name)
Expand Down
71 changes: 20 additions & 51 deletions kobo/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,33 +292,21 @@ def make_connection(self, host):
host_ = "%s:%s" % (host, TimeoutHTTPProxyConnection.default_port)

if self.proxy_config["proxy"] and host not in self.no_proxy and host_ not in self.no_proxy:
if sys.version_info[:2] < (2, 7):
host, extra_headers, x509 = self.get_host_info(host)
conn = TimeoutProxyHTTP(host, **self.proxy_config)
conn.set_timeout(self.timeout)
return conn
else:
CONNECTION_LOCK.acquire()
host, extra_headers, x509 = self.get_host_info(host)
conn = TimeoutProxyHTTPS(host, **self.proxy_config)
conn.set_timeout(self.timeout)
CONNECTION_LOCK.release()
return conn

if sys.version_info[:2] < (2, 7):
CONNECTION_LOCK.acquire()
host, extra_headers, x509 = self.get_host_info(host)
conn = TimeoutHTTP(host)
conn = TimeoutProxyHTTPS(host, **self.proxy_config)
conn.set_timeout(self.timeout)
return conn
else:
CONNECTION_LOCK.acquire()
self._connection = (None, None) # this disables connection caching which causes a race condition when running in threads
conn = xmlrpclib.Transport.make_connection(self, host)
CONNECTION_LOCK.release()
if self.timeout:
conn.timeout = self.timeout
return conn

CONNECTION_LOCK.acquire()
self._connection = (None, None) # this disables connection caching which causes a race condition when running in threads
conn = xmlrpclib.Transport.make_connection(self, host)
CONNECTION_LOCK.release()
if self.timeout:
conn.timeout = self.timeout
return conn

def send_cookies(self, connection, cookie_request):
"""Add cookies to the header."""
self.cookiejar.add_cookie_header(cookie_request)
Expand Down Expand Up @@ -535,14 +523,7 @@ def _single_request3(self, host, handler, request_body, verbose=False):
raise xmlrpclib.ProtocolError(host + handler, response.status, response.reason, response.msg)

# override the appropriate request method
if sys.version_info[0] >= 3:
single_request = _single_request3
elif hasattr(xmlrpclib.Transport, "single_request"):
# python 2.7+
single_request = _single_request
else:
# python 2.6-
request = _request

def send_headers(self, connection, headers):
headers.extend(self._cookie_headers)
Expand Down Expand Up @@ -578,33 +559,21 @@ def make_connection(self, host):
host_ = "%s:%s" % (host, TimeoutHTTPSProxyConnection.default_port)

if self.proxy_config["proxy"] and host not in self.no_proxy and host_ not in self.no_proxy:
if sys.version_info[:2] < (2, 7):
host, extra_headers, x509 = self.get_host_info(host)
conn = TimeoutProxyHTTPS(host, **self.proxy_config)
conn.set_timeout(self.timeout)
return conn
else:
CONNECTION_LOCK.acquire()
host, extra_headers, x509 = self.get_host_info(host)
conn = TimeoutProxyHTTPS(host, **self.proxy_config)
conn.set_timeout(self.timeout)
CONNECTION_LOCK.release()
return conn

if sys.version_info[:2] < (2, 7):
CONNECTION_LOCK.acquire()
host, extra_headers, x509 = self.get_host_info(host)
conn = TimeoutHTTPS(host, None, **(x509 or {}))
conn = TimeoutProxyHTTPS(host, **self.proxy_config)
conn.set_timeout(self.timeout)
return conn
else:
CONNECTION_LOCK.acquire()
self._connection = (None, None) # this disables connection caching which causes a race condition when running in threads
conn = xmlrpclib.SafeTransport.make_connection(self, host)
if self.timeout:
conn.timeout = self.timeout
CONNECTION_LOCK.release()
return conn

CONNECTION_LOCK.acquire()
self._connection = (None, None) # this disables connection caching which causes a race condition when running in threads
conn = xmlrpclib.SafeTransport.make_connection(self, host)
if self.timeout:
conn.timeout = self.timeout
CONNECTION_LOCK.release()
return conn

def __init__(self, *args, **kwargs):
self.context = kwargs.pop('context', None)
xmlrpclib.SafeTransport.__init__(self, *args, **kwargs)
Expand Down
6 changes: 1 addition & 5 deletions tests/test_hubproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ class FakeTransport(SafeCookieTransport):
Subclasses the real SafeCookieTransport so we get a real CookieJar.
"""
def __init__(self, *args, **kwargs):
# note: py2 transport classes do not subclass object
if sys.version_info[0] < 3:
SafeCookieTransport.__init__(self, *args, **kwargs)
else:
super().__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

self.fake_transport_calls = []

Expand Down

0 comments on commit 69804b3

Please sign in to comment.