From 15376835372fd7116f87a79103e7acc26672f395 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 15 Apr 2016 10:41:52 +0900 Subject: [PATCH] Bug 1264831 - Work around issues with the exec statement in older python 2.7 versions. r=gps --- python/mozbuild/mozbuild/configure/__init__.py | 9 ++++----- python/mozbuild/mozbuild/frontend/sandbox.py | 11 +++++------ python/mozbuild/mozbuild/util.py | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/python/mozbuild/mozbuild/configure/__init__.py b/python/mozbuild/mozbuild/configure/__init__.py index c1362079130d..c81a8d632423 100644 --- a/python/mozbuild/mozbuild/configure/__init__.py +++ b/python/mozbuild/mozbuild/configure/__init__.py @@ -28,10 +28,12 @@ from mozbuild.configure.util import ( LineIO, ) from mozbuild.util import ( + exec_, memoize, ReadOnlyDict, ReadOnlyNamespace, ) + import mozpack.path as mozpath @@ -200,7 +202,7 @@ class ConfigureSandbox(dict): code = compile(source, path, 'exec') - exec(code, self) + exec_(code, self) self._paths.pop(-1) @@ -570,10 +572,7 @@ class ConfigureSandbox(dict): import_line += 'import %s' % _import if _as: import_line += ' as %s' % _as - # Some versions of python fail with "SyntaxError: unqualified exec - # is not allowed in function '_apply_imports' it contains a nested - # function with free variable" when using the exec function. - exec import_line in {}, glob + exec_(import_line, {}, glob) def _resolve_and_set(self, data, name, value): # Don't set anything when --help was on the command line diff --git a/python/mozbuild/mozbuild/frontend/sandbox.py b/python/mozbuild/mozbuild/frontend/sandbox.py index 9f2666f8c51b..0bf1599f2084 100644 --- a/python/mozbuild/mozbuild/frontend/sandbox.py +++ b/python/mozbuild/mozbuild/frontend/sandbox.py @@ -23,7 +23,10 @@ import os import sys import weakref -from mozbuild.util import ReadOnlyDict +from mozbuild.util import ( + exec_, + ReadOnlyDict, +) from .context import Context from mozpack.files import FileFinder @@ -174,11 +177,7 @@ class Sandbox(dict): old_source = self._current_source self._current_source = source try: - # Ideally, we'd use exec(code, self), but that yield the - # following error: - # SyntaxError: unqualified exec is not allowed in function - # 'execute' it is a nested function. - exec code in self + exec_(code, self) finally: self._current_source = old_source diff --git a/python/mozbuild/mozbuild/util.py b/python/mozbuild/mozbuild/util.py index a1b70268334c..e7caee0adaa2 100644 --- a/python/mozbuild/mozbuild/util.py +++ b/python/mozbuild/mozbuild/util.py @@ -5,7 +5,7 @@ # This file contains miscellaneous utility functions that don't belong anywhere # in particular. -from __future__ import absolute_import, unicode_literals +from __future__ import absolute_import, unicode_literals, print_function import argparse import collections @@ -42,6 +42,22 @@ if sys.platform == 'win32': _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000 +def exec_(object, globals=None, locals=None): + """Wrapper around the exec statement to avoid bogus errors like: + + SyntaxError: unqualified exec is not allowed in function ... + it is a nested function. + + or + + SyntaxError: unqualified exec is not allowed in function ... + it contains a nested function with free variable + + which happen with older versions of python 2.7. + """ + exec(object, globals, locals) + + def hash_file(path, hasher=None): """Hashes a file specified by the path given and returns the hex digest."""