def test_freeVariables_globalStmt_2(self):
     tree = ast.parse(
         textwrap.dedent("""
             def f():
                 global x
                 return x
             """))
     with self.assertRaises(Exceptions.PythonToForaConversionError):
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
 def test_freeVariables_globalStmt_2(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             def f():
                 global x
                 return x
             """
             )
         )
     with self.assertRaises(Exceptions.PythonToForaConversionError):
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
    def test_member_acces_after_possible_assignment(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                def f():
                    if 0:
                       x = 2
                    x.y
                """
                )
            )
        expectedResult = set(['x'])
        self.assertEqual(
            expectedResult,
            PyAstUninstantiatedVariablesAnalysis.
                collectPossiblyUninitializedLocalVariables(tree)
            )

        self.assertEqual(
            set(),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )

        self.assertEqual(
            set(),
            PyAstFreeVariableAnalyses.getFreeVariableMemberAccessChains(tree)
            )        
    def test_freeVariables_classDef_1(self):
        tree = ast.parse(
            textwrap.dedent("""class C(object):
                    def f(x):
                        return len(f)"""))

        self.assertEqual(set(['object', 'len', 'f']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_Assign(self):
        tree = ast.parse(
            textwrap.dedent("""
                (x, y), z = w
                """))

        self.assertEqual(set(['w']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_Lambda_1(self):
        tree = ast.parse(
            textwrap.dedent("""
                lambda x, y = z, *args, **kwargs: (x, y, args, kwargs, free)
                """))

        self.assertEqual(set(['z', 'free']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_ListComp_2(self):
        tree = ast.parse(
            textwrap.dedent("""
                [val for val in x if val == free]
                """))

        self.assertEqual(set(['free', 'x']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_ListComp_4(self):
        tree = ast.parse(
            textwrap.dedent("""
                [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
                """))

        self.assertEqual(set(),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_SetComp_1(self):
        tree = ast.parse(
            textwrap.dedent("""
                {v for x in q}
                """))

        self.assertEqual(set(['v', 'q']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_DictComp_1(self):
        tree = ast.parse(
            textwrap.dedent("""
                d = { x: y for x, y in o.f() }
                """))

        self.assertEqual(set(['o']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_DictComp_2(self):
        tree = ast.parse(
            textwrap.dedent("""
                d = { x1: y1 for x2, y2 in o.f() }
                """))

        self.assertEqual(set(['o', 'x1', 'y1']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_functionDef_2(self):
        tree = ast.parse(
            textwrap.dedent("""def outer():
                    def f(x):
                        return len(f)"""))

        self.assertEqual(set(['len']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_classLocalVar_1(self):
        tree = ast.parse(
            textwrap.dedent("""
                class C(object):
                    x = 0
                """))

        self.assertEqual(set(['object']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
 def test_freeVariables_withStatement(self):
     tree = ast.parse(
         textwrap.dedent("""
             def f(arg):
                 with x as e:
                     return e
             """))
     self.assertEqual(set(['x']),
                      PyAstFreeVariableAnalyses.getFreeVariables(tree))
 def test_freeVariables_inAssignment(self):
     tree = ast.parse(
         textwrap.dedent("""
             def f(arg):
                 y = x
                 return y
             """))
     self.assertEqual(set(['x']),
                      PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_ListComp_3(self):
        tree = ast.parse(
            textwrap.dedent("""
                x = [1,2,3]
                [y for val in x]
                """))

        self.assertEqual(set(['y']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_call_and_then_member(self):
        tree = ast.parse(
            textwrap.dedent("""
                def f():
                    return g(1).__str__()
                """))

        self.assertEqual(set(['g']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
 def test_freeVariables_name_substring_bug(self):
     tree = ast.parse(
         textwrap.dedent("""
             al = 10
             l
             """))
     expectedResult = set(['l'])
     self.assertEqual(expectedResult,
                      PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_For(self):
        tree = ast.parse(
            textwrap.dedent("""
                for x in elt:
                    x + 2
                """))

        self.assertEqual(set(['elt']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_functionCalls(self):
        tree = ast.parse(
            textwrap.dedent("""
                x = 2
                f(x, y, z = 2, w = x, q = free, *args, **kwargs)
                """))

        self.assertEqual(set(['f', 'y', 'args', 'kwargs', 'free']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
 def test_freeVariables_notFreeNotDefinedOnAllPaths_2(self):
     tree = ast.parse(
         textwrap.dedent("""
             def f(arg):
                 if arg:
                     x = 3
                 return x
             """))
     self.assertEqual(set([]),
                      PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_function_context(self):
        tree = ast.parse(
            textwrap.dedent("""
                def fib(x):
                    if x < 2:
                        return 1
                    else:
                        return fib(x-1) + fib(x-2)
                """))

        self.assertEqual(
            set([]),
            PyAstFreeVariableAnalyses.getFreeVariables(tree.body[0],
                                                       isClassContext=False))

        self.assertEqual(
            set(['fib']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree.body[0],
                                                       isClassContext=True))
    def test_freeVariables_Sequence_4(self):
        tree = ast.parse(
            textwrap.dedent("""
                def f():
                    x = 3
                    x + 2
                """))

        self.assertEqual(set(),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
 def test_freeVariables_forLoop(self):
     tree = ast.parse(
         textwrap.dedent("""
             def f(arg):
                 tr = x
                 for x in xrange(0, arg):
                     tr += x
                 return tr
             """))
     self.assertEqual(set(['xrange']),
                      PyAstFreeVariableAnalyses.getFreeVariables(tree))
 def test_freeVariables_whileLoop(self):
     tree = ast.parse(
         textwrap.dedent("""
             def f(arg):
                 tr = 0
                 while x in range(0, arg):
                     tr += 1
                 return tr
             """))
     self.assertEqual(set(['x', 'range']),
                      PyAstFreeVariableAnalyses.getFreeVariables(tree))
 def test_members(self):
     tree = ast.parse(
         textwrap.dedent("""
             def f(arg):
                 if arg:
                     x.y = 3
                 return x
             """))
     expectedResult = set(['x'])
     self.assertEqual(expectedResult,
                      PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_nestedScopes_1(self):
        tree = ast.parse(
            textwrap.dedent("""
                def f(x):
                  y = 2
                  class C:
                    def g(self, arg):
                      x + y + arg
                """))

        self.assertEqual(set([]),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_Sequence_1(self):
        # this might seem a little strange,
        # but ast parses this as a module

        tree = ast.parse(
            textwrap.dedent("""
                x = 2
                x + 3
                """))

        self.assertEqual(set(),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_assignToSelf(self):
        tree = ast.parse(
            textwrap.dedent("""
                x = x
                def f():
                    y = y
                    class C(object):
                        z = z
                """))

        self.assertEqual(set(['object', 'z']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_classDef_2(self):
        tree = ast.parse(
            textwrap.dedent("""
                @decorator
                class C(B, D):
                    def f(self):
                        return self.g + C + g
                    def g(self):
                        return 0"""))

        self.assertEqual(set(['g', 'decorator', 'B', 'D']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
    def test_freeVariables_Sequence_6(self):
        tree = ast.parse(
            textwrap.dedent("""
                class C:
                    def f(self, arg):
                        return arg + x
                    x = 2
                    f
                """))

        self.assertEqual(set(['x', 'f']),
                         PyAstFreeVariableAnalyses.getFreeVariables(tree))
Exemplo n.º 32
0
    def test_freeVariables_DictComp_1(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                d = { x: y for x, y in o.f() }
                """
                )
            )

        self.assertEqual(
            set(['o']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 33
0
    def test_freeVariables_Lambda_1(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                lambda x, y = z, *args, **kwargs: (x, y, args, kwargs, free)
                """
                )
            )

        self.assertEqual(
            set(['z', 'free']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 34
0
    def test_freeVariables_DictComp_2(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                d = { x1: y1 for x2, y2 in o.f() }
                """
                )
            )

        self.assertEqual(
            set(['o', 'x1', 'y1']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 35
0
    def test_freeVariables_ListComp_2(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                [val for val in x if val == free]
                """
                )
            )

        self.assertEqual(
            set(['free', 'x']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 36
0
    def test_freeVariables_classDef_1(self):
        tree = ast.parse(
            textwrap.dedent(
                """class C(object):
                    def f(x):
                        return len(f)"""
                )
            )

        self.assertEqual(
            set(['object', 'len', 'f']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 37
0
 def test_freeVariables_ListComp_4(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
             """
             )
         )
     
     self.assertEqual(
         set(),
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 38
0
    def test_freeVariables_function_context(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                def fib(x):
                    if x < 2:
                        return 1
                    else:
                        return fib(x-1) + fib(x-2)
                """
                )
            )

        self.assertEqual(
            set([]),
            PyAstFreeVariableAnalyses.getFreeVariables(tree.body[0], isClassContext = False)
            )

        self.assertEqual(
            set(['fib']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree.body[0], isClassContext = True)
            )
Exemplo n.º 39
0
    def test_freeVariables_functionDef_2(self):
        tree = ast.parse(
            textwrap.dedent(
                """def outer():
                    def f(x):
                        return len(f)"""
                )
            )

        self.assertEqual(
            set(['len']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
    def test_freeVariables_class_context(self):
        tree = ast.parse(
            textwrap.dedent("""
                class testClass:
                    def __init__(self, x):
                        self.x = x

                    def f(self):
                        return testClass("a")
                """))

        self.assertEqual(
            set([]), PyAstFreeVariableAnalyses.getFreeVariables(tree.body[0]))
Exemplo n.º 41
0
    def test_freeVariables_Assign(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                (x, y), z = w
                """
                )
            )

        self.assertEqual(
            set(['w']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 42
0
    def test_freeVariables_SetComp_1(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                {v for x in q}
                """
                )
            )

        self.assertEqual(
            set(['v', 'q']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 43
0
    def test_freeVariables_ListComp_3(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                x = [1,2,3]
                [y for val in x]
                """
                )
            )

        self.assertEqual(
            set(['y']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 44
0
    def test_call_and_then_member(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                def f():
                    return g(1).__str__()
                """
                )
            )

        self.assertEqual(
            set(['g']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 45
0
 def test_freeVariables_withStatement(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             def f(arg):
                 with x as e:
                     return e
             """
             )
         )
     self.assertEqual(
         set(['x']),
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 46
0
    def test_freeVariables_functionCalls(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                x = 2
                f(x, y, z = 2, w = x, q = free, *args, **kwargs)
                """
                )
            )

        self.assertEqual(
            set(['f', 'y', 'args', 'kwargs', 'free']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 47
0
 def test_freeVariables_inAssignment(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             def f(arg):
                 y = x
                 return y
             """
             )
         )
     self.assertEqual(
         set(['x']),
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 48
0
 def test_freeVariables_name_substring_bug(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             al = 10
             l
             """
             )
         )
     expectedResult = set(['l'])
     self.assertEqual(
         expectedResult,
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 49
0
    def test_freeVariables_classLocalVar_1(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                class C(object):
                    x = 0
                """
                )
            )

        self.assertEqual(
            set(['object']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 50
0
    def test_freeVariables_For(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                for x in elt:
                    x + 2
                """
                )
            )

        self.assertEqual(
            set(['elt']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 51
0
 def test_freeVariables_notFreeNotDefinedOnAllPaths_2(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             def f(arg):
                 if arg:
                     x = 3
                 return x
             """
             )
         )
     self.assertEqual(
         set([]),
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 52
0
    def test_freeVariables_Sequence_4(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                def f():
                    x = 3
                    x + 2
                """
                )
            )

        self.assertEqual(
            set(),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 53
0
 def test_freeVariables_forLoop(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             def f(arg):
                 tr = x
                 for x in xrange(0, arg):
                     tr += x
                 return tr
             """
             )
         )
     self.assertEqual(
         set(['xrange']),
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 54
0
 def test_freeVariables_whileLoop(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             def f(arg):
                 tr = 0
                 while x in range(0, arg):
                     tr += 1
                 return tr
             """
             )
         )
     self.assertEqual(
         set(['x', 'range']),
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 55
0
 def test_members(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             def f(arg):
                 if arg:
                     x.y = 3
                 return x
             """
             )
         )
     expectedResult = set(['x'])
     self.assertEqual(
         expectedResult,
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
         )
Exemplo n.º 56
0
    def test_freeVariables_assignToSelf(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                x = x
                def f():
                    y = y
                    class C(object):
                        z = z
                """
                )
            )

        self.assertEqual(
            set(['object', 'z']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 57
0
    def test_freeVariables_nestedScopes_1(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                def f(x):
                  y = 2
                  class C:
                    def g(self, arg):
                      x + y + arg
                """
                )
            )

        self.assertEqual(
            set([]),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 58
0
    def test_freeVariables_Sequence_6(self):
        tree = ast.parse(
            textwrap.dedent(
                """
                class C:
                    def f(self, arg):
                        return arg + x
                    x = 2
                    f
                """
                )
            )

        self.assertEqual(
            set(['x', 'f']),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )
Exemplo n.º 59
0
 def test_freeVariables_CompsAndGenExp1(self):
     tree = ast.parse(
         textwrap.dedent(
             """
             [elt1 for elt1 in container1]
             {elt2 for elt2 in container2}
             {elt3: elt4 for elt4 in container4 for elt3 in container3}
             (x*y for x in range(10) for y in bar(x))
             """
             )
         )
     expectedResult = set(['container1', 'container2', 'container3',
                           'container4', 'bar', 'range'])
     self.assertEqual(
         expectedResult,
         PyAstFreeVariableAnalyses.getFreeVariables(tree)
     )
Exemplo n.º 60
0
    def test_freeVariables_Sequence_1(self):
        # this might seem a little strange,
        # but ast parses this as a module

        tree = ast.parse(
            textwrap.dedent(
                """
                x = 2
                x + 3
                """
                )
            )

        self.assertEqual(
            set(),
            PyAstFreeVariableAnalyses.getFreeVariables(tree)
            )