Пример #1
0
    def test_expr_generate(self):
        """test the round trip of expressions to AST back to python source"""
        x = 1
        y = 2

        class F(object):
            def bar(self, a,b):
                return a + b

        def lala(arg):
            return "blah" + arg

        local_dict = dict(x=x, y=y, foo=F(), lala=lala)

        code = "str((x+7*y) / foo.bar(5,6)) + lala('ho')"
        astnode = pyparser.parse(code)
        newcode = pyparser.ExpressionGenerator(astnode).value()
        eq_(eval(code, local_dict), eval(newcode, local_dict))

        a = ["one", "two", "three"]
        hoho = {'somevalue': "asdf"}
        g = [1, 2, 3, 4, 5]
        local_dict = dict(a=a, hoho=hoho, g=g)
        code = "a[2] + hoho['somevalue'] + "\
                "repr(g[3:5]) + repr(g[3:]) + repr(g[:5])"
        astnode = pyparser.parse(code)
        newcode = pyparser.ExpressionGenerator(astnode).value()
        eq_(eval(code, local_dict), eval(newcode, local_dict))

        local_dict = {'f': lambda: 9, 'x': 7}
        code = "x+f()"
        astnode = pyparser.parse(code)
        newcode = pyparser.ExpressionGenerator(astnode).value()
        eq_(eval(code, local_dict), eval(newcode, local_dict))

        for code in ["repr({'x':7,'y':18})",
                        "repr([])",
                        "repr({})",
                        "repr([{3:[]}])",
                        "repr({'x':37*2 + len([6,7,8])})",
                        "repr([1, 2, {}, {'x':'7'}])",
                        "repr({'x':-1})", "repr(((1,2,3), (4,5,6)))",
                        "repr(1 and 2 and 3 and 4)",
                        "repr(True and False or 55)",
                        "repr(lambda x, y: x+y)",
                        "repr(1 & 2 | 3)",
                        "repr(3//5)",
                        "repr(3^5)",
                        "repr([q.endswith('e') for q in "
                            "['one', 'two', 'three']])",
                        "repr([x for x in (5,6,7) if x == 6])",
                        "repr(not False)"]:
            local_dict = {}
            astnode = pyparser.parse(code)
            newcode = pyparser.ExpressionGenerator(astnode).value()
            eq_(eval(code, local_dict),
                eval(newcode, local_dict)
            )
Пример #2
0
    def __init__(self, code, **exception_kwargs):
        self.code = code
        
        # represents all identifiers which are assigned to at some point in the code
        self.declared_identifiers = set()
        
        # represents all identifiers which are referenced before their assignment, if any
        self.undeclared_identifiers = set()
        
        # note that an identifier can be in both the undeclared and declared lists.

        # using AST to parse instead of using code.co_varnames, 
        # code.co_names has several advantages:
        # - we can locate an identifier as "undeclared" even if 
        # its declared later in the same block of code
        # - AST is less likely to break with version changes 
        # (for example, the behavior of co_names changed a little bit
        # in python version 2.5)
        if isinstance(code, basestring):
            expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs)
        else:
            expr = code

        f = pyparser.FindIdentifiers(self, **exception_kwargs)
        f.visit(expr)
Пример #3
0
Файл: ast.py Проект: SjB/mako
 def __init__(self, code, allow_kwargs=True, **exception_kwargs):
     self.code = code
     expr = pyparser.parse(code, "exec", **exception_kwargs)
             
     f = pyparser.ParseFunc(self, **exception_kwargs)
     f.visit(expr)
     if not hasattr(self, 'funcname'):
         raise exceptions.CompileException("Code '%s' is not a function declaration" % code, **exception_kwargs)
     if not allow_kwargs and self.kwargs:
         raise exceptions.CompileException("'**%s' keyword argument not allowed here" % self.argnames[-1], **exception_kwargs)
Пример #4
0
    def __init__(self, code, allow_kwargs=True, **exception_kwargs):
        self.code = code
        expr = pyparser.parse(code, "exec", **exception_kwargs)

        f = pyparser.ParseFunc(self, **exception_kwargs)
        f.visit(expr)
        if not hasattr(self, "funcname"):
            raise exceptions.CompileException(
                "Code '%s' is not a function declaration" % code,
                **exception_kwargs)
        if not allow_kwargs and self.kwargs:
            raise exceptions.CompileException(
                "'**%s' keyword argument not allowed here" %
                self.kwargnames[-1], **exception_kwargs)
Пример #5
0
    def __init__(self, code, **exception_kwargs):
        self.codeargs = []
        self.args = []
        self.declared_identifiers = set()
        self.undeclared_identifiers = set()
        if isinstance(code, basestring):
            if re.match(r"\S", code) and not re.match(r",\s*$", code):
                # if theres text and no trailing comma, insure its parsed
                # as a tuple by adding a trailing comma
                code  += ","
            expr = pyparser.parse(code, "exec", **exception_kwargs)
        else:
            expr = code

        f = pyparser.FindTuple(self, PythonCode, **exception_kwargs)
        f.visit(expr)
Пример #6
0
    def __init__(self, code, **exception_kwargs):
        self.codeargs = []
        self.args = []
        self.declared_identifiers = set()
        self.undeclared_identifiers = set()
        if isinstance(code, compat.string_types):
            if re.match(r"\S", code) and not re.match(r",\s*$", code):
                # if theres text and no trailing comma, insure its parsed
                # as a tuple by adding a trailing comma
                code += ","
            expr = pyparser.parse(code, "exec", **exception_kwargs)
        else:
            expr = code

        f = pyparser.FindTuple(self, PythonCode, **exception_kwargs)
        f.visit(expr)
Пример #7
0
    def __init__(self, code, **exception_kwargs):
        self.code = code

        # represents all identifiers which are assigned to at some point in the code
        self.declared_identifiers = util.Set()

        # represents all identifiers which are referenced before their assignment, if any
        self.undeclared_identifiers = util.Set()

        # note that an identifier can be in both the undeclared and declared lists.

        # using AST to parse instead of using code.co_varnames, code.co_names has several advantages:
        # - we can locate an identifier as "undeclared" even if its declared later in the same block of code
        # - AST is less likely to break with version changes (for example, the behavior of co_names changed a little bit
        # in python version 2.5)
        if isinstance(code, basestring):
            expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs)
        else:
            expr = code

        f = pyparser.FindIdentifiers(self, **exception_kwargs)
        f.visit(expr)
Пример #8
0
    def test_expr_generate(self):
        """test the round trip of expressions to AST back to python source"""
        x = 1
        y = 2

        class F(object):
            def bar(self, a, b):
                return a + b

        def lala(arg):
            return "blah" + arg

        local_dict = dict(x=x, y=y, foo=F(), lala=lala)

        code = "str((x+7*y) / foo.bar(5,6)) + lala('ho')"
        astnode = pyparser.parse(code)
        newcode = pyparser.ExpressionGenerator(astnode).value()
        eq_(eval(code, local_dict), eval(newcode, local_dict))

        a = ["one", "two", "three"]
        hoho = {"somevalue": "asdf"}
        g = [1, 2, 3, 4, 5]
        local_dict = dict(a=a, hoho=hoho, g=g)
        code = (
            "a[2] + hoho['somevalue'] + "
            "repr(g[3:5]) + repr(g[3:]) + repr(g[:5])"
        )
        astnode = pyparser.parse(code)
        newcode = pyparser.ExpressionGenerator(astnode).value()
        eq_(eval(code, local_dict), eval(newcode, local_dict))

        local_dict = {"f": lambda: 9, "x": 7}
        code = "x+f()"
        astnode = pyparser.parse(code)
        newcode = pyparser.ExpressionGenerator(astnode).value()
        eq_(eval(code, local_dict), eval(newcode, local_dict))

        for code in [
            "repr({'x':7,'y':18})",
            "repr([])",
            "repr({})",
            "repr([{3:[]}])",
            "repr({'x':37*2 + len([6,7,8])})",
            "repr([1, 2, {}, {'x':'7'}])",
            "repr({'x':-1})",
            "repr(((1,2,3), (4,5,6)))",
            "repr(1 and 2 and 3 and 4)",
            "repr(True and False or 55)",
            "repr(lambda x, y: (x + y))",
            "repr(lambda *arg, **kw: arg, kw)",
            "repr(1 & 2 | 3)",
            "repr(3//5)",
            "repr(3^5)",
            "repr([q.endswith('e') for q in " "['one', 'two', 'three']])",
            "repr([x for x in (5,6,7) if x == 6])",
            "repr(not False)",
        ]:
            local_dict = {}
            astnode = pyparser.parse(code)
            newcode = pyparser.ExpressionGenerator(astnode).value()
            if "lambda" in code:
                eq_(code, newcode)
            else:
                eq_(eval(code, local_dict), eval(newcode, local_dict))
Пример #9
0
 def visitCode(self, node):
     expr = pyparser.parse(textwrap.dedent(node.code.code))
     visitor = PythonVisitor(node.lineno - 1, node.pos)
     visitor.visit(expr)
     self.locations.extend(visitor.locations)