예제 #1
0
def compile_let(builder, args, kw, star, dstar):
    """Compile the let() function"""
    if args or star or dstar:
        raise TypeError("let() only accepts inline keyword arguments")

    for k,v in kw:
        k = build(builder, k)
        assert type(k) is Const and isinstance(k.value, basestring)
        k = k.value
        v = build(builder, v)
        builder.bind({k:v})
    return True
예제 #2
0
def compile_let(builder, args, kw, star, dstar):
    """Compile the let() function"""
    if args or star or dstar:
        raise TypeError("let() only accepts inline keyword arguments")

    for k, v in kw:
        k = build(builder, k)
        assert type(k) is Const and isinstance(k.value, basestring)
        k = k.value
        v = build(builder, v)
        builder.bind({k: v})
    return True
예제 #3
0
def build_pattern(builder, node):
    old = builder.__class__
    builder.__class__ = SyntaxBuilder
    try:
        return build(builder, node)
    finally:
        builder.__class__ = old
예제 #4
0
    def expand(builder, *args):
        builder.push(bindings)  # globals, locals, etc.
        builder.bind(apply_meta(builder, (getargs, {}, arginfo), *args))

        # build in the newly-isolated namespace
        result = build(builder, parsed)
        builder.pop()
        return result
예제 #5
0
    def expand(builder, *args):        
        builder.push(bindings)   # globals, locals, etc.
        builder.bind(apply_meta(builder, (getargs, {}, arginfo), *args))

        # build in the newly-isolated namespace
        result = build(builder, parsed) 
        builder.pop()
        return result
예제 #6
0
def _expand_as(func, predicate_string, *namespaces):
    """Pre-parse predicate string and register meta function"""

    args, varargs, kw, defaults = arginfo = inspect.getargspec(func)
    argnames = list(flatten(filter(None, [args, varargs, kw])))
    parsed = parser.expr(predicate_string).totuple(1)[1]
    builder = CriteriaBuilder(
        dict([(arg,Local(arg)) for arg in argnames]), *namespaces
    )
    bindings = {}
    for b in builder.bindings[-len(namespaces):][::-1]:
        bindings.update(b)

    # Make a function that just gets the arguments we want
    c = Code.from_function(func)
    c.return_(Call(Const(locals),fold=False))
    getargs = new.function(
        c.code(), func.func_globals, func.func_name, func.func_defaults,
        func.func_closure
    )

    def expand(builder, *args):        
        builder.push(bindings)   # globals, locals, etc.
        builder.bind(apply_meta(builder, (getargs, {}, arginfo), *args))

        # build in the newly-isolated namespace
        result = build(builder, parsed) 
        builder.pop()
        return result

    meta_functions[func] = expand

    c = Code.from_function(func)
    c.return_()
    if func.func_code.co_code == c.code().co_code:  # function body is empty
        c = Code.from_function(func)
        c.return_(build(builder, parsed))
        func.func_code = c.code()

    return func
예제 #7
0
def _expand_as(func, predicate_string, *namespaces):
    """Pre-parse predicate string and register meta function"""

    args, varargs, kw, defaults = arginfo = inspect.getargspec(func)
    argnames = list(flatten(filter(None, [args, varargs, kw])))
    parsed = parser.expr(predicate_string).totuple(1)[1]
    builder = CriteriaBuilder(dict([(arg, Local(arg)) for arg in argnames]),
                              *namespaces)
    bindings = {}
    for b in builder.bindings[-len(namespaces):][::-1]:
        bindings.update(b)

    # Make a function that just gets the arguments we want
    c = Code.from_function(func)
    c.return_(Call(Const(locals), fold=False))
    getargs = new.function(c.code(), func.func_globals, func.func_name,
                           func.func_defaults, func.func_closure)

    def expand(builder, *args):
        builder.push(bindings)  # globals, locals, etc.
        builder.bind(apply_meta(builder, (getargs, {}, arginfo), *args))

        # build in the newly-isolated namespace
        result = build(builder, parsed)
        builder.pop()
        return result

    meta_functions[func] = expand
    func.__doc__  # workaround for PyPy issue #1293
    c = Code.from_function(func)
    c.return_()
    if func.func_code.co_code == c.code().co_code:  # function body is empty
        c = Code.from_function(func)
        c.return_(build(builder, parsed))
        func.func_code = c.code()

    return func
예제 #8
0
 def Compare(self, expr, ops):
     return Compare(build(self, expr),
                    [(op == '<>' and '!=' or op, build(self, arg))
                     for op, arg in ops])
예제 #9
0
        elif name=='__dstar__': data[name] = parse(name, dstar)
        else:
            break
        offset += 1

    for k, v in zip(argnames[offset:], args):
        data[k] = parse(k, v)

    varargpos = len(argnames)-offset
    if len(args)> varargpos:
        if not varargs:
            raise TypeError("Too many arguments for %r" % (func,))
        extra.extend([parse(varargs, node) for node in args[varargpos:]])

    for k,v in kw:
        k = build(builder, k)
        assert type(k) is Const and isinstance(k.value, basestring)
        k = k.value
        if k in data:
            raise TypeError("Duplicate keyword %s for %r" % (k,func))

        if varkw and k not in argnames and k not in parsers:
            data[k] = parse(varkw,  v)
        else:
            data[k] = parse(k, v)

    if star and '__star__' not in data:
        raise TypeError("%r does not support parsing *args" % (func,))

    if dstar and '__dstar__' not in data:
        raise TypeError("%r does not support parsing **kw" % (func,))
예제 #10
0
 def build_with(self, expr):
     self.push()
     try:
         return build(self, expr)
     finally:
         self.pop()
예제 #11
0
 def method(self, items):
     result = build(self,items[0])
     for item in items[1:]:
         result = nt(result, build(self,item))
     return result
예제 #12
0
 def build_with(self, expr):
     self.push()
     try:
         return build(self, expr)
     finally:
         self.pop()
예제 #13
0
 def Getattr(self, expr, attr):
     return Getattr(build(self,expr), attr)
예제 #14
0
 def method(self, expr):
     return nt(build(self,expr))
예제 #15
0
 def Slice2(self, start, stop):
     start = start and build(self, start) or Pass
     stop  = stop  and build(self, stop ) or Pass
     return GetSlice(Pass, start, stop)
예제 #16
0
 def Subscript(self, left, right):
     expr = build(self, left)
     key = build(self, right)
     if isinstance(key, GetSlice):
         return GetSlice(expr, key.start, key.stop)
     return Getitem(expr, key)
예제 #17
0
 def Getattr(self, expr, attr):
     return Getattr(build(self, expr), attr)
예제 #18
0
 def Slice3(self, start, stop, stride):
     start = start and build(self, start) or Pass
     stop = stop and build(self, stop) or Pass
     stride = stride and build(self, stride) or Pass
     return BuildSlice(start, stop, stride)
예제 #19
0
 def Slice2(self, start, stop):
     start = start and build(self, start) or Pass
     stop = stop and build(self, stop) or Pass
     return GetSlice(Pass, start, stop)
예제 #20
0
 def Subscript(self, left, right):
     expr = build(self,left)
     key =  build(self,right)
     if isinstance(key, GetSlice):
         return GetSlice(expr, key.start, key.stop)
     return Getitem(expr, key)
예제 #21
0
 def method(self, expr):
     return nt(build(self, expr))
예제 #22
0
 def Slice3(self, start, stop, stride):
     start  = start  and build(self, start ) or Pass
     stop   = stop   and build(self, stop  ) or Pass
     stride = stride and build(self, stride) or Pass
     return BuildSlice(start, stop, stride)
예제 #23
0
 def method(self, left, right):
     return nt(build(self, left), build(self, right))
예제 #24
0
 def Compare(self, expr, ops):
     return Compare(
         build(self, expr),
         [(op=='<>' and '!=' or op, build(self,arg)) for op, arg in ops]
     )
예제 #25
0
 def method(self, items):
     result = build(self, items[0])
     for item in items[1:]:
         result = nt(result, build(self, item))
     return result
예제 #26
0
 def method(self, left, right):
     return nt(build(self,left), build(self,right))
예제 #27
0
 def IfElse(self, tval, cond, fval):
     return IfElse(build(self, tval), build(self, cond), build(self, fval))
예제 #28
0
 def IfElse(self, tval, cond, fval):
     return IfElse(build(self,tval), build(self,cond), build(self,fval))
예제 #29
0
        elif name == '__dstar__': data[name] = parse(name, dstar)
        else:
            break
        offset += 1

    for k, v in zip(argnames[offset:], args):
        data[k] = parse(k, v)

    varargpos = len(argnames) - offset
    if len(args) > varargpos:
        if not varargs:
            raise TypeError("Too many arguments for %r" % (func, ))
        extra.extend([parse(varargs, node) for node in args[varargpos:]])

    for k, v in kw:
        k = build(builder, k)
        assert type(k) is Const and isinstance(k.value, basestring)
        k = k.value
        if k in data:
            raise TypeError("Duplicate keyword %s for %r" % (k, func))

        if varkw and k not in argnames and k not in parsers:
            data[k] = parse(varkw, v)
        else:
            data[k] = parse(k, v)

    if star and '__star__' not in data:
        raise TypeError("%r does not support parsing *args" % (func, ))

    if dstar and '__dstar__' not in data:
        raise TypeError("%r does not support parsing **kw" % (func, ))