Python inspect.getclosurevars() Examples
The following are 28
code examples of inspect.getclosurevars().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
inspect
, or try the search function
.
Example #1
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #2
Source File: __init__.py From incubator-tvm with Apache License 2.0 | 5 votes |
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)
Example #3
Source File: test_function.py From rpy2 with GNU General Public License v2.0 | 5 votes |
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
Example #4
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #5
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #6
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #7
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #8
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #9
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #10
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #11
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #12
Source File: condition_parser.py From CrossHair with MIT License | 5 votes |
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__
Example #13
Source File: module.py From ebonite with Apache License 2.0 | 5 votes |
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
Example #14
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #15
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #16
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #17
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #18
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #19
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #20
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #21
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 5 votes |
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)
Example #22
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #23
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #24
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #25
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #26
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #27
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #28
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)