aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/mesonmain.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2019-04-22 14:26:18 -0700
committerJussi Pakkanen <jpakkane@gmail.com>2019-04-23 02:03:19 +0300
commitadd821db64b3c1fd7568736aaa67de53ad382eb4 (patch)
treee27276655f7e251706552e9093f86b49a1b5b6d6 /mesonbuild/mesonmain.py
parent0259cc6de21c223f1f38f0a6d118779f455b881f (diff)
downloadmeson-add821db64b3c1fd7568736aaa67de53ad382eb4.zip
meson-add821db64b3c1fd7568736aaa67de53ad382eb4.tar.gz
meson-add821db64b3c1fd7568736aaa67de53ad382eb4.tar.bz2
Don't use mutable types as default arguments
This isn't safe given the way python implements default arguments. Basically python store a reference to the instance it was passed, and then if that argument is not provided it uses the default. That means that two calls to the same function get the same instance, if one of them mutates that instance every subsequent call that gets the default will receive the mutated instance. The idiom to this in python is to use None and replace the None, def in(value: str, container: Optional[List[str]]) -> boolean: return src in (container or []) if there is no chance of mutation it's less code to use or and take advantage of None being falsy. If you may want to mutate the value passed in you need a ternary (this example is stupid): def add(value: str, container: Optional[List[str]]) -> None: container = container if container is not None else [] container.append(value) I've used or everywhere I'm sure that the value will not be mutated by the function and erred toward caution by using ternaries for the rest.
Diffstat (limited to 'mesonbuild/mesonmain.py')
-rw-r--r--mesonbuild/mesonmain.py3
1 files changed, 2 insertions, 1 deletions
diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py
index e7005e9..f80dfdd 100644
--- a/mesonbuild/mesonmain.py
+++ b/mesonbuild/mesonmain.py
@@ -67,7 +67,8 @@ class CommandLineParser:
self.add_command('unstable-coredata', munstable_coredata.add_arguments, munstable_coredata.run,
help=argparse.SUPPRESS)
- def add_command(self, name, add_arguments_func, run_func, help, aliases=[]):
+ def add_command(self, name, add_arguments_func, run_func, help, aliases=None):
+ aliases = aliases or []
# FIXME: Cannot have hidden subparser:
# https://bugs.python.org/issue22848
if help == argparse.SUPPRESS: