Bug 1264831 - Work around issues with the exec statement in older python 2.7 versions. r=gps

This commit is contained in:
Mike Hommey 2016-04-15 10:41:52 +09:00
Родитель 1cc2a86d6d
Коммит 1537683537
3 изменённых файлов: 26 добавлений и 12 удалений

Просмотреть файл

@ -28,10 +28,12 @@ from mozbuild.configure.util import (
LineIO, LineIO,
) )
from mozbuild.util import ( from mozbuild.util import (
exec_,
memoize, memoize,
ReadOnlyDict, ReadOnlyDict,
ReadOnlyNamespace, ReadOnlyNamespace,
) )
import mozpack.path as mozpath import mozpack.path as mozpath
@ -200,7 +202,7 @@ class ConfigureSandbox(dict):
code = compile(source, path, 'exec') code = compile(source, path, 'exec')
exec(code, self) exec_(code, self)
self._paths.pop(-1) self._paths.pop(-1)
@ -570,10 +572,7 @@ class ConfigureSandbox(dict):
import_line += 'import %s' % _import import_line += 'import %s' % _import
if _as: if _as:
import_line += ' as %s' % _as import_line += ' as %s' % _as
# Some versions of python fail with "SyntaxError: unqualified exec exec_(import_line, {}, glob)
# 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
def _resolve_and_set(self, data, name, value): def _resolve_and_set(self, data, name, value):
# Don't set anything when --help was on the command line # Don't set anything when --help was on the command line

Просмотреть файл

@ -23,7 +23,10 @@ import os
import sys import sys
import weakref import weakref
from mozbuild.util import ReadOnlyDict from mozbuild.util import (
exec_,
ReadOnlyDict,
)
from .context import Context from .context import Context
from mozpack.files import FileFinder from mozpack.files import FileFinder
@ -174,11 +177,7 @@ class Sandbox(dict):
old_source = self._current_source old_source = self._current_source
self._current_source = source self._current_source = source
try: try:
# Ideally, we'd use exec(code, self), but that yield the exec_(code, self)
# following error:
# SyntaxError: unqualified exec is not allowed in function
# 'execute' it is a nested function.
exec code in self
finally: finally:
self._current_source = old_source self._current_source = old_source

Просмотреть файл

@ -5,7 +5,7 @@
# This file contains miscellaneous utility functions that don't belong anywhere # This file contains miscellaneous utility functions that don't belong anywhere
# in particular. # in particular.
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals, print_function
import argparse import argparse
import collections import collections
@ -42,6 +42,22 @@ if sys.platform == 'win32':
_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000 _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): def hash_file(path, hasher=None):
"""Hashes a file specified by the path given and returns the hex digest.""" """Hashes a file specified by the path given and returns the hex digest."""