From 702ea92596cabecfed85fc3a79ac0a8c3d275fdc Mon Sep 17 00:00:00 2001 From: Charles Short Date: Tue, 20 Jul 2021 13:26:15 -0400 Subject: [PATCH] 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 Change-Id: I129302b5a6199b705b3533ffb77e7700e815df2f --- .../sm_api/openstack/common/rpc/impl_kombu.py | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/impl_kombu.py b/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/impl_kombu.py index 8ea68aeb..67115502 100644 --- a/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/impl_kombu.py +++ b/service-mgmt-api/sm-api/sm_api/openstack/common/rpc/impl_kombu.py @@ -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: