当前位置: 首页>>代码示例>>Python>>正文


Python sourcetools.compile2函数代码示例

本文整理汇总了Python中pypy.tool.sourcetools.compile2函数的典型用法代码示例。如果您正苦于以下问题:Python compile2函数的具体用法?Python compile2怎么用?Python compile2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了compile2函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: make_objclass_getter

def make_objclass_getter(func, cls, cache={}):
    if hasattr(func, "im_func"):
        assert not cls or cls is func.im_class
        cls = func.im_class
    if not cls:
        return unknown_objclass_getter, cls
    try:
        return cache[cls]
    except KeyError:
        pass
    miniglobals = {}
    if isinstance(cls, str):
        assert cls.startswith("<"), "pythontype typecheck should begin with <"
        cls_name = cls[1:]
        typeexpr = "space.w_%s" % cls_name
    else:
        miniglobals["cls"] = cls
        typeexpr = "space.gettypeobject(cls.typedef)"
    source = """if 1:
        def objclass_getter(space):
            return %s
        \n""" % (
        typeexpr,
    )
    exec compile2(source) in miniglobals
    res = miniglobals["objclass_getter"], cls
    cache[cls] = res
    return res
开发者ID:AishwaryaKM,项目名称:python-tutorial,代码行数:28,代码来源:typedef.py

示例2: generate_function

def generate_function(sig, opcodes):
    consolidate(opcodes)
    lines = []
    has_orbyte = False
    for op in opcodes:
        if isinstance(op, str):
            if has_orbyte:
                nextbyte = ord(op[0])
                if nextbyte:
                    lines.append('orbyte |= %d' % nextbyte)
                lines.append('builder.write(chr(orbyte))')
                has_orbyte = False
                op = op[1:]
            if op:
                lines.append('builder.write(%r)' % (op,))
        else:
            has_orbyte = op.eval(lines, has_orbyte)
    assert not has_orbyte, "malformed bytecode"

    if not lines:
        lines.append('pass')
    args = ', '.join(['builder'] + ['arg%d'%i for i in range(1, len(sig)+1)])
    lines.insert(0, 'def encode(%s):' % args)
    source = '\n    '.join(lines) + '\n'
    miniglobals = {
        'packimm32': packimm32,
        'packimm8': packimm8,
        'packimm16': packimm16,
        }
    exec compile2(source) in miniglobals
    return miniglobals['encode']
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:31,代码来源:ri386setup.py

示例3: make_fastfunc

 def make_fastfunc(unwrap_spec, func):
     unwrap_info = UnwrapSpec_FastFunc_Unwrap()
     unwrap_info.apply_over(unwrap_spec)
     narg = unwrap_info.n
     args = ['space'] + unwrap_info.args
     if args == unwrap_info.unwrap:
         fastfunc = func
     else:
         # try to avoid excessive bloat
         mod = func.__module__
         if mod is None:
             mod = ""
         if mod == 'pypy.interpreter.astcompiler.ast':
             raise FastFuncNotSupported
         if (not mod.startswith('pypy.module.__builtin__') and
             not mod.startswith('pypy.module.sys') and
             not mod.startswith('pypy.module.math')):
             if not func.__name__.startswith('descr'):
                 raise FastFuncNotSupported
         d = {}
         unwrap_info.miniglobals['func'] = func
         source = """if 1:
             def fastfunc_%s_%d(%s):
                 return func(%s)
             \n""" % (func.__name__, narg,
                      ', '.join(args),
                      ', '.join(unwrap_info.unwrap))
         exec compile2(source) in unwrap_info.miniglobals, d
         fastfunc = d['fastfunc_%s_%d' % (func.__name__, narg)]
     return narg, fastfunc
开发者ID:gorakhargosh,项目名称:pypy,代码行数:30,代码来源:gateway.py

示例4: _make_unwrap_activation_class

    def _make_unwrap_activation_class(self, unwrap_spec, cache={}):
        try:
            key = tuple(unwrap_spec)
            activation_factory_cls, run_args = cache[key]
            assert run_args == self.run_args, (
                "unexpected: same spec, different run_args")
            return activation_factory_cls
        except KeyError:
            parts = []
            for el in unwrap_spec:
                if isinstance(el, tuple):
                    parts.append(''.join([getattr(subel, '__name__', subel)
                                          for subel in el]))
                else:
                    parts.append(getattr(el, '__name__', el))
            label = '_'.join(parts)
            #print label

            d = {}
            source = """if 1:
                def _run(self, space, scope_w):
                    return self.behavior(%s)
                \n""" % (', '.join(self.run_args),)
            exec compile2(source) in self.miniglobals, d

            activation_cls = type("BuiltinActivation_UwS_%s" % label,
                             (BuiltinActivation,), d)
            activation_cls._immutable_ = True

            cache[key] = activation_cls, self.run_args
            return activation_cls
开发者ID:gorakhargosh,项目名称:pypy,代码行数:31,代码来源:gateway.py

示例5: make_function

    def make_function(self, fnargs, nbargs_before, mrdtable):
        if self._function is not None:
            return self._function
        name = self.get_function_name()
        self.compress_typechecks(mrdtable)
        checklines = self.generate_typechecks(fnargs[nbargs_before:])
        if not checklines:
            body = self.body
        else:
            checklines.append(self.body)
            body = '\n    '.join(checklines)
        source = 'def %s(%s):\n    %s\n' % (name, ', '.join(fnargs), body)

        if 0:    # for debugging the generated mm sources
            f = open('/tmp/mm-source/%s' % name, 'a')
            for possiblename in self.possiblenames:
                print >> f, '#',
                for part in possiblename:
                    print >> f, getattr(part, '__name__', part),
                print >> f
            print >> f
            print >> f, source
            f.close()

        exec compile2(source) in self.miniglobals
        self._function = self.miniglobals[name]
        return self._function
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:27,代码来源:multimethod.py

示例6: make_func

def make_func(name, desc):
    sig = []
    fieldvalues = []
    for field in desc.fields:
        if field in desc.specializations:
            fieldvalues.append((field, desc.specializations[field]))
        else:
            sig.append(field.name)
            fieldvalues.append((field, field.name))
    if isinstance(desc, IDupDesc):
        for destfield, srcfield in desc.dupfields.iteritems():
            fieldvalues.append((destfield, srcfield.name))
    body = ['v = r_uint(0)']
    assert 'v' not in sig # that wouldn't be funny
    #body.append('print %r'%name + ', ' + ', '.join(["'%s:', %s"%(s, s) for s in sig]))
    for field, value in fieldvalues:
        if field.name == 'spr':
            body.append('spr = (%s&31) << 5 | (%s >> 5 & 31)'%(value, value))
            value = 'spr'
        body.append('v |= (%3s & r_uint(%#05x)) << %d'%(value,
                                           field.mask,
                                           (32 - field.right - 1)))
    body.append('self.emit(v)')
    src = 'def %s(self, %s):\n    %s'%(name, ', '.join(sig), '\n    '.join(body))
    d = {'r_uint':r_uint}
    #print src
    exec compile2(src) in d
    return d[name]
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:28,代码来源:rassemblermaker.py

示例7: make_function

 def make_function(self, fnargs, nbargs_before, mrdtable):
     if self._function is not None:
         return self._function
     name = self.get_function_name()
     self.compress_typechecks(mrdtable)
     checklines = self.generate_typechecks(mrdtable, fnargs[nbargs_before:])
     if not checklines:
         body = self.body
     else:
         checklines.append(self.body)
         body = '\n    '.join(checklines)
     source = 'def %s(%s):\n    %s\n' % (name, ', '.join(fnargs), body)
     self.debug_dump(source)
     exec compile2(source) in self.miniglobals
     self._function = self.miniglobals[name]
     return self._function
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:16,代码来源:multimethod.py

示例8: _make_objclass_getter

def _make_objclass_getter(cls):
    if not cls:
        return unknown_objclass_getter, cls
    miniglobals = {}
    if isinstance(cls, str):
        assert cls.startswith('<'),"pythontype typecheck should begin with <"
        cls_name = cls[1:]
        typeexpr = "space.w_%s" % cls_name
    else:
        miniglobals['cls'] = cls
        typeexpr = "space.gettypeobject(cls.typedef)"
    source = """if 1:
        def objclass_getter(space):
            return %s
        \n""" % (typeexpr,)
    exec compile2(source) in miniglobals
    res = miniglobals['objclass_getter'], cls
    return res
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:18,代码来源:typedef.py

示例9: inplace_add

def inplace_add(x, y):
    x += y
    return x

def inplace_sub(x, y):
    x -= y
    return x

def inplace_mul(x, y):
    x *= y
    return x

exec compile2("""
def inplace_truediv(x, y):
    x /= y
    return x
""", flags=__future__.CO_FUTURE_DIVISION, dont_inherit=1)
#                     makes an INPLACE_TRUE_DIVIDE

def inplace_floordiv(x, y):
    x //= y
    return x

exec compile2("""
def inplace_div(x, y):
    x /= y
    return x
""", flags=0, dont_inherit=1)    # makes an INPLACE_DIVIDE

def inplace_mod(x, y):
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:30,代码来源:operation.py

示例10: make_perform_trampoline

def make_perform_trampoline(prefix, exprargs, expr, miniglobals,  multimethod, selfindex=0,
                            allow_NotImplemented_results=False):
    """NOT_RPYTHON"""    
    # mess to figure out how to put a gateway around executing expr
    argnames = ['_%d'%(i+1) for i in range(multimethod.arity)]
    explicit_argnames = multimethod.extras.get('argnames', [])
    argnames[len(argnames)-len(explicit_argnames):] = explicit_argnames
    solid_arglist = ['w_'+name for name in argnames]
    wrapper_arglist = solid_arglist[:]
    if multimethod.extras.get('varargs_w', False):
        wrapper_arglist.append('args_w')
    if multimethod.extras.get('w_varargs', False):
        wrapper_arglist.append('w_args')        
    if multimethod.extras.get('keywords', False):
        raise Exception, "no longer supported, use __args__"
    if multimethod.extras.get('general__args__', False):
        wrapper_arglist.append('__args__')
    wrapper_arglist += multimethod.extras.get('extra_args', ())

    miniglobals.update({ 'OperationError': OperationError,
                         'typeerrormsg': typeerrormsg})
    
    app_defaults = multimethod.extras.get('defaults', ())
    i = len(argnames) - len(app_defaults)
    wrapper_signature = wrapper_arglist[:]
    for app_default in app_defaults:
        name = wrapper_signature[i]
        wrapper_signature[i] = '%s=%s' % (name, name)
        miniglobals[name] = app_default
        i += 1

    wrapper_signature.insert(0, wrapper_signature.pop(selfindex))
    wrapper_sig  = ', '.join(wrapper_signature)

    src = []
    dest = []
    for wrapper_arg,expr_arg in zip(['space']+wrapper_arglist, exprargs):
        if wrapper_arg != expr_arg:
            src.append(wrapper_arg)
            dest.append(expr_arg)
    renaming = ', '.join(dest) +" = "+', '.join(src)

    # add a call to resolve_target to give the thunk space a chance to replace
    # the thing with something else
    offset = len(multimethod.argnames_before)
    renaming += "; %s = space.resolve_target(%s)" % (
            exprargs[selfindex+offset], exprargs[selfindex+offset])

    if allow_NotImplemented_results and (len(multimethod.specialnames) > 1 or
                                         multimethod.name.startswith('inplace_')):
        # turn FailedToImplement into NotImplemented
        code = """def %s_perform_call(space, %s):
                      %s
                      try:
                          return %s
                      except FailedToImplement, e:
                          if e.w_type is not None:
                              raise OperationError(e.w_type, e.w_value)
                          else:
                              return space.w_NotImplemented
"""        % (prefix, wrapper_sig, renaming, expr)
    else:
        # turn FailedToImplement into nice TypeErrors
        code = """def %s_perform_call(space, %s):
                      %s
                      try:
                          w_res = %s
                      except FailedToImplement, e:
                          if e.w_type is not None:
                              raise OperationError(e.w_type, e.w_value)
                          else:
                              raise OperationError(space.w_TypeError,
                                  typeerrormsg(space, %r, [%s]))
                      if w_res is None:
                          w_res = space.w_None
                      return w_res
"""        % (prefix, wrapper_sig, renaming, expr,
              multimethod.operatorsymbol, ', '.join(solid_arglist))
    exec compile2(code, '', 'exec') in miniglobals 
    return miniglobals["%s_perform_call" % prefix]
开发者ID:enyst,项目名称:plexnet,代码行数:80,代码来源:stdtypedef.py

示例11: build_function

    def build_function(self, target, funcname, func_selfarg_index,
                       things_to_call):
        # support for inventing names for the entries in things_to_call
        # which are real function objects instead of strings
        miniglobals = {'FailedToImplement': FailedToImplement, '__name__': __name__}
        def invent_name(obj):
            if isinstance(obj, str):
                return obj
            name = obj.__name__
            n = 1
            while name in miniglobals:
                n += 1
                name = '%s%d' % (obj.__name__, n)
            miniglobals[name] = obj
            return name

        funcargs = ['arg%d' % i for i in range(self.multimethod.arity)]

        bodylines = []
        for conversion, call, call_selfarg_index in things_to_call:
            callargs = funcargs[:]
            if conversion is not None:
                to_convert = func_selfarg_index
                convert_callargs = (self.multimethod.argnames_before +
                                    [callargs[to_convert]])
                callargs[to_convert] = '%s(%s)' % (
                    invent_name(conversion), ', '.join(convert_callargs))
            callname = invent_name(call)
            if call_selfarg_index is not None:
                # fallback on root_class
                self.build_function(self.multimethod.root_class,
                                    callname, call_selfarg_index, [])
                callname = '%s.%s' % (callargs.pop(call_selfarg_index), callname)
            callargs = (self.multimethod.argnames_before +
                        callargs + self.multimethod.argnames_after)
            bodylines.append('return %s(%s)' % (callname, ', '.join(callargs)))

        fallback = False
        if not bodylines:
            miniglobals['raiseFailedToImplement'] = raiseFailedToImplement
            bodylines = ['return raiseFailedToImplement()']
            fallback = True
            # NB. make sure that there is only one fallback function object,
            # i.e. the key used in the mmfunccache below is always the same
            # for all functions with the same name and an empty bodylines.

        # protect all lines apart from the last one by a try:except:
        for i in range(len(bodylines)-2, -1, -1):
            bodylines[i:i+1] = ['try:',
                                '    ' + bodylines[i],
                                'except FailedToImplement:',
                                '    pass']

        if func_selfarg_index is not None:
            selfargs = [funcargs.pop(func_selfarg_index)]
        else:
            selfargs = []
        funcargs = (selfargs + self.multimethod.argnames_before +
                    funcargs + self.multimethod.argnames_after)

        if target is None and not self.baked_perform_call:
            return funcargs, bodylines[0][len('return '):], miniglobals, fallback

        # indent mode
        bodylines = ['    ' + line for line in bodylines]

        bodylines.insert(0, 'def %s(%s):' % (funcname, ', '.join(funcargs)))
        bodylines.append('')
        source = '\n'.join(bodylines)

        # XXX find a better place (or way) to avoid duplicate functions 
        l = miniglobals.items()
        l.sort()
        l = tuple(l)
        key = (source, l)
        try: 
            func = self.mmfunccache[key]
        except KeyError: 
            exec compile2(source) in miniglobals
            func = miniglobals[funcname]
            self.mmfunccache[key] = func 
        #else: 
        #    print "avoided duplicate function", func
        self.to_install.append((target, funcname, func, source, fallback))
        return func
开发者ID:Debug-Orz,项目名称:Sypy,代码行数:85,代码来源:multimethod.py


注:本文中的pypy.tool.sourcetools.compile2函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。