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 <takanattie@gmail.com>
This commit is contained in:
Takashi Natsume 2020-05-11 15:56:19 +00:00
parent 5191b4f2f0
commit 2c074b9486
9 changed files with 34 additions and 47 deletions

View File

@ -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 {}

View File

@ -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)

View File

@ -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):

View File

@ -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')

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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):