Bug 1547730 - Use new inspection methods introduced in py3 but work in py2.7 for functions r=#build

Differential Revision: https://phabricator.services.mozilla.com/D28112

--HG--
extra : rebase_source : bf24427996ad79357c4cf6214f80ea1c3a39f124
extra : intermediate-source : b066211fcb63cf31ea74da0a841f38c1dfbf5676
extra : source : 591fc8f07d19a2f05565cf56f2dc9b3af526e298
This commit is contained in:
Justin Wood 2019-04-15 22:53:10 -04:00
Родитель 23eb9233a8
Коммит 8253fe8c06
2 изменённых файлов: 15 добавлений и 15 удалений

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

@ -1063,7 +1063,7 @@ class ConfigureSandbox(dict):
glob = SandboxedGlobal(
(k, v)
for k, v in six.iteritems(func.func_globals)
for k, v in six.iteritems(func.__globals__)
if (inspect.isfunction(v) and v not in self._templates) or (
inspect.isclass(v) and issubclass(v, Exception))
)
@ -1086,20 +1086,20 @@ class ConfigureSandbox(dict):
# Note this is not entirely bullet proof (if the value is e.g. a list,
# the list contents could have changed), but covers the bases.
closure = None
if func.func_closure:
if func.__closure__:
def makecell(content):
def f():
content
return f.func_closure[0]
return f.__closure__[0]
closure = tuple(makecell(cell.cell_contents)
for cell in func.func_closure)
for cell in func.__closure__)
new_func = self.wraps(func)(types.FunctionType(
func.func_code,
func.__code__,
glob,
func.__name__,
func.func_defaults,
func.__defaults__,
closure
))
@self.wraps(new_func)

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

@ -87,7 +87,7 @@ class LintSandbox(ConfigureSandbox):
# to line numbers relative to co_firstlineno.
# If the offset we need to encode is larger than 255, we need to split it.
co_lnotab = (chr(0) + chr(255)) * (offset / 255) + chr(0) + chr(offset % 255)
code = thrower.func_code
code = thrower.__code__
code = types.CodeType(
code.co_argcount,
code.co_nlocals,
@ -104,10 +104,10 @@ class LintSandbox(ConfigureSandbox):
)
thrower = types.FunctionType(
code,
thrower.func_globals,
thrower.__globals__,
funcname,
thrower.func_defaults,
thrower.func_closure
thrower.__defaults__,
thrower.__closure__
)
thrower(exception)
@ -154,7 +154,7 @@ class LintSandbox(ConfigureSandbox):
# - don't use @imports
# - don't have a closure
# - don't use global variables
if func in self._has_imports or func.func_closure:
if func in self._has_imports or func.__closure__:
return True
for op, arg, _ in disassemble_as_iter(func):
if op in ('LOAD_GLOBAL', 'STORE_GLOBAL'):
@ -269,10 +269,10 @@ class LintSandbox(ConfigureSandbox):
self._raise_from(e, frame.f_back if frame else None)
def unwrap(self, func):
glob = func.func_globals
glob = func.__globals__
while func in self._wrapped:
if isinstance(func.func_globals, SandboxedGlobal):
glob = func.func_globals
if isinstance(func.__globals__, SandboxedGlobal):
glob = func.__globals__
func = self._wrapped[func]
return func, glob
@ -301,7 +301,7 @@ class LintSandbox(ConfigureSandbox):
what = _import.split('.')[0]
imports.add(what)
for op, arg, line in disassemble_as_iter(func):
code = func.func_code
code = func.__code__
if op == 'LOAD_GLOBAL' and \
arg not in glob and \
arg not in imports and \