Python six 模块,get_function_code() 实例源码

我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用six.get_function_code()

项目:django-ical    作者:pinkerton    | 项目源码 | 文件源码
def _get_dynamic_attr(self, attname, obj, default=None):
        """
        Copied from django.contrib.syndication.views.Feed (v1.7.1)
        """
        try:
            attr = getattr(self, attname)
        except AttributeError:
            return default
        if callable(attr):
            # Check co_argcount rather than try/excepting the function and
            # catching the TypeError, because something inside the function
            # may raise the TypeError. This technique is more accurate.
            try:
                code = six.get_function_code(attr)
            except AttributeError:
                code = six.get_function_code(attr.__call__)
            if code.co_argcount == 2:       # one argument is 'self'
                return attr(obj)
            else:
                return attr()
        return attr

    # NOTE: Not used by icalendar but required
    #       by the Django syndication framework.
项目:cleaREST    作者:petr-s    | 项目源码 | 文件源码
def parse_args(args, path, query, specials):
    def one_or_many(fn_, dict_, key):
        result = [fn_(value) for value in dict_[key]]
        return result[0] if len(result) == 1 else result

    kwargs = {}
    for arg, parse_fn in six.iteritems(args):
        if arg in specials:
            kwargs[arg] = specials[arg]()
        elif parse_fn is None:
            kwargs[arg] = one_or_many(lambda x: x, query, arg)
        elif isinstance(parse_fn, tuple):
            kwargs[arg] = parse_fn[DEFAULT] if arg not in query else one_or_many(parse_fn[CALLABLE], query, arg)
        elif isalambda(parse_fn):
            _code = six.get_function_code(parse_fn)
            closures = six.get_function_closure(parse_fn)
            if closures:
                assert len(closures) <= 1
                fn = closures[0].cell_contents
            else:
                fn = eval(".".join(_code.co_names), six.get_function_globals(parse_fn))
            kwargs[arg] = fn(**parse_args(get_function_args(parse_fn), path, query, specials))
        else:
            kwargs[arg] = one_or_many(parse_fn, query, arg)
    return kwargs
项目:stepler    作者:Mirantis    | 项目源码 | 文件源码
def get_unwrapped_func(func):
    """Get original function under decorator.

    Decorator hides original function inside itself. But in some cases it's
    important to get access to original function, for ex: for documentation.

    Args:
        func (function): function that can be potentially a decorator which
            hides original function

    Returns:
        function: unwrapped function or the same function
    """
    if not inspect.isfunction(func) and not inspect.ismethod(func):
        return func

    if func.__name__ != six.get_function_code(func).co_name:
        for cell in six.get_function_closure(func):
            obj = cell.cell_contents
            if inspect.isfunction(obj):
                if func.__name__ == six.get_function_code(obj).co_name:
                    return obj
                else:
                    return get_unwrapped_func(obj)
    return func
项目:tvlinker    作者:ozmartian    | 项目源码 | 文件源码
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))
项目:tvlinker    作者:ozmartian    | 项目源码 | 文件源码
def __init__(self, func, prototype=None, extensible=True, source=None):
        cand = fix_js_args(func)
        has_scope = cand is func
        func = cand
        self.argcount = six.get_function_code(func).co_argcount - 2 - has_scope
        self.code = func
        self.source = source if source else '{ [python code] }'
        self.func_name = func.__name__ if not func.__name__.startswith('PyJs_anonymous') else ''
        self.extensible = extensible
        self.prototype = prototype
        self.own = {}
        #set own property length to the number of arguments
        self.define_own_property('length', {'value': Js(self.argcount), 'writable': False,
                                            'enumerable': False, 'configurable': False})

        if self.func_name:
            self.define_own_property('name', {'value': Js(self.func_name), 'writable': False,
                                            'enumerable': False, 'configurable': True})

        # set own prototype
        proto = Js({})
        # constructor points to this function
        proto.define_own_property('constructor',{'value': self, 'writable': True,
                                                 'enumerable': False, 'configurable': True})
        self.define_own_property('prototype', {'value': proto, 'writable': True,
                                                 'enumerable': False, 'configurable': False})
项目:obsoleted-vpduserv    作者:InfraSIM    | 项目源码 | 文件源码
def test_get_function_code():
    def f():
        pass
    assert isinstance(six.get_function_code(f), types.CodeType)
    if not hasattr(sys, "pypy_version_info"):
        py.test.raises(AttributeError, six.get_function_code, hasattr)
项目:MaruMaru.bundle    作者:hojel    | 项目源码 | 文件源码
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))
项目:MaruMaru.bundle    作者:hojel    | 项目源码 | 文件源码
def __init__(self, func, prototype=None, extensible=True, source=None):
        cand = fix_js_args(func)
        has_scope = cand is func
        func = cand
        self.argcount = six.get_function_code(func).co_argcount - 2 - has_scope
        self.code = func
        self.source = source if source else '{ [python code] }'
        self.func_name = func.__name__ if not func.__name__.startswith('PyJs_anonymous') else ''
        self.extensible = extensible
        self.prototype = prototype
        self.own = {}
        #set own property length to the number of arguments
        self.define_own_property('length', {'value': Js(self.argcount), 'writable': False,
                                            'enumerable': False, 'configurable': False})

        if self.func_name:
            self.define_own_property('name', {'value': Js(self.func_name), 'writable': False,
                                            'enumerable': False, 'configurable': True})

        # set own prototype
        proto = Js({})
        # constructor points to this function
        proto.define_own_property('constructor',{'value': self, 'writable': True,
                                                 'enumerable': False, 'configurable': True})
        self.define_own_property('prototype', {'value': proto, 'writable': True,
                                                 'enumerable': False, 'configurable': False})
项目:Deploy_XXNET_Server    作者:jzp820927    | 项目源码 | 文件源码
def test_get_function_code():
    def f():
        pass
    assert isinstance(six.get_function_code(f), types.CodeType)
    if not hasattr(sys, "pypy_version_info"):
        py.test.raises(AttributeError, six.get_function_code, hasattr)
项目:pure_interface    作者:aranzgeo    | 项目源码 | 文件源码
def _is_empty_function(func, unwrap=False):
    """ Return True if func is considered empty.
     All functions with no return statement have an implicit return None - this is explicit in the code object.
    """
    if isinstance(func, (staticmethod, classmethod, types.MethodType)):
        func = six.get_method_function(func)
    if isinstance(func, property):
        func = property.fget
    if unwrap:
        func = _unwrap_function(func)
    try:
        code_obj = six.get_function_code(func)
    except AttributeError:
        # This callable is something else - assume it is OK.
        return True

    # quick check
    if code_obj.co_code == b'd\x00\x00S' and code_obj.co_consts[0] is None:
        return True
    if code_obj.co_code == b'd\x01\x00S' and code_obj.co_consts[1] is None:
        return True
    # convert bytes to instructions
    instructions = _get_instructions(code_obj)
    if len(instructions) < 2:
        return True  # this never happens as there is always the implicit return None which is 2 instructions
    assert instructions[-1].opname == 'RETURN_VALUE'  # returns TOS (top of stack)
    instruction = instructions[-2]
    if not (instruction.opname == 'LOAD_CONST' and code_obj.co_consts[instruction.arg] is None):  # TOS is None
        return False  # return is not None
    instructions = instructions[:-2]
    if len(instructions) == 0:
        return True
    # look for raise NotImplementedError
    if instructions[-1].opname == 'RAISE_VARARGS':
        # the thing we are raising should be the result of __call__  (instantiating exception object)
        if instructions[-2].opname == 'CALL_FUNCTION':
            for instr in instructions[:-2]:
                if instr.opname == 'LOAD_GLOBAL' and code_obj.co_names[instr.arg] == 'NotImplementedError':
                    return True

    return False
项目:FMoviesPlus.bundle    作者:coder-alpha    | 项目源码 | 文件源码
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))
项目:FMoviesPlus.bundle    作者:coder-alpha    | 项目源码 | 文件源码
def __init__(self, func, prototype=None, extensible=True, source=None):
        cand = fix_js_args(func)
        has_scope = cand is func
        func = cand
        self.argcount = six.get_function_code(func).co_argcount - 2 - has_scope
        self.code = func
        self.source = source if source else '{ [python code] }'
        self.func_name = func.__name__ if not func.__name__.startswith('PyJs_anonymous') else ''
        self.extensible = extensible
        self.prototype = prototype
        self.own = {}
        #set own property length to the number of arguments
        self.define_own_property('length', {'value': Js(self.argcount), 'writable': False,
                                            'enumerable': False, 'configurable': False})

        if self.func_name:
            self.define_own_property('name', {'value': Js(self.func_name), 'writable': False,
                                            'enumerable': False, 'configurable': True})

        # set own prototype
        proto = Js({})
        # constructor points to this function
        proto.define_own_property('constructor',{'value': self, 'writable': True,
                                                 'enumerable': False, 'configurable': True})
        self.define_own_property('prototype', {'value': proto, 'writable': True,
                                                 'enumerable': False, 'configurable': False})
项目:plugin.video.9anime    作者:DxCx    | 项目源码 | 文件源码
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))
项目:plugin.video.9anime    作者:DxCx    | 项目源码 | 文件源码
def __init__(self, func, prototype=None, extensible=True, source=None):
        cand = fix_js_args(func)
        has_scope = cand is func
        func = cand
        self.argcount = six.get_function_code(func).co_argcount - 2 - has_scope
        self.code = func
        self.source = source if source else '{ [python code] }'
        self.func_name = func.__name__ if not func.__name__.startswith('PyJs_anonymous') else ''
        self.extensible = extensible
        self.prototype = prototype
        self.own = {}
        #set own property length to the number of arguments
        self.define_own_property('length', {'value': Js(self.argcount), 'writable': False,
                                            'enumerable': False, 'configurable': False})

        if self.func_name:
            self.define_own_property('name', {'value': Js(self.func_name), 'writable': False,
                                            'enumerable': False, 'configurable': True})

        # set own prototype
        proto = Js({})
        # constructor points to this function
        proto.define_own_property('constructor',{'value': self, 'writable': True,
                                                 'enumerable': False, 'configurable': True})
        self.define_own_property('prototype', {'value': proto, 'writable': True,
                                                 'enumerable': False, 'configurable': False})
项目:six    作者:benjaminp    | 项目源码 | 文件源码
def test_get_function_code():
    def f():
        pass
    assert isinstance(six.get_function_code(f), types.CodeType)
    if not hasattr(sys, "pypy_version_info"):
        py.test.raises(AttributeError, six.get_function_code, hasattr)
项目:blender    作者:gastrodia    | 项目源码 | 文件源码
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))
项目:blender    作者:gastrodia    | 项目源码 | 文件源码
def __init__(self, func, prototype=None, extensible=True, source=None):
        cand = fix_js_args(func)
        has_scope = cand is func
        func = cand
        self.argcount = six.get_function_code(func).co_argcount - 2 - has_scope
        self.code = func
        self.source = source if source else '{ [python code] }'
        self.func_name = func.__name__ if not func.__name__.startswith('PyJs_anonymous') else ''
        self.extensible = extensible
        self.prototype = prototype
        self.own = {}
        #set own property length to the number of arguments
        self.define_own_property('length', {'value': Js(self.argcount), 'writable': False,
                                            'enumerable': False, 'configurable': False})

        if self.func_name:
            self.define_own_property('name', {'value': Js(self.func_name), 'writable': False,
                                            'enumerable': False, 'configurable': True})

        # set own prototype
        proto = Js({})
        # constructor points to this function
        proto.define_own_property('constructor',{'value': self, 'writable': True,
                                                 'enumerable': False, 'configurable': True})
        self.define_own_property('prototype', {'value': proto, 'writable': True,
                                                 'enumerable': False, 'configurable': False})
项目:wuye.vim    作者:zhaoyingnan911    | 项目源码 | 文件源码
def object_build_function(node, member, localname):
    """create astroid for a living function object"""
    args, varargs, varkw, defaults = getargspec(member)
    if varargs is not None:
        args.append(varargs)
    if varkw is not None:
        args.append(varkw)
    func = build_function(getattr(member, '__name__', None) or localname, args,
                          defaults, six.get_function_code(member).co_flags, member.__doc__)
    node.add_local_node(func, localname)
项目:python-alp    作者:tboquet    | 项目源码 | 文件源码
def serialize(cust_obj):
    """A function to serialize custom objects passed to a model

    Args:
        cust_obj(callable): a custom layer or function to serialize

    Returns:
        a dict of the serialized components of the object"""
    ser_func = dict()
    if isinstance(cust_obj, types.FunctionType):

        func_code = six.get_function_code(cust_obj)
        func_code_d = dill.dumps(func_code).decode('raw_unicode_escape')
        ser_func['func_code_d'] = func_code_d
        ser_func['name_d'] = pickle.dumps(
            cust_obj.__name__).decode('raw_unicode_escape')
        ser_func['args_d'] = pickle.dumps(
            six.get_function_defaults(cust_obj)).decode('raw_unicode_escape')
        clos = dill.dumps(
            six.get_function_closure(cust_obj)).decode('raw_unicode_escape')
        ser_func['clos_d'] = clos
        ser_func['type_obj'] = 'func'
    else:
        if hasattr(cust_obj, '__module__'):  # pragma: no cover
            cust_obj.__module__ = '__main__'
        ser_func['name_d'] = None
        ser_func['args_d'] = None
        ser_func['clos_d'] = None
        ser_func['type_obj'] = 'class'
        loaded = dill.dumps(cust_obj).decode('raw_unicode_escape')
        ser_func['func_code_d'] = loaded
    return ser_func
项目:Chromium_DepotTools    作者:p07r0457    | 项目源码 | 文件源码
def object_build(self, node, obj):
        """recursive method which create a partial ast from real objects
         (only function, class, and method are handled)
        """
        if obj in self._done:
            return self._done[obj]
        self._done[obj] = node
        for name in dir(obj):
            try:
                member = getattr(obj, name)
            except AttributeError:
                # damned ExtensionClass.Base, I know you're there !
                attach_dummy_node(node, name)
                continue
            if ismethod(member):
                member = six.get_method_function(member)
            if isfunction(member):
                # verify this is not an imported function
                filename = getattr(six.get_function_code(member),
                                   'co_filename', None)
                if filename is None:
                    assert isinstance(member, object)
                    object_build_methoddescriptor(node, member, name)
                elif filename != getattr(self._module, '__file__', None):
                    attach_dummy_node(node, name, member)
                else:
                    object_build_function(node, member, name)
            elif isbuiltin(member):
                if (not _io_discrepancy(member) and
                        self.imported_member(node, member, name)):
                    continue
                object_build_methoddescriptor(node, member, name)
            elif isclass(member):
                if self.imported_member(node, member, name):
                    continue
                if member in self._done:
                    class_node = self._done[member]
                    if not class_node in node.locals.get(name, ()):
                        node.add_local_node(class_node, name)
                else:
                    class_node = object_build_class(node, member, name)
                    # recursion
                    self.object_build(class_node, member)
                if name == '__class__' and class_node.parent is None:
                    class_node.parent = self._done[self._module]
            elif ismethoddescriptor(member):
                assert isinstance(member, object)
                object_build_methoddescriptor(node, member, name)
            elif isdatadescriptor(member):
                assert isinstance(member, object)
                object_build_datadescriptor(node, member, name)
            elif type(member) in _CONSTANTS:
                attach_const_node(node, name, member)
            else:
                # create an empty node so that the name is actually defined
                attach_dummy_node(node, name, member)
项目:node-gn    作者:Shouqun    | 项目源码 | 文件源码
def object_build(self, node, obj):
        """recursive method which create a partial ast from real objects
         (only function, class, and method are handled)
        """
        if obj in self._done:
            return self._done[obj]
        self._done[obj] = node
        for name in dir(obj):
            try:
                member = getattr(obj, name)
            except AttributeError:
                # damned ExtensionClass.Base, I know you're there !
                attach_dummy_node(node, name)
                continue
            if ismethod(member):
                member = six.get_method_function(member)
            if isfunction(member):
                # verify this is not an imported function
                filename = getattr(six.get_function_code(member),
                                   'co_filename', None)
                if filename is None:
                    assert isinstance(member, object)
                    object_build_methoddescriptor(node, member, name)
                elif filename != getattr(self._module, '__file__', None):
                    attach_dummy_node(node, name, member)
                else:
                    object_build_function(node, member, name)
            elif isbuiltin(member):
                if (not _io_discrepancy(member) and
                        self.imported_member(node, member, name)):
                    continue
                object_build_methoddescriptor(node, member, name)
            elif isclass(member):
                if self.imported_member(node, member, name):
                    continue
                if member in self._done:
                    class_node = self._done[member]
                    if not class_node in node.locals.get(name, ()):
                        node.add_local_node(class_node, name)
                else:
                    class_node = object_build_class(node, member, name)
                    # recursion
                    self.object_build(class_node, member)
                if name == '__class__' and class_node.parent is None:
                    class_node.parent = self._done[self._module]
            elif ismethoddescriptor(member):
                assert isinstance(member, object)
                object_build_methoddescriptor(node, member, name)
            elif isdatadescriptor(member):
                assert isinstance(member, object)
                object_build_datadescriptor(node, member, name)
            elif type(member) in _CONSTANTS:
                attach_const_node(node, name, member)
            else:
                # create an empty node so that the name is actually defined
                attach_dummy_node(node, name, member)
项目:depot_tools    作者:webrtc-uwp    | 项目源码 | 文件源码
def object_build(self, node, obj):
        """recursive method which create a partial ast from real objects
         (only function, class, and method are handled)
        """
        if obj in self._done:
            return self._done[obj]
        self._done[obj] = node
        for name in dir(obj):
            try:
                member = getattr(obj, name)
            except AttributeError:
                # damned ExtensionClass.Base, I know you're there !
                attach_dummy_node(node, name)
                continue
            if ismethod(member):
                member = six.get_method_function(member)
            if isfunction(member):
                # verify this is not an imported function
                filename = getattr(six.get_function_code(member),
                                   'co_filename', None)
                if filename is None:
                    assert isinstance(member, object)
                    object_build_methoddescriptor(node, member, name)
                elif filename != getattr(self._module, '__file__', None):
                    attach_dummy_node(node, name, member)
                else:
                    object_build_function(node, member, name)
            elif isbuiltin(member):
                if (not _io_discrepancy(member) and
                        self.imported_member(node, member, name)):
                    continue
                object_build_methoddescriptor(node, member, name)
            elif isclass(member):
                if self.imported_member(node, member, name):
                    continue
                if member in self._done:
                    class_node = self._done[member]
                    if not class_node in node.locals.get(name, ()):
                        node.add_local_node(class_node, name)
                else:
                    class_node = object_build_class(node, member, name)
                    # recursion
                    self.object_build(class_node, member)
                if name == '__class__' and class_node.parent is None:
                    class_node.parent = self._done[self._module]
            elif ismethoddescriptor(member):
                assert isinstance(member, object)
                object_build_methoddescriptor(node, member, name)
            elif isdatadescriptor(member):
                assert isinstance(member, object)
                object_build_datadescriptor(node, member, name)
            elif type(member) in _CONSTANTS:
                attach_const_node(node, name, member)
            else:
                # create an empty node so that the name is actually defined
                attach_dummy_node(node, name, member)
项目:wuye.vim    作者:zhaoyingnan911    | 项目源码 | 文件源码
def object_build(self, node, obj):
        """recursive method which create a partial ast from real objects
         (only function, class, and method are handled)
        """
        if obj in self._done:
            return self._done[obj]
        self._done[obj] = node
        for name in dir(obj):
            try:
                member = getattr(obj, name)
            except AttributeError:
                # damned ExtensionClass.Base, I know you're there !
                attach_dummy_node(node, name)
                continue
            if ismethod(member):
                member = six.get_method_function(member)
            if isfunction(member):
                # verify this is not an imported function
                filename = getattr(six.get_function_code(member),
                                   'co_filename', None)
                if filename is None:
                    assert isinstance(member, object)
                    object_build_methoddescriptor(node, member, name)
                elif filename != getattr(self._module, '__file__', None):
                    attach_dummy_node(node, name, member)
                else:
                    object_build_function(node, member, name)
            elif isbuiltin(member):
                if (not _io_discrepancy(member) and
                        self.imported_member(node, member, name)):
                    continue
                object_build_methoddescriptor(node, member, name)
            elif isclass(member):
                if self.imported_member(node, member, name):
                    continue
                if member in self._done:
                    class_node = self._done[member]
                    if not class_node in node.locals.get(name, ()):
                        node.add_local_node(class_node, name)
                else:
                    class_node = object_build_class(node, member, name)
                    # recursion
                    self.object_build(class_node, member)
                if name == '__class__' and class_node.parent is None:
                    class_node.parent = self._done[self._module]
            elif ismethoddescriptor(member):
                assert isinstance(member, object)
                object_build_methoddescriptor(node, member, name)
            elif isdatadescriptor(member):
                assert isinstance(member, object)
                object_build_datadescriptor(node, member, name)
            elif type(member) in _CONSTANTS:
                attach_const_node(node, name, member)
            else:
                # create an empty node so that the name is actually defined
                attach_dummy_node(node, name, member)