Bug 1266343 - Use mozbuild.util.exec_ in the various configure tests. r?chmanchester
Because some older python 2.7 versions throw bogus errors when using the
exec statement as a function, use the function we added in
bug 1264831
everywhere we use exec in the various configure tests. It doesn't take
much to trigger them, and the following changes ends up doing exactly
that.
--- a/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
@@ -10,16 +10,17 @@ import textwrap
import unittest
from mozunit import main
from mozbuild.configure import (
ConfigureError,
ConfigureSandbox,
)
+from mozbuild.util import exec_
from buildconfig import topsrcdir
class FindProgramSandbox(ConfigureSandbox):
def __init__(self, *args, **kwargs):
super(FindProgramSandbox, self).__init__(*args, **kwargs)
@@ -53,17 +54,17 @@ class FindProgramSandbox(ConfigureSandbo
class TestChecksConfigure(unittest.TestCase):
def test_checking(self):
out = StringIO()
sandbox = FindProgramSandbox({}, stdout=out, stderr=out)
base_dir = os.path.join(topsrcdir, 'build', 'moz.configure')
sandbox.include_file(os.path.join(base_dir, 'checks.configure'))
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@checking('for a thing')
def foo(value):
return value
'''), sandbox)
foo = sandbox['foo']
foo(True)
@@ -83,17 +84,17 @@ class TestChecksConfigure(unittest.TestC
out.truncate(0)
data = ['foo', 'bar']
foo(data)
self.assertEqual(out.getvalue(), 'checking for a thing... %r\n' % data)
# When the function given to checking does nothing interesting, the
# behavior is not altered
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@checking('for a thing', lambda x: x)
def foo(value):
return value
'''), sandbox)
foo = sandbox['foo']
out.truncate(0)
@@ -112,17 +113,17 @@ class TestChecksConfigure(unittest.TestC
foo('foo')
self.assertEqual(out.getvalue(), 'checking for a thing... foo\n')
out.truncate(0)
data = ['foo', 'bar']
foo(data)
self.assertEqual(out.getvalue(), 'checking for a thing... %r\n' % data)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
def munge(x):
if not x:
return 'not found'
if isinstance(x, (str, bool, int)):
return x
return ' '.join(x)
@checking('for a thing', munge)
@@ -158,17 +159,17 @@ class TestChecksConfigure(unittest.TestC
out = StringIO()
sandbox = FindProgramSandbox(config, environ, [prog] + args, out, out)
base_dir = os.path.join(topsrcdir, 'build', 'moz.configure')
sandbox.include_file(os.path.join(base_dir, 'util.configure'))
sandbox.include_file(os.path.join(base_dir, 'checks.configure'))
status = 0
try:
- exec(command, sandbox)
+ exec_(command, sandbox)
sandbox.run()
except SystemExit as e:
status = e.code
return config, out.getvalue(), status
def test_check_prog(self):
config, out, status = self.get_result(
--- a/python/mozbuild/mozbuild/test/configure/test_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_configure.py
@@ -19,16 +19,17 @@ from mozbuild.configure.options import (
InvalidOptionError,
NegativeOptionValue,
PositiveOptionValue,
)
from mozbuild.configure import (
ConfigureError,
ConfigureSandbox,
)
+from mozbuild.util import exec_
import mozpack.path as mozpath
test_data_path = mozpath.abspath(mozpath.dirname(__file__))
test_data_path = mozpath.join(test_data_path, 'data')
class TestConfigure(unittest.TestCase):
@@ -239,100 +240,100 @@ class TestConfigure(unittest.TestCase):
self.assertNotIn('CFLAGS', config)
def test_imports(self):
config = {}
out = StringIO()
sandbox = ConfigureSandbox(config, {}, [], out, out)
with self.assertRaises(ImportError):
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
def foo():
import sys
foo()'''),
sandbox
)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports('sys')
def foo():
return sys'''),
sandbox
)
self.assertIs(sandbox['foo'](), sys)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports(_from='os', _import='path')
def foo():
return path'''),
sandbox
)
self.assertIs(sandbox['foo'](), os.path)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports(_from='os', _import='path', _as='os_path')
def foo():
return os_path'''),
sandbox
)
self.assertIs(sandbox['foo'](), os.path)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports('__builtin__')
def foo():
return __builtin__'''),
sandbox
)
import __builtin__
self.assertIs(sandbox['foo'](), __builtin__)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports(_from='__builtin__', _import='open')
def foo():
return open('%s')''' % os.devnull),
sandbox
)
f = sandbox['foo']()
self.assertEquals(f.name, os.devnull)
f.close()
# This unlocks the sandbox
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports(_import='__builtin__', _as='__builtins__')
def foo():
import sys
return sys'''),
sandbox
)
self.assertIs(sandbox['foo'](), sys)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports('__sandbox__')
def foo():
return __sandbox__'''),
sandbox
)
self.assertIs(sandbox['foo'](), sandbox)
- exec(textwrap.dedent('''
+ exec_(textwrap.dedent('''
@template
@imports(_import='__sandbox__', _as='s')
def foo():
return s'''),
sandbox
)
self.assertIs(sandbox['foo'](), sandbox)