diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2019-04-22 14:26:18 -0700 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2019-04-23 02:03:19 +0300 |
commit | add821db64b3c1fd7568736aaa67de53ad382eb4 (patch) | |
tree | e27276655f7e251706552e9093f86b49a1b5b6d6 /mesonbuild/build.py | |
parent | 0259cc6de21c223f1f38f0a6d118779f455b881f (diff) | |
download | meson-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/build.py')
-rw-r--r-- | mesonbuild/build.py | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 66f08d8..5248d97 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -249,12 +249,12 @@ class ExtractedObjects: ''' Holds a list of sources for which the objects must be extracted ''' - def __init__(self, target, srclist=[], genlist=[], objlist=[], recursive=True): + def __init__(self, target, srclist=None, genlist=None, objlist=None, recursive=True): self.target = target self.recursive = recursive - self.srclist = srclist - self.genlist = genlist - self.objlist = objlist + self.srclist = srclist if srclist is not None else [] + self.genlist = genlist if genlist is not None else [] + self.objlist = objlist if objlist is not None else [] if self.target.is_unity: self.check_unity_compatible() @@ -1339,8 +1339,8 @@ class Generator: relpath = pathlib.PurePath(trial).relative_to(parent) return relpath.parts[0] != '..' # For subdirs we can only go "down". - def process_files(self, name, files, state, preserve_path_from=None, extra_args=[]): - output = GeneratedList(self, state.subdir, preserve_path_from, extra_args=extra_args) + def process_files(self, name, files, state, preserve_path_from=None, extra_args=None): + output = GeneratedList(self, state.subdir, preserve_path_from, extra_args=extra_args if extra_args is not None else []) for f in files: if isinstance(f, str): f = File.from_source_file(state.environment.source_dir, state.subdir, f) @@ -1355,7 +1355,7 @@ class Generator: class GeneratedList: - def __init__(self, generator, subdir, preserve_path_from=None, extra_args=[]): + def __init__(self, generator, subdir, preserve_path_from=None, extra_args=None): if hasattr(generator, 'held_object'): generator = generator.held_object self.generator = generator @@ -1367,7 +1367,7 @@ class GeneratedList: self.extra_depends = [] self.depend_files = [] self.preserve_path_from = preserve_path_from - self.extra_args = extra_args + self.extra_args = extra_args if extra_args is not None else [] if isinstance(generator.exe, dependencies.ExternalProgram): if not generator.exe.found(): raise InvalidArguments('Tried to use not-found external program as generator') |