Bug 1402010 - Avoid mutable default argument value
Mutable default argument values lead to surprises since Python
reuses objects on subsequent calls if the argument isn't passed!
MozReview-Commit-ID: BVLiwl2Jymi
--- a/python/mozbuild/mozbuild/frontend/reader.py
+++ b/python/mozbuild/mozbuild/frontend/reader.py
@@ -182,16 +182,17 @@ class MozbuildSandbox(Sandbox):
context is a Context instance.
metadata is a dict of metadata that can be used during the sandbox
evaluation.
"""
def __init__(self, context, metadata={}, finder=default_finder):
assert isinstance(context, Context)
+ metadata = metadata or {}
Sandbox.__init__(self, context, finder=finder)
self._log = logging.getLogger(__name__)
self.metadata = dict(metadata)
exports = self.metadata.get('exports', {})
self.exports = set(exports.keys())
@@ -1025,17 +1026,17 @@ class BuildReader(object):
source = fh.read()
tree = ast.parse(source, full)
Visitor().visit(tree)
for name, key, value in assignments:
yield p, name, key, value
- def read_mozbuild(self, path, config, descend=True, metadata={}):
+ def read_mozbuild(self, path, config, descend=True, metadata=None):
"""Read and process a mozbuild file, descending into children.
This starts with a single mozbuild file, executes it, and descends into
other referenced files per our traversal logic.
The traversal logic is to iterate over the *DIRS variables, treating
each element as a relative directory path. For each encountered
directory, we will open the moz.build file located in that
@@ -1046,16 +1047,18 @@ class BuildReader(object):
Arbitrary metadata in the form of a dict can be passed into this
function. This feature is intended to facilitate the build reader
injecting state and annotations into moz.build files that is
independent of the sandbox's execution context.
Traversal is performed depth first (for no particular reason).
"""
+ metadata = metadata or {}
+
self._execution_stack.append(path)
try:
for s in self._read_mozbuild(path, config, descend=descend,
metadata=metadata):
yield s
except BuildReaderError as bre:
raise bre