Bug 1063414 - Move sandbox execution stack to context.Context. r=gps

This commit is contained in:
Mike Hommey 2014-10-02 09:14:07 +09:00
parent be7f345453
commit b8d9ec3440
3 changed files with 16 additions and 14 deletions

View File

@ -182,9 +182,9 @@ class MozbuildSandbox(Sandbox):
mozpath.join(self._context.config.topsrcdir, path[1:]))
elif srcdir:
return mozpath.normpath(mozpath.join(srcdir, path))
elif len(self._execution_stack):
elif self._context.current_path:
return mozpath.normpath(mozpath.join(
mozpath.dirname(self._execution_stack[-1]), path))
mozpath.dirname(self._context.current_path), path))
else:
return mozpath.normpath(
mozpath.join(self._context.config.topsrcdir, path))
@ -201,7 +201,7 @@ class MozbuildSandbox(Sandbox):
normalized_path = self.normalize_path(path,
filesystem_absolute=filesystem_absolute)
if not is_read_allowed(normalized_path, self._context.config):
raise SandboxLoadError(list(self._execution_stack),
raise SandboxLoadError(self._context.source_stack,
sys.exc_info()[2], illegal_path=path)
Sandbox.exec_file(self, normalized_path)
@ -285,7 +285,7 @@ class MozbuildSandbox(Sandbox):
print('WARNING: %s' % message, file=sys.stderr)
def _error(self, message):
raise SandboxCalledError(self._execution_stack, message)
raise SandboxCalledError(self._context.source_stack, message)
def _template_decorator(self, func):
"""Registers template as expected by _create_template_function.
@ -346,7 +346,7 @@ class MozbuildSandbox(Sandbox):
code = '\n' * (firstlineno + begin[0] - 3) + 'if True:\n'
code += ''.join(lines[begin[0] - 1:])
self.templates[name] = func, code, self._execution_stack[-1]
self.templates[name] = func, code, self._context.current_path
@memoize
def _create_template_function(self, template):
@ -363,7 +363,7 @@ class MozbuildSandbox(Sandbox):
def template_function(*args, **kwargs):
context = TemplateContext(VARIABLES, self._context.config)
context.add_source(self._execution_stack[-1])
context.add_source(self._context.current_path)
for p in self._context.all_paths:
context.add_source(p)

View File

@ -117,7 +117,6 @@ class Sandbox(dict):
assert isinstance(context, Context)
self._context = context
self._execution_stack = []
# We need to record this because it gets swallowed as part of
# evaluation.
@ -136,7 +135,7 @@ class Sandbox(dict):
with open(path, 'rt') as fd:
source = fd.read()
except Exception as e:
raise SandboxLoadError(list(self._execution_stack),
raise SandboxLoadError(self._context.source_stack,
sys.exc_info()[2], read_error=path)
self.exec_source(source, path)
@ -151,10 +150,8 @@ class Sandbox(dict):
does not perform extra path normalization. This can cause relative
paths to behave weirdly.
"""
self._execution_stack.append(path)
if path:
self._context.add_source(path)
self._context.push_source(path)
# We don't have to worry about bytecode generation here because we are
# too low-level for that. However, we could add bytecode generation via
@ -183,17 +180,18 @@ class Sandbox(dict):
if self._last_name_error is not None:
actual = self._last_name_error
raise SandboxExecutionError(list(self._execution_stack),
raise SandboxExecutionError(self._context.source_stack,
type(actual), actual, sys.exc_info()[2])
except Exception as e:
# Need to copy the stack otherwise we get a reference and that is
# mutated during the finally.
exc = sys.exc_info()
raise SandboxExecutionError(list(self._execution_stack), exc[0],
raise SandboxExecutionError(self._context.source_stack, exc[0],
exc[1], exc[2])
finally:
self._execution_stack.pop()
if path:
self._context.pop_source()
def __getitem__(self, key):
if key.isupper():

View File

@ -354,6 +354,7 @@ SOURCES += ['hoge.cpp']
'DIRS': [sandbox.normalize_path('foo')],
})
sandbox2 = self.sandbox(metadata={'templates': sandbox.templates})
source = '''
TemplateError([
'foo.cpp',
@ -371,6 +372,7 @@ TemplateError([
# TemplateGlobalVariable tries to access 'illegal' but that is expected
# to throw.
sandbox2 = self.sandbox(metadata={'templates': sandbox.templates})
source = '''
illegal = True
TemplateGlobalVariable()
@ -416,6 +418,7 @@ SOURCES += ['hoge.cpp']
# Template names must be CamelCase. Here, we can define the template
# inline because the error happens before inspect.getsourcelines.
sandbox2 = self.sandbox(metadata={'templates': sandbox.templates})
source = '''
@template
def foo():
@ -433,6 +436,7 @@ def foo():
'Template function names must be CamelCase.')
# Template names must not already be registered.
sandbox2 = self.sandbox(metadata={'templates': sandbox.templates})
source = '''
@template
def Template():