Remove arguments "{}" and "[]" in function definitions

remove mutable default arguments "{}" and "[]" in function definitions
refer to:http://docs.python-guide.org/en/latest/writing/gotchas/

Change-Id: Ib03d9820a259b2cbd83aa8bd675ac6153cf78ebd
Closes-Bug: #1527080
This commit is contained in:
zhurong 2015-12-16 23:27:06 +08:00
parent 10d8593003
commit c4d1f7e687
7 changed files with 39 additions and 13 deletions

View File

@ -62,7 +62,10 @@ def init(default_rule=None, use_conf=True):
_ENFORCER.load_rules()
def check(rule, ctxt, target={}, do_raise=True, exc=exceptions.HTTPForbidden):
def check(rule, ctxt, target=None, do_raise=True,
exc=exceptions.HTTPForbidden):
if target is None:
target = {}
creds = ctxt.to_dict()
try:

View File

@ -44,7 +44,9 @@ class NoMethodFound(Exception):
class NoClassFound(Exception):
def __init__(self, name, packages=[]):
def __init__(self, name, packages=None):
if packages is None:
packages = []
packages = ', '.join("{0}/{1}".format(p.name, p.version)
for p in packages)
super(NoClassFound, self).__init__(

View File

@ -67,7 +67,9 @@ class ActionServices(object):
return task_id
@staticmethod
def execute(action_id, session, unit, token, args={}):
def execute(action_id, session, unit, token, args=None):
if args is None:
args = {}
environment = actions_db.get_environment(session, unit)
action = ActionServices.find_action(session.description, action_id)
if action is None:

View File

@ -89,11 +89,13 @@ class MuranoApiTestCase(base.MuranoWithDBTestCase, FakeLogMixin):
super(MuranoApiTestCase, self).tearDown()
timeutils.utcnow.override_time = None
def _stub_uuid(self, values=[]):
def _stub_uuid(self, values=None):
class FakeUUID(object):
def __init__(self, v):
self.hex = v
if values is None:
values = []
mock_uuid4 = mock.patch('uuid.uuid4').start()
mock_uuid4.side_effect = [FakeUUID(v) for v in values]
return mock_uuid4
@ -128,7 +130,9 @@ class ControllerTest(object):
self._policy_check_expectations = []
self._actual_policy_checks = []
def wrap_policy_check(rule, ctxt, target={}, **kwargs):
def wrap_policy_check(rule, ctxt, target=None, **kwargs):
if target is None:
target = {}
self._actual_policy_checks.append((rule, target))
return real_policy_check(rule, ctxt, target=target, **kwargs)
@ -184,13 +188,16 @@ class ControllerTest(object):
def _delete(self, path, params=None, user=DEFAULT_USER,
tenant=DEFAULT_TENANT):
params = params or {}
if params is None:
params = {}
return self._simple_request(path, params=params, method='DELETE',
user=user, tenant=tenant)
def _data_request(self, path, data, content_type='application/json',
method='POST', params={},
method='POST', params=None,
user=DEFAULT_USER, tenant=DEFAULT_TENANT):
if params is None:
params = {}
environ = self._environ(path)
environ['REQUEST_METHOD'] = method
@ -207,20 +214,26 @@ class ControllerTest(object):
return req
def _post(self, path, data, content_type='application/json', params={},
def _post(self, path, data, content_type='application/json', params=None,
user=DEFAULT_USER, tenant=DEFAULT_TENANT):
if params is None:
params = {}
return self._data_request(path, data, content_type, params=params,
user=user, tenant=tenant)
def _put(self, path, data, content_type='application/json', params={},
def _put(self, path, data, content_type='application/json', params=None,
user=DEFAULT_USER, tenant=DEFAULT_TENANT):
if params is None:
params = {}
return self._data_request(path, data, content_type, method='PUT',
params=params, user=user, tenant=tenant)
def _set_policy_rules(self, rules):
policy.set_rules(rules, default_rule='default')
def expect_policy_check(self, action, target={}):
def expect_policy_check(self, action, target=None):
if target is None:
target = {}
self._policy_check_expectations.append((action, target))
def _assert_policy_checks(self):

View File

@ -85,11 +85,13 @@ class TestTemplateServices(base.MuranoWithDBTestCase,
self.test_create_template()
self.template_services.delete("template_id")
def _stub_uuid(self, values=[]):
def _stub_uuid(self, values=None):
class FakeUUID(object):
def __init__(self, v):
self.hex = v
if values is None:
values = []
mock_uuid4 = mock.patch('uuid.uuid4').start()
mock_uuid4.side_effect = [FakeUUID(v) for v in values]
return mock_uuid4

View File

@ -300,11 +300,13 @@ class TestExecutionPlan(base.MuranoTestCase):
'Version': '1.0.0'
}
def _stub_uuid(self, values=[]):
def _stub_uuid(self, values=None):
class FakeUUID(object):
def __init__(self, v):
self.hex = v
if values is None:
values = []
mock_uuid4 = mock.patch('uuid.uuid4').start()
mock_uuid4.side_effect = [FakeUUID(v) for v in values]
return mock_uuid4

View File

@ -18,8 +18,10 @@ from murano.db import session
def dummy_context(user='test_username', tenant_id='test_tenant_id',
password='password', roles=[], user_id=None,
password='password', roles=None, user_id=None,
is_admin=False, request_id='dummy-request'):
if roles is None:
roles = []
# NOTE(kzaitsev) passing non-False value by default to request_id, to
# prevent generation during tests.
return context.RequestContext.from_dict({