Improve Python 3.x compatibility

Mechanical translation of the deprecated
except x,y: construct with except x as y:
The latter works with any Python >= 2.6.
Add Hacking check.

Change-Id: I845829d97d379c1cd9b3a77e7e5786586f263b64
This commit is contained in:
Dirk Mueller
2013-05-18 00:18:18 +02:00
parent 4a06f9fa59
commit 4b0556e32f
7 changed files with 28 additions and 9 deletions

View File

@@ -211,6 +211,25 @@ Example::
LOG.error(msg % {"s_id": "1234", "m_key": "imageId"})
Python 3.x compatibility
------------------------
Nova code should stay Python 3.x compatible. That means all Python 2.x-only
constructs should be avoided. An example is
except x,y:
Use
except x as y:
instead. Other Python 3.x compatility issues, like e.g. print operator
can be avoided in new code by using
from __future__ import print_function
at the top of your module.
Creating Unit Tests
-------------------
For every new feature, unit tests should be created that both test and

View File

@@ -73,7 +73,7 @@ def wrap_exception(notifier=None, publisher_id=None, event_type=None,
# contain confidential information.
try:
return f(self, context, *args, **kw)
except Exception, e:
except Exception as e:
with excutils.save_and_reraise_exception():
if notifier:
payload = dict(exception=e)

View File

@@ -203,7 +203,7 @@ def trycmd(*args, **kwargs):
try:
out, err = execute(*args, **kwargs)
failed = False
except ProcessExecutionError, exn:
except ProcessExecutionError as exn:
out, err = '', str(exn)
failed = True

View File

@@ -66,7 +66,7 @@ class SchedulerOptions(object):
"""Get the last modified datetime. Broken out for testing."""
try:
return os.path.getmtime(filename)
except os.error, e:
except os.error as e:
with excutils.save_and_reraise_exception():
LOG.exception(_("Could not stat scheduler options file "
"%(filename)s: '%(e)s'"), locals())
@@ -75,7 +75,7 @@ class SchedulerOptions(object):
"""Decode the JSON file. Broken out for testing."""
try:
return json.load(handle)
except ValueError, e:
except ValueError as e:
LOG.exception(_("Could not decode scheduler options: "
"'%(e)s'") % locals())
return {}

View File

@@ -309,7 +309,7 @@ class ApiEc2TestCase(test.TestCase):
try:
self.ec2.create_key_pair('test')
except boto_exc.EC2ResponseError, e:
except boto_exc.EC2ResponseError as e:
if e.code == 'InvalidKeyPair.Duplicate':
pass
else:

View File

@@ -219,7 +219,7 @@ class BaseMigrationTestCase(test.TestCase):
for key, value in defaults.items():
self.test_databases[key] = value
self.snake_walk = cp.getboolean('walk_style', 'snake_walk')
except ConfigParser.ParsingError, e:
except ConfigParser.ParsingError as e:
self.fail("Failed to read test_migrations.conf config "
"file. Got error: %s" % e)
else:

View File

@@ -927,7 +927,7 @@ def tempdir(**kwargs):
finally:
try:
shutil.rmtree(tmpdir)
except OSError, e:
except OSError as e:
LOG.error(_('Could not remove tmpdir: %s'), str(e))
@@ -1007,7 +1007,7 @@ def last_bytes(file_like_object, num):
try:
file_like_object.seek(-num, os.SEEK_END)
except IOError, e:
except IOError as e:
if e.errno == 22:
file_like_object.seek(0, os.SEEK_SET)
else:
@@ -1074,7 +1074,7 @@ class ExceptionHelper(object):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except rpc_common.ClientException, e:
except rpc_common.ClientException as e:
raise (e._exc_info[1], None, e._exc_info[2])
return wrapper