本文整理匯總了Python中inspect.getclosurevars方法的典型用法代碼示例。如果您正苦於以下問題:Python inspect.getclosurevars方法的具體用法?Python inspect.getclosurevars怎麽用?Python inspect.getclosurevars使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類inspect
的用法示例。
在下文中一共展示了inspect.getclosurevars方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_name_resolution
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_name_resolution(self):
# Basic test of the 4 different resolution mechanisms
def f(nonlocal_ref):
def g(local_ref):
print(local_ref, nonlocal_ref, _global_ref, unbound_ref)
return g
_arg = object()
nonlocal_vars = {"nonlocal_ref": _arg}
global_vars = {"_global_ref": _global_ref}
builtin_vars = {"print": print}
unbound_names = {"unbound_ref"}
expected = inspect.ClosureVars(nonlocal_vars, global_vars,
builtin_vars, unbound_names)
self.assertEqual(inspect.getclosurevars(f(_arg)), expected)
示例2: test_generator_closure
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_generator_closure(self):
def f(nonlocal_ref):
def g(local_ref):
print(local_ref, nonlocal_ref, _global_ref, unbound_ref)
yield
return g
_arg = object()
nonlocal_vars = {"nonlocal_ref": _arg}
global_vars = {"_global_ref": _global_ref}
builtin_vars = {"print": print}
unbound_names = {"unbound_ref"}
expected = inspect.ClosureVars(nonlocal_vars, global_vars,
builtin_vars, unbound_names)
self.assertEqual(inspect.getclosurevars(f(_arg)), expected)
示例3: test_method_closure
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_method_closure(self):
class C:
def f(self, nonlocal_ref):
def g(local_ref):
print(local_ref, nonlocal_ref, _global_ref, unbound_ref)
return g
_arg = object()
nonlocal_vars = {"nonlocal_ref": _arg}
global_vars = {"_global_ref": _global_ref}
builtin_vars = {"print": print}
unbound_names = {"unbound_ref"}
expected = inspect.ClosureVars(nonlocal_vars, global_vars,
builtin_vars, unbound_names)
self.assertEqual(inspect.getclosurevars(C().f(_arg)), expected)
示例4: test_nonlocal_vars
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_nonlocal_vars(self):
# More complex tests of nonlocal resolution
def _nonlocal_vars(f):
return inspect.getclosurevars(f).nonlocals
def make_adder(x):
def add(y):
return x + y
return add
def curry(func, arg1):
return lambda arg2: func(arg1, arg2)
def less_than(a, b):
return a < b
# The infamous Y combinator.
def Y(le):
def g(f):
return le(lambda x: f(f)(x))
Y.g_ref = g
return g(g)
def check_y_combinator(func):
self.assertEqual(_nonlocal_vars(func), {'f': Y.g_ref})
inc = make_adder(1)
add_two = make_adder(2)
greater_than_five = curry(less_than, 5)
self.assertEqual(_nonlocal_vars(inc), {'x': 1})
self.assertEqual(_nonlocal_vars(add_two), {'x': 2})
self.assertEqual(_nonlocal_vars(greater_than_five),
{'arg1': 5, 'func': less_than})
self.assertEqual(_nonlocal_vars((lambda x: lambda y: x + y)(3)),
{'x': 3})
Y(check_y_combinator)
示例5: test_getclosurevars_empty
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_getclosurevars_empty(self):
def foo(): pass
_empty = inspect.ClosureVars({}, {}, {}, set())
self.assertEqual(inspect.getclosurevars(lambda: True), _empty)
self.assertEqual(inspect.getclosurevars(foo), _empty)
示例6: test_builtins_fallback
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_builtins_fallback(self):
f, ns = self._private_globals()
ns.pop("__builtins__", None)
expected = inspect.ClosureVars({}, {}, {"print":print}, {"path"})
self.assertEqual(inspect.getclosurevars(f), expected)
示例7: test_builtins_as_dict
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_builtins_as_dict(self):
f, ns = self._private_globals()
ns["__builtins__"] = {"path":1}
expected = inspect.ClosureVars({}, {}, {"path":1}, {"print"})
self.assertEqual(inspect.getclosurevars(f), expected)
示例8: test_builtins_as_module
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_builtins_as_module(self):
f, ns = self._private_globals()
ns["__builtins__"] = os
expected = inspect.ClosureVars({}, {}, {"path":os.path}, {"print"})
self.assertEqual(inspect.getclosurevars(f), expected)
示例9: add_closure_inspection
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def add_closure_inspection(f):
@wraps(f)
def wrapper(pickler: '_EboniteRequirementAnalyzer', obj):
closure = inspect.getclosurevars(obj)
for field in ['nonlocals', 'globals']:
for o in getattr(closure, field).values():
if isinstance(o, ModuleType):
pickler._add_requirement(o)
else:
pickler.save(o)
if is_from_installable_module(obj):
return f(pickler, obj)
# to add from local imports inside user (non PIP package) code
tree = ast.parse(inspect.getsource(obj).strip())
class ImportFromVisitor(ast.NodeVisitor):
def visit_ImportFrom(self, node: ast.ImportFrom): # noqa
warnings.warn(f'Detected local import in {obj.__module__}.{obj.__name__}')
if node.level == 0:
mod = import_module(node.module)
else:
mod = import_module('.' + node.module, get_object_module(obj).__package__)
pickler._add_requirement(mod)
ImportFromVisitor().visit(tree)
return f(pickler, obj)
return wrapper
示例10: fn_globals
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def fn_globals(fn: Callable) -> Dict[str, object]:
if hasattr(fn, '__wrapped__'):
return fn_globals(fn.__wrapped__) # type: ignore
if inspect.isfunction(fn): # excludes built-ins, which don't have closurevars
closure_vars = inspect.getclosurevars(fn)
if closure_vars.nonlocals:
return {**closure_vars.nonlocals, **closure_vars.globals}
if hasattr(fn, '__globals__'):
return fn.__globals__ # type:ignore
return builtins.__dict__
示例11: test_wrap_r_function
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def test_wrap_r_function(is_method):
r_code = 'function(x, y=FALSE, z="abc") TRUE'
parameter_names = ('self', 'x', 'y', 'z') if is_method else ('x', 'y', 'z')
r_func = robjects.r(r_code)
foo = robjects.functions.wrap_r_function(r_func, 'foo',
is_method=is_method)
assert inspect.getclosurevars(foo).nonlocals['r_func'].rid == r_func.rid
assert tuple(foo.__signature__.parameters.keys()) == parameter_names
if not is_method:
res = foo(1)
assert res[0] is True
示例12: script
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import getclosurevars [as 別名]
def script(pyfunc):
"""Decorate a python function function as hybrid script.
The hybrid function support emulation mode and parsing to
the internal language IR.
Returns
-------
hybrid_func : function
A decorated hybrid script function.
"""
# pylint: disable=import-outside-toplevel, missing-docstring
def wrapped_func(func, *args, **kwargs):
from .util import _is_tvm_arg_types
if _is_tvm_arg_types(args):
src = _pruned_source(func)
closure_vars = inspect.getclosurevars(func).nonlocals
closure_vars.update(inspect.getclosurevars(func).globals)
return source_to_op(src, args, func.__globals__, closure_vars)
from .runtime import _enter_hybrid_runtime, _restore_runtime
intersect = _enter_hybrid_runtime(func)
value = func(*args, **kwargs)
_restore_runtime(func, intersect)
return value
return decorate(pyfunc, wrapped_func)