def test_handle_class_scope(self):
     tree = ast.parse('class SomeClass:\n\tdef some_method():\n\t\tpass')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_class_scope(tree.body[0].body[0], tracker)
     self.assertTrue(
         len(tracker.definitions['classes']['SomeClass']['methods']), 1)
 def test_handle_function_scope_outside(self):
     tree = ast.parse('def some_function():\n\tpass\na = 42')
     add_parents(tree)
     tree = FunctionDefHandler().handle(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_function_scope(tree.body[1].targets[0], tracker)
     self.assertEqual(handled_node, tree.body[1].targets[0])
 def test_handle_function_scope(self):
     tree = ast.parse(
         'def global_function():\n\tdef function_function():\n\t\tpass')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_function_scope(tree.body[0].body[0], tracker)
     pass
 def test_handle_global_scope(self):
     tree = ast.parse('a = "some string"')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_global_scope(tree.body[0].targets[0], tracker)
     self.assertEqual(handled_node.id, '_0x61')
     self.assertEqual(len(tracker.definitions['variables']), 1)
 def test_handle_global_scope_outside(self):
     tree = ast.parse(
         'def global_function():\n\tdef function_function():\n\t\tpass')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_global_scope(tree.body[0].body[0], tracker)
     self.assertEqual(len(tracker.definitions['functions']), 0)
 def test_handle_class_scope_outside(self):
     tree = ast.parse(
         'def some_function():\n\tpass\nclass SomeClass:\n\tpass')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_class_scope(tree.body[0], tracker)
     self.assertTrue('some_function' not in tracker.definitions['classes']
                     ['SomeClass']['methods'])
 def test_handle_calls_class(self):
     tree = ast.parse(
         'class SomeClass:\n\tpass\nsome_variable = SomeClass()')
     add_parents(tree)
     tree = ClassDefHandler().handle(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_calls(tree.body[1].value.func, tracker)
     self.assertEqual(handled_node.id, '_0x38a')
 def test_handle_calls_function(self):
     tree = ast.parse(
         'def some_function():\n\tpass\nsome_variable = some_function()')
     add_parents(tree)
     tree = FunctionDefHandler().handle(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_calls(tree.body[1].value.func, tracker)
     self.assertEqual(handled_node.id, '_0x579')
 def test_handle_global_scope_outside(self):
     tree = ast.parse('def some_function():\n\tsome_variable = 42')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_global_scope(tree.body[0].body[0].targets[0],
                                        tracker)
     self.assertEqual(handled_node, tree.body[0].body[0].targets[0])
     self.assertEqual(len(tracker.definitions['variables']), 0)
 def test_handle_args(self):
     tree = ast.parse(
         'def some_function(param_1, param_2, param_3):\n\tpass')
     add_parents(tree)
     self.functiondef_handler.handle(tree)
     handled_node = handle_args(tree.body[0].args.args[0],
                                DefinitionTracker.get_instance())
     self.assertEqual(handled_node.arg, '_0x2a1')
Beispiel #11
0
 def setUp(self):
     self.fixture = AttributeHandler()
     self.classdef_handler = ClassDefHandler()
     self.functiondef_handler = FunctionDefHandler()
     self.tracker = DefinitionTracker.get_instance()
     self.source = 'class SomeClass:\n\tdef __init__():\n\t\tself.property = 42\n\t\tself.property2 = 84'
     self.tree = ast.parse(self.source)
     add_parents(self.tree)
     self.tree = self.classdef_handler.handle(self.tree)
 def test_handle_returns_variable(self):
     tree = ast.parse(
         'def some_function():\n\tsome_variable = 42\n\treturn some_variable'
     )
     add_parents(tree)
     tree = FunctionDefHandler().handle(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_returns(tree.body[0].body[1].value, tracker)
     self.assertEqual(handled_node.id, '_0x559')
 def test_handle_returns_func_in_func(self):
     tree = ast.parse(
         'def some_function():\n\tdef inside_function():\n\t\tpass\n\treturn inside_function()'
     )
     add_parents(tree)
     tree = FunctionDefHandler().handle(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_returns(tree.body[0].body[1].value.func, tracker)
     self.assertEqual(handled_node.id, '_0x641')
 def test_handle_class_scope_outside(self):
     tree = ast.parse('class SomeClass:\n\tpass\nvariable = 42')
     add_parents(tree)
     tree = ClassDefHandler().handle(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_class_scope(tree.body[1].targets[0], tracker)
     self.assertEqual(handled_node, tree.body[1].targets[0])
     self.assertEqual(
         len(tracker.definitions['classes']['SomeClass']['variables']), 0)
Beispiel #15
0
    def visit_Call(self, node):
        """Overrides the NodeTransformer visit_Call method. This method makes modifications
           to the abstract syntax tree and stores assignments with the DefinitionTracker class

        Args:
            **node (:obj: `ast.Call`)**: The current Call node to be modified
        """
        self.logger.info('visit_Call')
        tracker = DefinitionTracker.get_instance()
        return node
 def test_handle_returns_class(self):
     tree = ast.parse(
         'class SomeClass:\n\tpass\ndef some_function():\n\treturn SomeClass()'
     )
     add_parents(tree)
     tree = ClassDefHandler().handle(tree)
     tree = FunctionDefHandler().handle(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_returns(tree.body[1].body[0].value.func, tracker)
     self.assertEqual(handled_node.id, '_0x38a')
 def setUp(self):
     self.fixture = ClassDefHandler()
     self.tracker = DefinitionTracker.get_instance()
     self.tracker.clear_definitions()
     self.source = 'class TestClass(BaseClass):\n\t' \
                   'eighty_four = 84\n\t' \
                   'def __init__(self):\n\t\t' \
                   'self.forty_two = 42\n\t' \
                   'def some_method(self):\n\t\t' \
                   'pass'
     self.tree = ast.parse(self.source)
Beispiel #18
0
    def visit_arg(self, node):
        """Overrides the NodeTransformer visit_arg method. This method makes modifications
           to arg nodes in an abstract syntax tree

        Args:
            **node (:obj: `ast.Assign`)**: The current arg node to be modified

        Returns:
            The modified arg node
        """
        self.logger.info('visit_arg')
        tracker = DefinitionTracker.get_instance()
        node = handle_args(node, tracker)
        return node
 def setUp(self):
     self.fixture = FunctionDefHandler()
     self.classdef_handler = ClassDefHandler()
     self.tracker = DefinitionTracker.get_instance()
     self.source = 'def FirstFunction(param_1, param_2):\n\t' \
                   'first_variable = 42\n\t' \
                   'second_variable = param_1 + param_2\n\t' \
                   'return second_variable\n' \
                   'class SomeClass():\n\t' \
                   'def some_method():\n\t\t' \
                   'pass'
     self.tree = ast.parse(self.source)
     add_parents(self.tree)
     self.tree = self.classdef_handler.handle(self.tree)
Beispiel #20
0
    def visit_Attribute(self, node):
        """Overrides the NodeTransformer visit_Attribute method. This method makes modifications
           to the abstract syntax tree

        Args:
            **node (:obj: `ast.Attribute`)**: The current Attribute node to be modified

        Returns:
            The modified Attribute node
        """
        self.logger.info('visit_Attribute')
        tracker = DefinitionTracker.get_instance()
        node = handle_class_properties(node, tracker)
        node = handle_class_method_calls(node, tracker)
        return node
 def setUp(self):
     self.fixture = load_handlers(False, False)
     self.tracker = DefinitionTracker.get_instance()
     self.source = 'class FirstClass:\n\tpass\n\n' \
                   'class SecondClass:\n\tpass\n\n' \
                   'def FirstFunction():\n\tc = "a literal"\n\n' \
                   'def SecondFunction():\n\tpass\n\n' \
                   'a = FirstClass()\n\n' \
                   'b = SecondClass()\n\n' \
                   'FirstFunction()\n\n' \
                   'SecondFunction()\n\n' \
                   'a = SecondClass()\n\n' \
                   'c = 42'
     self.tree = ast.parse(self.source)
     add_parents(self.tree)
 def test_obscure_class_bases(self):
     tree = ast.parse('class SomeClass(Base1, Base2):\n\tpass')
     add_parents(tree)
     bases = []
     tracker = DefinitionTracker.get_instance()
     tracker.definitions['classes']['Base1'] = {
         'prev_name': 'Base1',
         'new_name': 'ObscuredBase1'
     }
     tracker.definitions['classes']['Base2'] = {
         'prev_name': 'Base2',
         'new_name': 'ObscuredBase2'
     }
     for base in tree.body[0].bases:
         bases.append(obscure_class_bases(base, tracker))
     self.assertEqual(bases[0].id, 'ObscuredBase1')
     self.assertEqual(bases[1].id, 'ObscuredBase2')
    def visit_ClassDef(self, node):
        """Overrides the NodeTransformer visit_ClassDef method. This method makes modifications
           to the abstract syntax tree and stores class definitions with the DefinitionTracker class

           Args:
               **node (:obj: `ast.ClassDef`)**: The current ClassDef node to be modified

            Returns:
                The modified ClassDef node
        """
        self.logger.info('visit_ClassDef')
        tracker = DefinitionTracker.get_instance()
        # Check to make sure this node is not already in tracker definitions
        if isinstance(node.name, str):
            class_dict = create_class_dictionary(node)
            tracker.add_class(class_dict)
            node.name = tracker.definitions['classes'][node.name]['new_name']
        return node
    def visit_FunctionDef(self, node):
        """Overrides the NodeTransformer visit_FunctionDef method. This method makes modifications
           to the abstract syntax tree and stores assignments with the DefinitionTracker class

        Args:
            **node (:obj: `ast.FunctionDef`)**: The current FunctionDef node to be modified

        Returns:
            The modified FunctionDef node
        """
        self.logger.info('visit_FunctionDef')
        tracker = DefinitionTracker.get_instance()
        if isinstance(node.name, str):
            node = handle_global_scope(node, tracker)
            node = handle_class_scope(node, tracker)
            node = handle_function_scope(node, tracker)
            self.generic_visit(node)

        return node
Beispiel #25
0
    def visit_Name(self, node):
        """Overrides the NodeTransformer visit_Name method. This method makes modifications
           to the abstract syntax tree  when it encounters a Name node

        Args:
           **node (:obj: `ast.Name`)**: The current Name node to be modified

        Returns:
            The modified Name node
        """
        self.logger.info('visit_Name')
        tracker = DefinitionTracker.get_instance()
        if isinstance(node.id, str):
            node = obscure_class_bases(node, tracker)
            node = handle_global_scope(node, tracker)
            node = handle_class_scope(node, tracker)
            node = handle_function_scope(node, tracker)
            node = handle_calls(node, tracker)
            node = handle_returns(node, tracker)
            self.generic_visit(node)

        return node
 def test_handle_global_scope(self):
     tree = ast.parse('def global_function():\n\tpass')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_global_scope(tree.body[0], tracker)
     self.assertTrue('global_function' in tracker.definitions['functions'])
 def tearDown(self):
     tracker = DefinitionTracker.get_instance()
     tracker.clear_definitions()
 def test_handle_returns_none(self):
     tree = ast.parse('def some_function():\n\tsome_variable = 42')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_returns(tree.body[0].body[0].targets[0], tracker)
     self.assertEqual(handled_node, tree.body[0].body[0].targets[0])
 def setUp(self):
     self.fixture = AssignHandler()
     self.tracker = DefinitionTracker.get_instance()
 def test_handle_calls_outside(self):
     tree = ast.parse('a = 42')
     add_parents(tree)
     tracker = DefinitionTracker.get_instance()
     handled_node = handle_calls(tree.body[0].targets[0], tracker)
     self.assertEqual(handled_node, tree.body[0].targets[0])