From 2c074b9486ed8c2e1fd17b958bf4d2a26517509b Mon Sep 17 00:00:00 2001 From: Takashi Natsume Date: Mon, 11 May 2020 15:56:19 +0000 Subject: [PATCH] Remove six.reraise Replace six.reraise with Python 3 style code. Subsequent patches will replace other six usages. Change-Id: Ib129cb399d1521ad6d18fcf0b8ac9fd793888c81 Implements: blueprint six-removal Signed-off-by: Takashi Natsume --- nova/api/metadata/vendordata_dynamic.py | 3 +- nova/compute/manager.py | 21 ++++++------ nova/image/glance.py | 4 +-- nova/network/security_group_api.py | 32 ++++++------------- nova/test.py | 2 +- .../test_instance.py | 4 +-- nova/utils.py | 2 +- nova/virt/hyperv/driver.py | 11 +++---- nova/volume/cinder.py | 2 +- 9 files changed, 34 insertions(+), 47 deletions(-) diff --git a/nova/api/metadata/vendordata_dynamic.py b/nova/api/metadata/vendordata_dynamic.py index 2db9a478e6a6..cc18cbb4601d 100644 --- a/nova/api/metadata/vendordata_dynamic.py +++ b/nova/api/metadata/vendordata_dynamic.py @@ -21,7 +21,6 @@ from keystoneauth1 import exceptions as ks_exceptions from keystoneauth1 import loading as ks_loading from oslo_log import log as logging from oslo_serialization import jsonutils -import six from nova.api.metadata import vendordata import nova.conf @@ -115,7 +114,7 @@ class DynamicVendorData(vendordata.VendorDataDriver): 'error': e}, instance=self.instance) if CONF.api.vendordata_dynamic_failure_fatal: - six.reraise(type(e), e, sys.exc_info()[2]) + raise e.with_traceback(sys.exc_info()[2]) return {} diff --git a/nova/compute/manager.py b/nova/compute/manager.py index a4fc43e4c095..a1bcc9c144fa 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -1740,15 +1740,14 @@ class ComputeManager(manager.Manager): # for this async greenthread to finish before calling # instance.save(). return nwinfo - except Exception: - exc_info = sys.exc_info() + except Exception as e: log_info = {'attempt': attempt, 'attempts': attempts} if attempt == attempts: LOG.exception('Instance failed network setup ' 'after %(attempts)d attempt(s)', log_info) - six.reraise(*exc_info) + raise e LOG.warning('Instance failed network setup ' '(attempt %(attempt)d of %(attempts)d)', log_info, instance=instance) @@ -2911,7 +2910,7 @@ class ComputeManager(manager.Manager): def _cleanup_volumes(self, context, instance, bdms, raise_exc=True, detach=True): - exc_info = None + original_exception = None for bdm in bdms: if detach and bdm.volume_id: try: @@ -2921,7 +2920,7 @@ class ComputeManager(manager.Manager): self._detach_volume(context, bdm, instance, destroy_bdm=destroy) except Exception as exc: - exc_info = sys.exc_info() + original_exception = exc LOG.warning('Failed to detach volume: %(volume_id)s ' 'due to %(exc)s', {'volume_id': bdm.volume_id, 'exc': exc}) @@ -2932,12 +2931,12 @@ class ComputeManager(manager.Manager): instance_uuid=instance.uuid) self.volume_api.delete(context, bdm.volume_id) except Exception as exc: - exc_info = sys.exc_info() + original_exception = exc LOG.warning('Failed to delete volume: %(volume_id)s ' 'due to %(exc)s', {'volume_id': bdm.volume_id, 'exc': exc}) - if exc_info is not None and raise_exc: - six.reraise(exc_info[0], exc_info[1], exc_info[2]) + if original_exception is not None and raise_exc: + raise original_exception def _delete_instance(self, context, instance, bdms): """Delete an instance on this host. @@ -5299,7 +5298,11 @@ class ComputeManager(manager.Manager): ) else: # not re-scheduling - six.reraise(*exc_info) + if exc_info[1] is None: + exc_info[1] = exc_info[0]() + if exc_info[1].__traceback__ is not exc_info[2]: + raise exc_info[1].with_traceback(exc_info[2]) + raise exc_info[1] # TODO(stephenfin): Remove unused request_spec parameter in API v6.0 @messaging.expected_exceptions(exception.MigrationPreCheckError) diff --git a/nova/image/glance.py b/nova/image/glance.py index 631e3bc71d8f..be0c1eeccdc9 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -969,14 +969,14 @@ def _reraise_translated_image_exception(image_id): """Transform the exception for the image but keep its traceback intact.""" exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_image_exception(image_id, exc_value) - six.reraise(type(new_exc), new_exc, exc_trace) + raise new_exc.with_traceback(exc_trace) def _reraise_translated_exception(): """Transform the exception but keep its traceback intact.""" exc_type, exc_value, exc_trace = sys.exc_info() new_exc = _translate_plain_exception(exc_value) - six.reraise(type(new_exc), new_exc, exc_trace) + raise new_exc.with_traceback(exc_trace) def _translate_image_exception(image_id, exc_value): diff --git a/nova/network/security_group_api.py b/nova/network/security_group_api.py index e07dba162eb0..3b09993b58ee 100644 --- a/nova/network/security_group_api.py +++ b/nova/network/security_group_api.py @@ -17,8 +17,6 @@ # License for the specific language governing permissions and limitations # under the License. -import sys - import netaddr from neutronclient.common import exceptions as n_exc from neutronclient.neutron import v2_0 as neutronv20 @@ -72,13 +70,12 @@ def validate_name( except n_exc.NeutronClientNoUniqueMatch as e: raise exception.NoUniqueMatch(six.text_type(e)) except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 404: LOG.debug('Neutron security group %s not found', name) raise exception.SecurityGroupNotFound(six.text_type(e)) else: LOG.error('Neutron Error: %s', e) - six.reraise(*exc_info) + raise e def parse_cidr(cidr): @@ -241,7 +238,6 @@ def create_security_group(context, name, description): except n_exc.BadRequest as e: raise exception.Invalid(six.text_type(e)) except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() LOG.exception("Neutron Error creating security group %s", name) if e.status_code == 401: # TODO(arosen) Cannot raise generic response from neutron here @@ -250,7 +246,7 @@ def create_security_group(context, name, description): raise exc.HTTPBadRequest() elif e.status_code == 409: raise exception.SecurityGroupLimitExceeded(six.text_type(e)) - six.reraise(*exc_info) + raise e return _convert_to_nova_security_group_format(security_group) @@ -261,14 +257,13 @@ def update_security_group(context, security_group, name, description): security_group = neutron.update_security_group( security_group['id'], body).get('security_group') except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() LOG.exception("Neutron Error updating security group %s", name) if e.status_code == 401: # TODO(arosen) Cannot raise generic response from neutron here # as this error code could be related to bad input or over # quota raise exc.HTTPBadRequest() - six.reraise(*exc_info) + raise e return _convert_to_nova_security_group_format(security_group) @@ -314,13 +309,12 @@ def get(context, id): group = neutron.show_security_group(id).get('security_group') return _convert_to_nova_security_group_format(group) except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 404: LOG.debug('Neutron security group %s not found', id) raise exception.SecurityGroupNotFound(six.text_type(e)) else: LOG.error("Neutron Error: %s", e) - six.reraise(*exc_info) + raise e def list(context, project, search_opts=None): @@ -364,14 +358,13 @@ def destroy(context, security_group): try: neutron.delete_security_group(security_group['id']) except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 404: raise exception.SecurityGroupNotFound(six.text_type(e)) elif e.status_code == 409: raise exception.Invalid(six.text_type(e)) else: LOG.error("Neutron Error: %s", e) - six.reraise(*exc_info) + raise e def add_rules(context, id, name, vals): @@ -389,7 +382,6 @@ def add_rules(context, id, name, vals): rules = neutron.create_security_group_rule( body).get('security_group_rules') except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 404: LOG.exception("Neutron Error getting security group %s", name) raise exception.SecurityGroupNotFound(six.text_type(e)) @@ -401,7 +393,7 @@ def add_rules(context, id, name, vals): LOG.exception("Neutron Error: %s", e) raise exception.Invalid(six.text_type(e)) else: - six.reraise(*exc_info) + raise e converted_rules = [] for rule in rules: converted_rules.append( @@ -467,13 +459,12 @@ def get_rule(context, id): rule = neutron.show_security_group_rule( id).get('security_group_rule') except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 404: LOG.debug("Neutron security group rule %s not found", id) raise exception.SecurityGroupNotFound(six.text_type(e)) else: LOG.error("Neutron Error: %s", e) - six.reraise(*exc_info) + raise e return _convert_to_nova_security_group_rule_format(rule) @@ -616,7 +607,6 @@ def add_to_instance(context, instance, security_group_name): except n_exc.NeutronClientNoUniqueMatch as e: raise exception.NoUniqueMatch(six.text_type(e)) except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 404: msg = (_("Security group %(name)s is not found for " "project %(project)s") % @@ -624,7 +614,7 @@ def add_to_instance(context, instance, security_group_name): 'project': context.project_id}) raise exception.SecurityGroupNotFound(msg) else: - six.reraise(*exc_info) + raise e params = {'device_id': instance.uuid} try: ports = neutron.list_ports(**params).get('ports') @@ -657,12 +647,11 @@ def add_to_instance(context, instance, security_group_name): 'port_id': port['id']}) neutron.update_port(port['id'], {'port': updated_port}) except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 400: raise exception.SecurityGroupCannotBeApplied( six.text_type(e)) else: - six.reraise(*exc_info) + raise e except Exception: with excutils.save_and_reraise_exception(): LOG.exception("Neutron Error:") @@ -677,7 +666,6 @@ def remove_from_instance(context, instance, security_group_name): security_group_name, context.project_id) except n_exc.NeutronClientException as e: - exc_info = sys.exc_info() if e.status_code == 404: msg = (_("Security group %(name)s is not found for " "project %(project)s") % @@ -685,7 +673,7 @@ def remove_from_instance(context, instance, security_group_name): 'project': context.project_id}) raise exception.SecurityGroupNotFound(msg) else: - six.reraise(*exc_info) + raise e params = {'device_id': instance.uuid} try: ports = neutron.list_ports(**params).get('ports') diff --git a/nova/test.py b/nova/test.py index 12477ff6cc50..dd0fc4daa595 100644 --- a/nova/test.py +++ b/nova/test.py @@ -137,7 +137,7 @@ class NovaExceptionReraiseFormatError(object): def _wrap_log_exception(self): exc_info = sys.exc_info() NovaExceptionReraiseFormatError.real_log_exception(self) - six.reraise(*exc_info) + raise exc_info[1] # NOTE(melwitt) This needs to be done at import time in order to also catch diff --git a/nova/tests/functional/notification_sample_tests/test_instance.py b/nova/tests/functional/notification_sample_tests/test_instance.py index 594bf4ad42e1..a57319129a1a 100644 --- a/nova/tests/functional/notification_sample_tests/test_instance.py +++ b/nova/tests/functional/notification_sample_tests/test_instance.py @@ -1120,8 +1120,8 @@ class TestInstanceNotificationSample( call, but the rescheduled also was unsuccessful. In this case called the exception block. In the exception block send a notification about error. - At end called the six.reraise(*exc_info), which not - send another error. + At end called raising an exception based on *exc_info, + which not send another error. """ def _build_resources(*args, **kwargs): raise exception.FlavorDiskTooSmall() diff --git a/nova/utils.py b/nova/utils.py index d7845bae06a2..84c28224c87d 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -584,7 +584,7 @@ class ExceptionHelper(object): try: return func(*args, **kwargs) except messaging.ExpectedException as e: - six.reraise(*e.exc_info) + raise e.exc_info[1] return wrapper diff --git a/nova/virt/hyperv/driver.py b/nova/virt/hyperv/driver.py index 8c3c1aaa4348..fc42f7955161 100644 --- a/nova/virt/hyperv/driver.py +++ b/nova/virt/hyperv/driver.py @@ -24,7 +24,6 @@ import sys from os_win import exceptions as os_win_exc from os_win import utilsfactory from oslo_log import log as logging -import six from nova import context as nova_context from nova import exception @@ -62,12 +61,10 @@ def convert_exceptions(function, exception_map): break exc_info = sys.exc_info() - # NOTE(claudiub): Python 3 raises the exception object given as - # the second argument in six.reraise. - # The original message will be maintained by passing the original - # exception. - exc = raised_exception(six.text_type(exc_info[1])) - six.reraise(raised_exception, exc, exc_info[2]) + # NOTE(claudiub): The original message will be maintained + # by passing the original exception. + exc = raised_exception(str(exc_info[1])) + raise exc.with_traceback(exc_info[2]) return wrapper diff --git a/nova/volume/cinder.py b/nova/volume/cinder.py index 5bb56bf61019..fdbcb50c9bb7 100644 --- a/nova/volume/cinder.py +++ b/nova/volume/cinder.py @@ -480,7 +480,7 @@ def translate_mixed_exceptions(method): def _reraise(desired_exc): - six.reraise(type(desired_exc), desired_exc, sys.exc_info()[2]) + raise desired_exc.with_traceback(sys.exc_info()[2]) class API(object):