From 0cdf5a1d4c9ec76d6c6f699685c1b4484b683616 Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Sun, 4 Aug 2013 17:14:04 +0200 Subject: [PATCH] Fix H501: Do not use locals() for string formatting Change-Id: Ide587d3311c22f698a82eeee210c5bdbe24eb484 --- nova/image/download/file.py | 7 +++--- nova/image/glance.py | 2 +- .../compute/contrib/test_admin_actions.py | 3 +-- .../compute/plugins/v3/test_admin_actions.py | 3 +-- nova/tests/compute/test_resource_tracker.py | 2 +- nova/tests/db/test_migrations.py | 22 ++++++++-------- nova/tests/fake_processutils.py | 4 +-- nova/tests/integrated/api/client.py | 15 +++++++---- nova/tests/integrated/test_api_samples.py | 25 +++++++++++-------- nova/tests/virt/powervm/test_powervm.py | 3 ++- tox.ini | 3 +-- 11 files changed, 49 insertions(+), 40 deletions(-) diff --git a/nova/image/download/file.py b/nova/image/download/file.py index f3ac7da85cfb..e9ccc9203b93 100644 --- a/nova/image/download/file.py +++ b/nova/image/download/file.py @@ -119,7 +119,7 @@ class FileTransfer(xfer_base.TransferBase): if r not in metadata: url = url_parts.geturl() msg = _('The key %(r)s is required in the location metadata ' - 'to access the url %(url)s.') % locals() + 'to access the url %(url)s.') % {'r': r, 'url': url} LOG.info(msg) raise exception.ImageDownloadModuleMetaDataError( module=str(self), reason=msg) @@ -128,8 +128,9 @@ class FileTransfer(xfer_base.TransferBase): def _normalize_destination(self, nova_mount, glance_mount, path): if not path.startswith(glance_mount): - msg = _('The mount point advertised by glance: %(glance_mount)s, ' - 'does not match the URL path: %(path)s') % locals() + msg = (_('The mount point advertised by glance: %(glance_mount)s, ' + 'does not match the URL path: %(path)s') % + {'glance_mount': glance_mount, 'path': path}) raise exception.ImageDownloadModuleMetaDataError( module=str(self), reason=msg) new_path = path.replace(glance_mount, nova_mount, 1) diff --git a/nova/image/glance.py b/nova/image/glance.py index 9c5c0abcb042..1da2a28fa5b9 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -311,7 +311,7 @@ class GlanceImageService(object): return None except Exception as ex: LOG.error(_("Failed to instantiate the download handler " - "for %(scheme)s") % locals()) + "for %(scheme)s") % {'scheme': scheme}) return def download(self, context, image_id, data=None): diff --git a/nova/tests/api/openstack/compute/contrib/test_admin_actions.py b/nova/tests/api/openstack/compute/contrib/test_admin_actions.py index e3136db081d4..ec267efeaad3 100644 --- a/nova/tests/api/openstack/compute/contrib/test_admin_actions.py +++ b/nova/tests/api/openstack/compute/contrib/test_admin_actions.py @@ -140,8 +140,7 @@ class AdminActionsTest(test.TestCase): req.content_type = 'application/json' res = req.get_response(app) self.assertEqual(res.status_int, 409) - self.assertIn("Cannot \'%(_action)s\' while instance" % locals(), - res.body) + self.assertIn("Cannot \'%s\' while instance" % _action, res.body) def test_admin_api_actions_raise_not_found(self): app = fakes.wsgi_app(init_only=('servers',)) diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_admin_actions.py b/nova/tests/api/openstack/compute/plugins/v3/test_admin_actions.py index 23a735dac58b..66787e3c584c 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_admin_actions.py +++ b/nova/tests/api/openstack/compute/plugins/v3/test_admin_actions.py @@ -152,8 +152,7 @@ class AdminActionsTest(test.TestCase): res = self._make_request('/v3/servers/%s/action' % self.UUID, {_action: None}) self.assertEqual(res.status_int, 409) - self.assertIn("Cannot \'%(_action)s\' while instance" % locals(), - res.body) + self.assertIn("Cannot \'%s\' while instance" % _action, res.body) def test_admin_api_actions_with_non_existed_instance(self): app = fakes.wsgi_app_v3(init_only=('servers', 'os-admin-actions')) diff --git a/nova/tests/compute/test_resource_tracker.py b/nova/tests/compute/test_resource_tracker.py index 8e8cc7df5f2a..5e95a2e6da0a 100644 --- a/nova/tests/compute/test_resource_tracker.py +++ b/nova/tests/compute/test_resource_tracker.py @@ -433,7 +433,7 @@ class BaseTrackerTestCase(BaseTestCase): if field not in tracker.compute_node: raise test.TestingException( - "'%(field)s' not in compute node." % locals()) + "'%(field)s' not in compute node." % {'field': field}) x = tracker.compute_node[field] self.assertEqual(value, x) diff --git a/nova/tests/db/test_migrations.py b/nova/tests/db/test_migrations.py index f782b1ec97f9..5575fe6f9445 100644 --- a/nova/tests/db/test_migrations.py +++ b/nova/tests/db/test_migrations.py @@ -86,8 +86,7 @@ def _get_connect_string(backend, user, passwd, database): else: raise Exception("Unrecognized backend: '%s'" % backend) - return ("%(backend)s://%(user)s:%(passwd)s@localhost/%(database)s" - % locals()) + return ("%s://%s:%s@localhost/%s" % (backend, user, passwd, database)) def _is_backend_avail(backend, user, passwd, database): @@ -262,13 +261,14 @@ class BaseMigrationTestCase(test.TestCase): # operations there is a special database template1. sqlcmd = ("psql -w -U %(user)s -h %(host)s -c" " '%(sql)s' -d template1") + sqldict = {'user': user, 'host': host} - sql = ("drop database if exists %(database)s;") % locals() - droptable = sqlcmd % locals() + sqldict['sql'] = ("drop database if exists %s;") % database + droptable = sqlcmd % sqldict self.execute_cmd(droptable) - sql = ("create database %(database)s;") % locals() - createtable = sqlcmd % locals() + sqldict['sql'] = ("create database %s;") % database + createtable = sqlcmd % sqldict self.execute_cmd(createtable) os.unsetenv('PGPASSWORD') @@ -294,9 +294,11 @@ class BaseMigrationTestCase(test.TestCase): (user, password, database, host) = \ get_mysql_connection_info(conn_pieces) sql = ("drop database if exists %(database)s; " - "create database %(database)s;") % locals() + "create database %(database)s;" + % {'database': database}) cmd = ("mysql -u \"%(user)s\" %(password)s -h %(host)s " - "-e \"%(sql)s\"") % locals() + "-e \"%(sql)s\"" % {'user': user, + 'password': password, 'host': host, 'sql': sql}) self.execute_cmd(cmd) elif conn_string.startswith('postgresql'): self._reset_pg(conn_pieces) @@ -324,7 +326,7 @@ class BaseMigrationTestCase(test.TestCase): total = connection.execute("SELECT count(*) " "from information_schema.TABLES " "where TABLE_SCHEMA='%(database)s'" % - locals()) + {'database': database}) self.assertTrue(total.scalar() > 0, "No tables found. Wrong schema?") noninnodb = connection.execute("SELECT count(*) " @@ -332,7 +334,7 @@ class BaseMigrationTestCase(test.TestCase): "where TABLE_SCHEMA='%(database)s' " "and ENGINE!='InnoDB' " "and TABLE_NAME!='migrate_version'" % - locals()) + {'database': database}) count = noninnodb.scalar() self.assertEqual(count, 0, "%d non InnoDB tables created" % count) connection.close() diff --git a/nova/tests/fake_processutils.py b/nova/tests/fake_processutils.py index b5b57c15eac5..4386cb5ea506 100644 --- a/nova/tests/fake_processutils.py +++ b/nova/tests/fake_processutils.py @@ -96,10 +96,8 @@ def fake_execute(*cmd_parts, **kwargs): LOG.debug(_('Faked command raised an exception %s'), e) raise - stdout = reply[0] - stderr = reply[1] LOG.debug(_("Reply to faked command is stdout='%(stdout)s' " - "stderr='%(stderr)s'") % locals()) + "stderr='%(stderr)s'") % {'stdout': reply[0], 'stderr': reply[1]}) # Replicate the sleep call in the real function greenthread.sleep(0) diff --git a/nova/tests/integrated/api/client.py b/nova/tests/integrated/api/client.py index 2c6b5c7c2200..0e18be116aee 100644 --- a/nova/tests/integrated/api/client.py +++ b/nova/tests/integrated/api/client.py @@ -36,8 +36,10 @@ class OpenStackApiException(Exception): _status = response.status _body = response.read() - message = _('%(message)s\nStatus Code: %(_status)s\n' - 'Body: %(_body)s') % locals() + message = (_('%(message)s\nStatus Code: %(_status)s\n' + 'Body: %(_body)s') % + {'message': message, '_status': _status, + '_body': _body}) super(OpenStackApiException, self).__init__(message) @@ -103,7 +105,8 @@ class TestOpenStackClient(object): relative_url = parsed_url.path if parsed_url.query: relative_url = relative_url + "?" + parsed_url.query - LOG.info(_("Doing %(method)s on %(relative_url)s") % locals()) + LOG.info(_("Doing %(method)s on %(relative_url)s") % + {'method': method, 'relative_url': relative_url}) if body: LOG.info(_("Body: %s") % body) @@ -123,7 +126,8 @@ class TestOpenStackClient(object): headers=headers) http_status = response.status - LOG.debug(_("%(auth_uri)s => code %(http_status)s") % locals()) + LOG.debug(_("%(auth_uri)s => code %(http_status)s") % + {'auth_uri': auth_uri, 'http_status': http_status}) if http_status == 401: raise OpenStackApiAuthenticationException(response=response) @@ -153,7 +157,8 @@ class TestOpenStackClient(object): response = self.request(full_uri, **kwargs) http_status = response.status - LOG.debug(_("%(relative_uri)s => code %(http_status)s") % locals()) + LOG.debug(_("%(relative_uri)s => code %(http_status)s") % + {'relative_uri': relative_uri, 'http_status': http_status}) if check_response_status: if http_status not in check_response_status: diff --git a/nova/tests/integrated/test_api_samples.py b/nova/tests/integrated/test_api_samples.py index b30c3ca907e9..bdc743d85f17 100644 --- a/nova/tests/integrated/test_api_samples.py +++ b/nova/tests/integrated/test_api_samples.py @@ -185,7 +185,7 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): if isinstance(expected, dict): if not isinstance(result, dict): raise NoMatch(_('%(result_str)s: %(result)s is not a dict.') - % locals()) + % {'result_str': result_str, 'result': result}) ex_keys = sorted(expected.keys()) res_keys = sorted(result.keys()) if ex_keys != res_keys: @@ -200,8 +200,9 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): raise NoMatch( _('Dictionary key mismatch:\n' 'Extra key(s) in template:\n%(ex_delta)s\n' - 'Extra key(s) in %(result_str)s:\n%(res_delta)s\n') - % locals()) + 'Extra key(s) in %(result_str)s:\n%(res_delta)s\n') % + {'ex_delta': ex_delta, 'result_str': result_str, + 'res_delta': res_delta}) for key in ex_keys: res = self._compare_result(subs, expected[key], result[key], result_str) @@ -209,7 +210,8 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): elif isinstance(expected, list): if not isinstance(result, list): raise NoMatch( - _('%(result_str)s: %(result)s is not a list.') % locals()) + _('%(result_str)s: %(result)s is not a list.') % + {'result_str': result_str, 'result': result}) expected = expected[:] extra = [] @@ -232,8 +234,8 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): error.extend([repr(o) for o in expected]) if extra: - error.append(_('Extra list items in %(result_str)s:') - % locals()) + error.append(_('Extra list items in %(result_str)s:') % + {'result_str': result_str}) error.extend([repr(o) for o in extra]) if error: @@ -254,8 +256,9 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): if not match: raise NoMatch( _('Values do not match:\n' - 'Template: %(expected)s\n%(result_str)s: %(result)s') - % locals()) + 'Template: %(expected)s\n%(result_str)s: %(result)s') % + {'expected': expected, 'result_str': result_str, + 'result': result}) try: matched_value = match.group('id') except IndexError: @@ -269,8 +272,10 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase): if expected != result: raise NoMatch( _('Values do not match:\n' - 'Template: %(expected)s\n%(result_str)s: %(result)s') - % locals()) + 'Template: %(expected)s\n%(result_str)s: ' + '%(result)s') % {'expected': expected, + 'result_str': result_str, + 'result': result}) return matched_value def generalize_subs(self, subs, vanilla_regexes): diff --git a/nova/tests/virt/powervm/test_powervm.py b/nova/tests/virt/powervm/test_powervm.py index b350ac4b5440..96ee88249d68 100644 --- a/nova/tests/virt/powervm/test_powervm.py +++ b/nova/tests/virt/powervm/test_powervm.py @@ -809,7 +809,8 @@ class PowerVMDriverLparTestCase(test.TestCase): exp_mac_str = mac[:-2].replace(':', '') exp_cmd = ('chsyscfg -r lpar -i "name=%(inst_name)s, ' - 'virtual_eth_mac_base_value=%(exp_mac_str)s"') % locals() + 'virtual_eth_mac_base_value=%(exp_mac_str)s"' + % {'inst_name': inst_name, 'exp_mac_str': exp_mac_str}) fake_op = self.powervm_connection._powervm self.mox.StubOutWithMock(fake_op._operator, 'run_vios_command') diff --git a/tox.ini b/tox.ini index 4a32de35aebc..cd497806f2c7 100644 --- a/tox.ini +++ b/tox.ini @@ -43,9 +43,8 @@ commands = {posargs} [flake8] # TODO Hacking 0.6 checks to fix # H102 Apache 2.0 license header not found -# H501 Do not use locals() for string formatting -ignore = E121,E122,E123,E124,E126,E127,E128,E711,E712,H102,H302,H404,F403,H501,F811,F841 +ignore = E121,E122,E123,E124,E126,E127,E128,E711,E712,H102,H302,H404,F403,F811,F841 exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,*plugins/xenserver*,tools [hacking]