Bug 1316250 - Don't pretend imported modules in templates are inherited by functions they contain. r?chmanchester
The current state of python configure sandbox execution is that if a
template imports a module, and a function defined in the template tries
to use the module, it doesn't work. Ideally, it would, but rather than
try to fix this corner case, we remove the unit tests that assume it
works (and consequently pass for half bad reasons), and add a unit test
so that the behavior doesn't change unwillingly.
--- a/python/mozbuild/mozbuild/test/configure/test_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_configure.py
@@ -334,16 +334,34 @@ class TestConfigure(unittest.TestCase):
)
self.assertIs(sandbox['foo'](), sandbox)
# Nothing leaked from the function being executed
self.assertEquals(sandbox.keys(), ['__builtins__', 'foo'])
self.assertEquals(sandbox['__builtins__'], ConfigureSandbox.BUILTINS)
+ exec_(textwrap.dedent('''
+ @template
+ @imports('sys')
+ def foo():
+ @depends(when=True)
+ def bar():
+ return sys
+ return bar
+ bar = foo()'''),
+ sandbox
+ )
+
+ with self.assertRaises(NameError) as e:
+ sandbox._depends[sandbox['bar']].result
+
+ self.assertEquals(e.exception.message,
+ "global name 'sys' is not defined")
+
def test_apply_imports(self):
imports = []
class CountApplyImportsSandbox(ConfigureSandbox):
def _apply_imports(self, *args, **kwargs):
imports.append((args, kwargs))
super(CountApplyImportsSandbox, self)._apply_imports(
*args, **kwargs)
--- a/python/mozbuild/mozbuild/test/configure/test_lint.py
+++ b/python/mozbuild/mozbuild/test/configure/test_lint.py
@@ -84,38 +84,16 @@ class TestLint(unittest.TestCase):
tmpl()
'''):
self.lint_test()
self.assertEquals(e.exception.message,
"`bar` depends on '--help' and `foo`. "
"`foo` must depend on '--help'")
- with self.assertRaises(ConfigureError) as e:
- with self.moz_configure('''
- @template
- @imports('os')
- def tmpl():
- option('--foo', help='foo')
- @depends('--foo')
- def foo(value):
- os
- return value
-
- @depends('--help', foo)
- def bar(help, foo):
- return
- tmpl()
- '''):
- self.lint_test()
-
- self.assertEquals(e.exception.message,
- "`bar` depends on '--help' and `foo`. "
- "`foo` must depend on '--help'")
-
with self.moz_configure('''
option('--foo', help='foo')
@depends('--foo')
def foo(value):
return value
include(foo)
'''):
@@ -131,32 +109,11 @@ class TestLint(unittest.TestCase):
include(foo)
'''):
self.lint_test()
self.assertEquals(e.exception.message,
"Missing @depends for `foo`: '--help'")
- with self.assertRaises(ConfigureError) as e:
- with self.moz_configure('''
- @template
- @imports('os')
- def tmpl():
- option('--foo', help='foo')
- @depends('--foo')
- def foo(value):
- os
- return value
- return foo
-
- foo = tmpl()
-
- include(foo)
- '''):
- self.lint_test()
-
- self.assertEquals(e.exception.message,
- "Missing @depends for `foo`: '--help'")
-
if __name__ == '__main__':
main()