зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1266343 - Change ConfigureSandbox._apply_imports such that it becomes easy to override imports in unit tests. r=chmanchester
This commit is contained in:
Родитель
24ce400759
Коммит
a3cf230161
|
@ -533,6 +533,8 @@ class ConfigureSandbox(dict):
|
||||||
raise TypeError("Unexpected type: '%s'" % type(value).__name__)
|
raise TypeError("Unexpected type: '%s'" % type(value).__name__)
|
||||||
if value is not None and not self.RE_MODULE.match(value):
|
if value is not None and not self.RE_MODULE.match(value):
|
||||||
raise ValueError("Invalid argument to @imports: '%s'" % value)
|
raise ValueError("Invalid argument to @imports: '%s'" % value)
|
||||||
|
if _as and '.' in _as:
|
||||||
|
raise ValueError("Invalid argument to @imports: '%s'" % _as)
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
if func in self._templates:
|
if func in self._templates:
|
||||||
|
@ -552,27 +554,33 @@ class ConfigureSandbox(dict):
|
||||||
|
|
||||||
def _apply_imports(self, func, glob):
|
def _apply_imports(self, func, glob):
|
||||||
for _from, _import, _as in self._imports.get(func, ()):
|
for _from, _import, _as in self._imports.get(func, ()):
|
||||||
|
_from = '%s.' % _from if _from else ''
|
||||||
|
if _as:
|
||||||
|
glob[_as] = self._get_one_import('%s%s' % (_from, _import))
|
||||||
|
else:
|
||||||
|
what = _import.split('.')[0]
|
||||||
|
glob[what] = self._get_one_import('%s%s' % (_from, what))
|
||||||
|
|
||||||
|
def _get_one_import(self, what):
|
||||||
# The special `__sandbox__` module gives access to the sandbox
|
# The special `__sandbox__` module gives access to the sandbox
|
||||||
# instance.
|
# instance.
|
||||||
if _from is None and _import == '__sandbox__':
|
if what == '__sandbox__':
|
||||||
glob[_as or _import] = self
|
return self
|
||||||
continue
|
|
||||||
# Special case for the open() builtin, because otherwise, using it
|
# Special case for the open() builtin, because otherwise, using it
|
||||||
# fails with "IOError: file() constructor not accessible in
|
# fails with "IOError: file() constructor not accessible in
|
||||||
# restricted mode"
|
# restricted mode"
|
||||||
if _from == '__builtin__' and _import == 'open':
|
if what == '__builtin__.open':
|
||||||
glob[_as or _import] = \
|
return lambda *args, **kwargs: open(*args, **kwargs)
|
||||||
lambda *args, **kwargs: open(*args, **kwargs)
|
|
||||||
continue
|
|
||||||
# Until this proves to be a performance problem, just construct an
|
# Until this proves to be a performance problem, just construct an
|
||||||
# import statement and execute it.
|
# import statement and execute it.
|
||||||
import_line = ''
|
import_line = ''
|
||||||
if _from:
|
if '.' in what:
|
||||||
|
_from, what = what.rsplit('.', 1)
|
||||||
import_line += 'from %s ' % _from
|
import_line += 'from %s ' % _from
|
||||||
import_line += 'import %s' % _import
|
import_line += 'import %s as imported' % what
|
||||||
if _as:
|
glob = {}
|
||||||
import_line += ' as %s' % _as
|
|
||||||
exec_(import_line, {}, glob)
|
exec_(import_line, {}, glob)
|
||||||
|
return glob['imported']
|
||||||
|
|
||||||
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
|
||||||
|
|
Загрузка…
Ссылка в новой задаче