aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/ast/interpreter.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/ast/interpreter.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/ast/interpreter.py')
-rw-r--r--mesonbuild/ast/interpreter.py6
1 files changed, 3 insertions, 3 deletions
diff --git a/mesonbuild/ast/interpreter.py b/mesonbuild/ast/interpreter.py
index a82ba5d..a75c0b7 100644
--- a/mesonbuild/ast/interpreter.py
+++ b/mesonbuild/ast/interpreter.py
@@ -22,7 +22,7 @@ from .. import environment
from ..interpreterbase import InvalidArguments, BreakRequest, ContinueRequest
import os, sys
-from typing import List
+from typing import List, Optional
class DontCareObject(interpreterbase.InterpreterObject):
pass
@@ -46,9 +46,9 @@ ADD_SOURCE = 0
REMOVE_SOURCE = 1
class AstInterpreter(interpreterbase.InterpreterBase):
- def __init__(self, source_root: str, subdir: str, visitors: List[AstVisitor] = []):
+ def __init__(self, source_root: str, subdir: str, visitors: Optional[List[AstVisitor]] = None):
super().__init__(source_root, subdir)
- self.visitors = visitors
+ self.visitors = visitors if visitors is not None else []
self.visited_subdirs = {}
self.assignments = {}
self.assign_vals = {}