Don't use instances of complex types as a default value in args

This can produce unexpected buthurt (at least for me).
Cause python has super strange behavior in this case.

E.g.

def a(t=[]):
    t.append(1)
    print(t)

a() # prints [1]
a() # prints [1, 1]

Change-Id: Ie5287dd4c958d2d1ccaf98486b9522a2d2c18724
This commit is contained in:
Boris Pavlovic 2014-06-19 09:46:20 +04:00
parent 4063937f84
commit edd165773f

View File

@ -82,7 +82,8 @@ class CreateGroupException(Exception):
pass pass
def run_command(cmd, status=False, env={}): def run_command(cmd, status=False, env=None):
env = env or {}
cmd_list = shlex.split(str(cmd)) cmd_list = shlex.split(str(cmd))
newenv = os.environ newenv = os.environ
newenv.update(env) newenv.update(env)
@ -97,25 +98,29 @@ def run_command(cmd, status=False, env={}):
return out.strip() return out.strip()
def run_command_status(cmd, env={}): def run_command_status(cmd, env=None):
env = env or {}
return run_command(cmd, True, env) return run_command(cmd, True, env)
def git_command(repo_dir, sub_cmd, env={}): def git_command(repo_dir, sub_cmd, env=None):
env = env or {}
git_dir = os.path.join(repo_dir, '.git') git_dir = os.path.join(repo_dir, '.git')
cmd = "git --git-dir=%s --work-tree=%s %s" % (git_dir, repo_dir, sub_cmd) cmd = "git --git-dir=%s --work-tree=%s %s" % (git_dir, repo_dir, sub_cmd)
status, _ = run_command(cmd, True, env) status, _ = run_command(cmd, True, env)
return status return status
def git_command_output(repo_dir, sub_cmd, env={}): def git_command_output(repo_dir, sub_cmd, env=None):
env = env or {}
git_dir = os.path.join(repo_dir, '.git') git_dir = os.path.join(repo_dir, '.git')
cmd = "git --git-dir=%s --work-tree=%s %s" % (git_dir, repo_dir, sub_cmd) cmd = "git --git-dir=%s --work-tree=%s %s" % (git_dir, repo_dir, sub_cmd)
status, out = run_command(cmd, True, env) status, out = run_command(cmd, True, env)
return (status, out) return (status, out)
def fetch_config(project, remote_url, repo_path, env={}): def fetch_config(project, remote_url, repo_path, env=None):
env = env or {}
# Poll for refs/meta/config as gerrit may not have written it out for # Poll for refs/meta/config as gerrit may not have written it out for
# us yet. # us yet.
for x in range(10): for x in range(10):
@ -178,7 +183,8 @@ def copy_acl_config(project, repo_path, acl_config):
return status != 0 return status != 0
def push_acl_config(project, remote_url, repo_path, gitid, env={}): def push_acl_config(project, remote_url, repo_path, gitid, env=None):
env = env or {}
cmd = "commit -a -m'Update project config.' --author='%s'" % gitid cmd = "commit -a -m'Update project config.' --author='%s'" % gitid
status = git_command(repo_path, cmd) status = git_command(repo_path, cmd)
if status != 0: if status != 0: