py3: Fix exception types

Except keywords expects a tuple containing only base classes,
or a base class as the exception type in Python3.
This is compatible with Python2.

Convert lists to tuples, and expand nested tuples.
Suppress pylint warnings.

This was taken from d41f3fe19939f1759ca3cf73874724bd75eebe43
in the starlingx/config repo.

Story: 2006796
Task: 42866

Signed-off-by: Charles Short <charles.short@windriver.com>
Change-Id: I129302b5a6199b705b3533ffb77e7700e815df2f
This commit is contained in:
Charles Short 2021-07-20 13:26:15 -04:00
parent 8655ae2aa1
commit 702ea92596
1 changed files with 17 additions and 4 deletions

View File

@ -488,7 +488,7 @@ class Connection(object):
"%(hostname)s:%(port)d") % params)
try:
self.connection.release()
except self.connection_errors:
except tuple(self.connection_errors): # pylint: disable=catching-non-exception
pass
# Setting this in case the next statement fails, though
# it shouldn't be doing any network operations, yet.
@ -523,11 +523,14 @@ class Connection(object):
while True:
params = self.params_list[attempt % len(self.params_list)]
attempt += 1
e = None
try:
self._connect(params)
return
except (IOError, self.connection_errors) as e:
pass
except IOError as ex:
e = ex
except tuple(self.connection_errors) as ex: # pylint: disable=catching-non-exception
e = ex
except Exception as e:
# NOTE(comstud): Unfortunately it's possible for amqplib
# to return an error not covered by its transport
@ -535,6 +538,7 @@ class Connection(object):
# a protocol response. (See paste link in LP888621)
# So, we check all exceptions for 'timeout' in them
# and try to reconnect in this case.
e = ex
if 'timeout' not in str(e):
raise
@ -569,7 +573,16 @@ class Connection(object):
while True:
try:
return method(*args, **kwargs)
except (self.channel_errors, self.connection_errors, socket.timeout, IOError) as e:
except tuple(self.channel_errors) as e: # pylint: disable=catching-non-exception
if error_callback:
error_callback(e)
except tuple(self.connection_errors) as e: # pylint: disable=catching-non-exception
if error_callback:
error_callback(e)
except socket.timeout as e:
if error_callback:
error_callback(e)
except IOError as e:
if error_callback:
error_callback(e)
except Exception as e: