Exemple #1
0
    def test_TransvisitorsDontModifyTree(self):
        tree = ast.parse(self.some_python_code)
        # deep-copy tree because transformers modify the AST in place
        # making the test of areAstsIdentical(tree, tree') meaningless
        tree1 = copy.deepcopy(tree)
        noopTransformer = NodeVisitorBases.SemanticOrderNodeTransvisitor()
        tree2 = noopTransformer.visit(tree1)

        self.assertIsNotNone(tree2)
        self.assertTrue(PyAstUtil.areAstsIdentical(tree, tree2))

        scopeMgr = NodeVisitorBases.InScopeSaveRestoreValue(
            lambda: True, lambda x: None)
        noopTransformer = NodeVisitorBases.GenericScopedTransvisitor(scopeMgr)
        tree3 = noopTransformer.visit(tree1)

        self.assertIsNotNone(tree3)
        self.assertTrue(PyAstUtil.areAstsIdentical(tree, tree3))

        freeVarsVisitor = PyAstFreeVariableAnalyses._FreeVariableMemberAccessChainsTransvisitor(
        )
        tree4 = freeVarsVisitor.visit(tree1)

        self.assertIsNotNone(tree4)
        self.assertTrue(PyAstUtil.areAstsIdentical(tree, tree4))
Exemple #2
0
 def test_FreeVariableTransformer_3(self):
     tree = ast.parse(
         textwrap.dedent("""
             def g(y):
                 return x.y.z.__str__()
             """))
     # deep-copy tree because transformers modify the AST in place
     # making the test of areAstsIdentical(tree, tree') meaningless
     tree1 = copy.deepcopy(tree)
     tree2 = PyAstFreeVariableAnalyses.collapseFreeVariableMemberAccessChains(
         tree1, {('x', 'y', 'z'): 'x_y_z'}, isClassContext=False)
     self.assertIsNotNone(tree2)
     self.assertFalse(PyAstUtil.areAstsIdentical(tree, tree2))