Adds Python 3.x compatible exception handling

Fixes exception handling syntax for Python 3.x compatibility

Changes generated with:

    2to3 -f except -w -n cloudbaseinit
This commit is contained in:
Alessandro Pilotti 2014-09-07 21:03:01 +03:00
parent 2a9be0e16d
commit b59a89e5e5
13 changed files with 14 additions and 14 deletions

View File

@ -70,7 +70,7 @@ class InitManager(object):
self._set_plugin_status(osutils, instance_id, plugin_name,
status)
return reboot_required
except Exception, ex:
except Exception as ex:
LOG.error('plugin \'%(plugin_name)s\' failed with error '
'\'%(ex)s\'', {'plugin_name': plugin_name, 'ex': ex})
LOG.exception(ex)
@ -124,7 +124,7 @@ class InitManager(object):
if reboot_required and CONF.allow_reboot:
try:
osutils.reboot()
except Exception, ex:
except Exception as ex:
LOG.error('reboot failed with error \'%s\'' % ex)
elif CONF.stop_service_on_exit:
osutils.terminate()

View File

@ -47,7 +47,7 @@ def get_metadata_service():
try:
if service.load():
return service
except Exception, ex:
except Exception as ex:
LOG.error("Failed to load metadata service '%s'" % class_path)
LOG.exception(ex)
raise Exception("No available service found")

View File

@ -51,7 +51,7 @@ class EC2Service(base.BaseMetadataService):
try:
self.get_host_name()
return True
except Exception, ex:
except Exception as ex:
LOG.exception(ex)
LOG.debug('Metadata not found at URL \'%s\'' %
CONF.ec2_metadata_base_url)

View File

@ -59,7 +59,7 @@ class MaaSHttpService(base.BaseMetadataService):
try:
self._get_data('%s/meta-data/' % self._metadata_version)
return True
except Exception, ex:
except Exception as ex:
LOG.exception(ex)
LOG.debug('Metadata not found at URL \'%s\'' %
CONF.maas_metadata_url)

View File

@ -593,7 +593,7 @@ class WindowsUtils(base.BaseOSUtils):
time.sleep(1)
LOG.info('Waiting for sysprep completion. '
'GeneralizationState: %d' % gen_state)
except WindowsError, ex:
except WindowsError as ex:
if ex.winerror == 2:
LOG.debug('Sysprep data not found in the registry, '
'skipping sysprep completion check.')

View File

@ -57,5 +57,5 @@ def exec_file(file_path):
LOG.debug('User_data stderr:\n%s' % err)
return ret_val
except Exception, ex:
except Exception as ex:
LOG.warning('An error occurred during file execution: \'%s\'' % ex)

View File

@ -106,7 +106,7 @@ class UserDataPlugin(base.BasePlugin):
new_user_handlers)
else:
ret_val = user_data_plugin.process(part)
except Exception, ex:
except Exception as ex:
LOG.error('Exception during multipart part handling: '
'%(content_type)s, %(filename)s' %
{'content_type': part.get_content_type(),

View File

@ -36,7 +36,7 @@ class ShellScriptPlugin(base.BaseUserDataPlugin):
f.write(part.get_payload())
return fileexecutils.exec_file(target_path)
except Exception, ex:
except Exception as ex:
LOG.warning('An error occurred during user_data execution: \'%s\''
% ex)
finally:

View File

@ -69,7 +69,7 @@ def execute_user_data_script(user_data):
LOG.debug('User_data stderr:\n%s' % err)
return ret_val
except Exception, ex:
except Exception as ex:
LOG.warning('An error occurred during user_data execution: \'%s\''
% ex)
finally:

View File

@ -108,7 +108,7 @@ def _bind_dhcp_client_socket(s, max_bind_attempts, bind_retry_interval):
try:
s.bind(('', 68))
break
except socket.error, ex:
except socket.error as ex:
if (bind_attempts >= max_bind_attempts or
ex.errno not in [48, 10048]):
raise

View File

@ -60,6 +60,6 @@ def check_metadata_ip_route(metadata_url):
gateway,
interface_index,
10)
except Exception, ex:
except Exception as ex:
# Ignore it
LOG.exception(ex)

View File

@ -58,7 +58,7 @@ def _get_registry_dhcp_server(adapter_name):
if dhcp_server == "255.255.255.255":
dhcp_server = None
return dhcp_server
except Exception, ex:
except Exception as ex:
# Not found
if ex.errno != 2:
raise

View File

@ -103,7 +103,7 @@ class WinRMConfig(object):
session = self._get_wsman_session()
try:
return session.Get(resource_uri)
except pywintypes.com_error, ex:
except pywintypes.com_error as ex:
if len(ex.excepinfo) > 5 and ex.excepinfo[5] == -2144108544:
return None
else: