Example #1
0
 def rename(self, new_name):
     builtin = self._name
     super(BuiltinBinding, self).rename(new_name)
     self.namespace.body = list(
         insert(
             self.namespace.body,
             ast.Assign(targets=[ast.Name(id=new_name, ctx=ast.Store())],
                        value=ast.Name(id=builtin, ctx=ast.Load())),
         ))
Example #2
0
    def rename(self, new_name):
        Replacer(self.references, new_name).visit(self._local_namespace)

        self._local_namespace.body = list(
            insert(
                self._local_namespace.body,
                ast.Assign(targets=[ast.Name(id=new_name, ctx=ast.Store())],
                           value=self._value_node)))

        self._name = new_name
    def rename(self, new_name):

        for node in self.references:
            replace(node, ast.Name(id=new_name, ctx=ast.Load()))

        self._local_namespace.body = list(
            insert(
                self._local_namespace.body,
                ast.Assign(targets=[ast.Name(id=new_name, ctx=ast.Store())],
                           value=self._value_node)))

        self._name = new_name
Example #4
0
    def rename(self, new_name):
        """
        Rename this binding and all nodes that reference it

        :param str new_name: The new name to use

        """

        for node in self.references:

            if isinstance(node, ast.Name):

                if isinstance(node.ctx, (ast.Load, ast.Store, ast.Del)):
                    node.id = new_name
                else:
                    # Python 2 Param context

                    if arg_rename_in_place(node):
                        node.id = new_name

                    else:
                        node.namespace.body = list(
                            insert(
                                node.namespace.body,
                                ast.Assign(
                                    targets=[
                                        ast.Name(id=new_name, ctx=ast.Store())
                                    ],
                                    value=ast.Name(id=node.id, ctx=ast.Load()),
                                ),
                            ))

            elif isinstance(node, ast.FunctionDef) or (
                    hasattr(ast, 'AsyncFunctionDef')
                    and isinstance(node, ast.AsyncFunctionDef)):
                node.name = new_name
            elif isinstance(node, ast.ClassDef):
                node.name = new_name
            elif isinstance(node, ast.alias):
                if new_name == node.name:
                    node.asname = None
                else:
                    node.asname = new_name
            elif hasattr(ast, 'arg') and isinstance(node, ast.arg):

                if arg_rename_in_place(node):
                    node.arg = new_name

                else:
                    node.namespace.body = list(
                        insert(
                            node.namespace.body,
                            ast.Assign(
                                targets=[
                                    ast.Name(id=new_name, ctx=ast.Store())
                                ],
                                value=ast.Name(id=node.arg, ctx=ast.Load()),
                            ),
                        ))

            elif isinstance(node, ast.ExceptHandler):
                node.name = new_name
            elif isinstance(
                    node, ast.Global) or (hasattr(ast, 'Nonlocal')
                                          and isinstance(node, ast.Nonlocal)):
                node.names = [
                    new_name if n == self._name else n for n in node.names
                ]
            elif isinstance(node, ast.arguments):

                if node.vararg == self._name:
                    node.vararg = new_name
                if node.kwarg == self._name:
                    node.kwarg = new_name

        self._name = new_name
Example #5
0
    def rename(self, new_name):
        """
        Rename this binding and all nodes that reference it

        :param str new_name: The new name to use

        """

        func_namespace_binding = None

        for node in self.references:

            if isinstance(node, ast.Name):

                if isinstance(node.ctx, (ast.Load, ast.Store, ast.Del)):
                    node.id = new_name
                else:
                    # Python 2 Param context

                    if arg_rename_in_place(node):
                        node.id = new_name

                    else:
                        if func_namespace_binding is None:
                            func_namespace_binding = node.namespace
                        else:
                            assert func_namespace_binding is node.namespace

            elif is_ast_node(node, (ast.FunctionDef, 'AsyncFunctionDef')):
                node.name = new_name
            elif isinstance(node, ast.ClassDef):
                node.name = new_name
            elif isinstance(node, ast.alias):
                if new_name == node.name:
                    node.asname = None
                else:
                    node.asname = new_name
            elif is_ast_node(node, 'arg'):

                if arg_rename_in_place(node):
                    node.arg = new_name

                else:
                    if func_namespace_binding is None:
                        func_namespace_binding = node.namespace
                    else:
                        assert func_namespace_binding is node.namespace

            elif isinstance(node, ast.ExceptHandler):
                node.name = new_name
            elif is_ast_node(node, (ast.Global, 'Nonlocal')):
                node.names = [
                    new_name if n == self._name else n for n in node.names
                ]
            elif isinstance(node, ast.arguments):

                rename_vararg = (node.vararg == self._name) and not getattr(
                    node, 'vararg_renamed', False)
                rename_kwarg = (node.kwarg == self._name) and not getattr(
                    node, 'kwarg_renamed', False)

                if rename_vararg:
                    node.vararg = new_name
                    node.vararg_renamed = True
                if rename_kwarg:
                    node.kwarg = new_name
                    node.kwarg_renamed = True

            elif is_ast_node(node, 'MatchAs'):
                node.name = new_name
            elif is_ast_node(node, 'MatchStar'):
                node.name = new_name
            elif is_ast_node(node, 'MatchMapping'):
                node.rest = new_name

        if func_namespace_binding is not None:
            func_namespace_binding.body = list(
                insert(
                    func_namespace_binding.body,
                    ast.Assign(
                        targets=[ast.Name(id=new_name, ctx=ast.Store())],
                        value=ast.Name(id=self._name, ctx=ast.Load()),
                    ),
                ))

        self._name = new_name