Python six.get_function_closure() Examples
The following are 11
code examples of six.get_function_closure().
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
six
, or try the search function
.
Example #1
Source File: utils.py From py2swagger with MIT License | 5 votes |
def get_decorators(function): # If we have no func_closure, it means we are not wrapping any other functions. decorators = [] try: func_closure = six.get_function_closure(function) except AttributeError: return decorators if not func_closure: return [function] # Otherwise, we want to collect all of the recursive results for every closure we have. for closure in func_closure: if isinstance(closure.cell_contents, types.FunctionType): decorators.extend(get_decorators(closure.cell_contents)) return [function] + decorators
Example #2
Source File: annotate.py From tangent with Apache License 2.0 | 5 votes |
def __init__(self, func): self.func = func self.namespace = six.get_function_globals(func) if six.get_function_closure(func): self.namespace.update(dict(zip( func.__code__.co_freevars, (cell.cell_contents for cell in six.get_function_closure(func)))))
Example #3
Source File: injector.py From addon with GNU General Public License v3.0 | 5 votes |
def fix_js_args(func): '''Use this function when unsure whether func takes this and arguments as its last 2 args. It will append 2 args if it does not.''' fcode = six.get_function_code(func) fargs = fcode.co_varnames[fcode.co_argcount - 2:fcode.co_argcount] if fargs == ('this', 'arguments') or fargs == ('arguments', 'var'): return func code = append_arguments(six.get_function_code(func), ('this', 'arguments')) return types.FunctionType( code, six.get_function_globals(func), func.__name__, closure=six.get_function_closure(func))
Example #4
Source File: injector.py From xbmc-addons-chinese with GNU General Public License v2.0 | 5 votes |
def fix_js_args(func): '''Use this function when unsure whether func takes this and arguments as its last 2 args. It will append 2 args if it does not.''' fcode = six.get_function_code(func) fargs = fcode.co_varnames[fcode.co_argcount-2:fcode.co_argcount] if fargs==('this', 'arguments') or fargs==('arguments', 'var'): return func code = append_arguments(six.get_function_code(func), ('this','arguments')) return types.FunctionType(code, six.get_function_globals(func), func.__name__, closure=six.get_function_closure(func))
Example #5
Source File: test_six.py From six with MIT License | 5 votes |
def test_get_function_closure(): def f(): x = 42 def g(): return x return g cell = six.get_function_closure(f())[0] assert type(cell).__name__ == "cell"
Example #6
Source File: utils.py From barbican with Apache License 2.0 | 5 votes |
def construct_new_test_function(original_func, name, build_params): """Builds a new test function based on parameterized data. :param original_func: The original test function that is used as a template :param name: The fullname of the new test function :param build_params: A dictionary or list containing args or kwargs for the new test :return: A new function object """ new_func = types.FunctionType( six.get_function_code(original_func), six.get_function_globals(original_func), name=name, argdefs=six.get_function_defaults(original_func), closure=six.get_function_closure(original_func) ) for key, val in original_func.__dict__.items(): if key != 'build_data': new_func.__dict__[key] = val # Support either an arg list or kwarg dict for our data build_args = build_params if isinstance(build_params, list) else [] build_kwargs = build_params if isinstance(build_params, dict) else {} # Build a test wrapper to execute with our kwargs def test_wrapper(func, test_args, test_kwargs): @functools.wraps(func) def wrapper(self): return func(self, *test_args, **test_kwargs) return wrapper return test_wrapper(new_func, build_args, build_kwargs)
Example #7
Source File: test_six.py From c4ddev with MIT License | 5 votes |
def test_get_function_closure(): def f(): x = 42 def g(): return x return g cell = six.get_function_closure(f())[0] assert type(cell).__name__ == "cell"
Example #8
Source File: injector.py From bazarr with GNU General Public License v3.0 | 5 votes |
def fix_js_args(func): '''Use this function when unsure whether func takes this and arguments as its last 2 args. It will append 2 args if it does not.''' fcode = six.get_function_code(func) fargs = fcode.co_varnames[fcode.co_argcount - 2:fcode.co_argcount] if fargs == ('this', 'arguments') or fargs == ('arguments', 'var'): return func code = append_arguments(six.get_function_code(func), ('this', 'arguments')) return types.FunctionType( code, six.get_function_globals(func), func.__name__, closure=six.get_function_closure(func))
Example #9
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
def test_get_function_closure(): def f(): x = 42 def g(): return x return g cell = six.get_function_closure(f())[0] assert type(cell).__name__ == "cell"
Example #10
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
def test_get_function_closure(): def f(): x = 42 def g(): return x return g cell = six.get_function_closure(f())[0] assert type(cell).__name__ == "cell"
Example #11
Source File: util.py From pecan with BSD 3-Clause "New" or "Revised" License | 4 votes |
def getargspec(method): """ Drill through layers of decorators attempting to locate the actual argspec for a method. """ argspec = _getargspec(method) args = argspec[0] if args and args[0] == 'self': return argspec if hasattr(method, '__func__'): method = method.__func__ func_closure = six.get_function_closure(method) # NOTE(sileht): if the closure is None we cannot look deeper, # so return actual argspec, this occurs when the method # is static for example. if not func_closure: return argspec closure = None # In the case of deeply nested decorators (with arguments), it's possible # that there are several callables in scope; Take a best guess and go # with the one that looks most like a pecan controller function # (has a __code__ object, and 'self' is the first argument) func_closure = filter( lambda c: ( six.callable(c.cell_contents) and hasattr(c.cell_contents, '__code__') ), func_closure ) func_closure = sorted( func_closure, key=lambda c: 'self' in c.cell_contents.__code__.co_varnames, reverse=True ) closure = func_closure[0] method = closure.cell_contents return getargspec(method)