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/backend/ninjabackend.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/backend/ninjabackend.py')
-rw-r--r-- | mesonbuild/backend/ninjabackend.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 08b14c8..8edaeec 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -2098,10 +2098,13 @@ rule FORTRAN_DEP_HACK%s commands += compiler.get_include_args(self.get_target_private_dir(target), False) return commands - def generate_single_compile(self, target, outfile, src, is_generated=False, header_deps=[], order_deps=[]): + def generate_single_compile(self, target, outfile, src, is_generated=False, header_deps=None, order_deps=None): """ Compiles C/C++, ObjC/ObjC++, Fortran, and D sources """ + header_deps = header_deps if header_deps is not None else [] + order_deps = order_deps if order_deps is not None else [] + if isinstance(src, str) and src.endswith('.h'): raise AssertionError('BUG: sources should not contain headers {!r}'.format(src)) @@ -2256,7 +2259,8 @@ rule FORTRAN_DEP_HACK%s dep = dst + '.' + compiler.get_depfile_suffix() return commands, dep, dst, [] # Gcc does not create an object file during pch generation. - def generate_pch(self, target, outfile, header_deps=[]): + def generate_pch(self, target, outfile, header_deps=None): + header_deps = header_deps if header_deps is not None else [] cstr = '' pch_objects = [] if target.is_cross: @@ -2435,7 +2439,9 @@ rule FORTRAN_DEP_HACK%s return guessed_dependencies + absolute_libs - def generate_link(self, target, outfile, outname, obj_list, linker, extra_args=[], stdlib_args=[]): + def generate_link(self, target, outfile, outname, obj_list, linker, extra_args=None, stdlib_args=None): + extra_args = extra_args if extra_args is not None else [] + stdlib_args = stdlib_args if stdlib_args is not None else [] if isinstance(target, build.StaticLibrary): linker_base = 'STATIC' else: |