Ejemplo n.º 1
0
 def transform(self, node, results):
     syms = self.syms
     imports = results.get(u"imports")
     f = results.get(u"f")
     names = results.get(u"names")
     if imports:
         if imports.type == syms.import_as_name or not imports.children:
             children = [imports]
         else:
             children = imports.children
         for child in children[::2]:
             if isinstance(child, Node):
                 for kid in child.children:
                     if kid.value == u"filterfalse":
                         kid.changed()
                         kid.value = u"ifilterfalse"
                         break
             elif child.value == u"filterfalse":
                 child.changed()
                 child.value = u"ifilterfalse"
                 break
     elif names:
         for name in names:
             if is_probably_builtin(name):
                 name.value = u"i" + name.value
                 touch_import(u"itertools", name.value, node)
     elif f:
         f.changed()
         f.value = u"ifilterfalse"
Ejemplo n.º 2
0
 def transform(self, node, results):
     syms = self.syms
     imports = results.get("imports")
     f = results.get("f")
     names = results.get("names")
     if imports:
         if imports.type == syms.import_as_name or not imports.children:
             children = [imports]
         else:
             children = imports.children
         for child in children[::2]:
             if isinstance(child, Node):
                 for kid in child.children:
                     if kid.value == "filterfalse":
                         kid.changed()
                         kid.value = "ifilterfalse"
                         break
             elif child.value == "filterfalse":
                 child.changed()
                 child.value = "ifilterfalse"
                 break
     elif names:
         for name in names:
             if is_probably_builtin(name):
                 name.value = "i" + name.value
                 touch_import("itertools", name.value, node)
     elif f:
         f.changed()
         f.value = "ifilterfalse"
Ejemplo n.º 3
0
    def transform(self, node, results):
        if 'importname' in results:
            # Change the import from CONSTANT to get_constant:
            node = results['importname']
            node.value = 'get_constant'
            node.changed()

        if 'constant' in results or 'attribute' in results:
            if 'attribute' in results:
                # Here it's used as an attribute.
                node = results['attribute']
            else:
                # Here it's used standalone.
                node = results['constant']
                # Assert that it really is standalone and not
                # an attribute of something else, or an
                # assignment etc:
                if not is_probably_builtin(node):
                    return None

            # Now we replace the earlier constant name with the
            # new function call. If it was renamed on import
            # from 'CONSTANT' we keep the renaming else we
            # replace it with the new 'get_constant' name:
            name = node.value
            if name == 'CONSTANT':
                name = 'get_constant'
            node.replace(Call(Name(name), prefix=node.prefix))
Ejemplo n.º 4
0
 def transform(self, node, results):
     if 'importname' in results:
         # Change the import from CONSTANT to get_constant:
         node = results['importname']
         node.value = 'get_constant'
         node.changed()
         
     if 'constant' in results or 'attribute' in results:
         if 'attribute' in results:
             # Here it's used as an attribute.
             node = results['attribute']
         else:
             # Here it's used standalone.
             node = results['constant']
             # Assert that it really is standalone and not
             # an attribute of something else, or an
             # assignment etc:
             if not is_probably_builtin(node):
                 return None
             
         # Now we replace the earlier constant name with the
         # new function call. If it was renamed on import
         # from 'CONSTANT' we keep the renaming else we
         # replace it with the new 'get_constant' name:
         name = node.value
         if name == 'CONSTANT':
             name = 'get_constant'
         node.replace(Call(Name(name), prefix=node.prefix))
Ejemplo n.º 5
0
 def transform(self, node, results):
     if is_probably_builtin(node):
         arg1 = results['arg1']
         if 'arg2' not in results:
             is_kwarg_expansion = (arg1.type == self.syms.argument and
                                   arg1.children[0].value == '**')
             if arg1.type != self.syms.star_expr and not is_kwarg_expansion:
                 return
         touch_import('syx', 'hasattr', node)
         return node
Ejemplo n.º 6
0
 def transform(self, node, results):
     val = node.value
     if node.type == token.NUMBER and self.base(val) == 10:
         assert not val[-1] in "lL", "Invalid py3k literal: " + str(val)
         val += "L"
         return Number(val, prefix=node.prefix)
     elif is_probably_builtin(node):
         assert node.type == token.NAME, "Sanity check failed: " + str(val)
         new = self.static_long.clone()
         new.prefix = node.prefix
         return new
Ejemplo n.º 7
0
 def transform(self, node, results):
     val = node.value
     if node.type == token.NUMBER and self.base(val) == 10:
         assert not val[-1] in "lL", "Invalid py3k literal: " + str(val)
         val += "L"
         return Number(val, prefix=node.prefix)
     elif is_probably_builtin(node):
         assert node.type == token.NAME, "Sanity check failed: " + str(val)
         new = self.static_long.clone()
         new.prefix = node.prefix
         return new
Ejemplo n.º 8
0
def list_called(node):
    u"""
    Returns the power node that contains list as its first child if node
    is contained in a list() call, otherwise False.
    """
    parent = node.parent
    if parent is not None and parent.type == syms.trailer:
        prev = parent.prev_sibling
        if prev is not None and \
           prev.type == token.NAME and \
           prev.value == u"list" and \
           is_probably_builtin(prev):
            return prev.parent
    return False
Ejemplo n.º 9
0
def list_called(node):
    u"""
    Returns the power node that contains list as its first child if node
    is contained in a list() call, otherwise False.
    """
    parent = node.parent
    if parent is not None and parent.type == syms.trailer:
        prev = parent.prev_sibling
        if prev is not None and \
           prev.type == token.NAME and \
           prev.value == u"list" and \
           is_probably_builtin(prev):
            return prev.parent
    return False
Ejemplo n.º 10
0
 def transform(self, node, results):
     name = results[u"name"]
     if not is_probably_builtin(name):
         return
     list_call = list_called(node)
     if list_call:
         new_node = node.clone()
         new_node.prefix = list_call.prefix
         parent = list_call.parent
         i = list_call.remove()
         for after in list_call.children[2:]:
             new_node.append_child(after)
         parent.insert_child(i, new_node)
     else:
         name.replace(Name(u"xrange", prefix=name.prefix))
Ejemplo n.º 11
0
 def transform(self, node, results):
     name = results[u"name"]
     if not is_probably_builtin(name):
         return
     list_call = list_called(node)
     if list_call:
         new_node = node.clone()
         new_node.prefix = list_call.prefix
         parent = list_call.parent
         i = list_call.remove()
         for after in list_call.children[2:]:
             new_node.append_child(after)
         parent.insert_child(i, new_node)
     else:
         name.replace(Name(u"xrange", prefix=name.prefix))
Ejemplo n.º 12
0
 def transform(self, node, results):
     from_import = results.get("from_import")
     from_import_submod = results.get("from_import_submod")
     name_import = results.get("name_import")
     dotted_name = results.get("dotted_name")
     name = results.get("name")
     names = results.get("names")
     attr = results.get("attr")
     imported = results.get("imported")
     if names:
         for name in names:
             if name.type == token.NAME:
                 self.fix_simple_name(name)
             elif name.type == syms.dotted_as_name:
                 self.fix_simple_name(name.children[0]) if name.children[0].type == token.NAME else \
                 self.fix_dotted_name(name.children[0])
             elif name.type == syms.dotted_name:
                 self.fix_dotted_name(name)
     elif from_import_submod:
         renamed = results.get("renamed")
         new_name, new_attr = self.get_dotted_import_replacement(
             name, attr, renamed=renamed)
         if new_attr is not None:
             name.replace(new_name)
             attr.replace(new_attr)
         else:
             children = [Name("import"), new_name]
             node.replace(
                 Node(syms.import_name, children, prefix=node.prefix))
     elif dotted_name:
         self.fix_dotted_name(dotted_name)
     elif name_import or from_import:
         self.fix_simple_name(name)
     elif name and not attr:
         if does_tree_import(None, MAPPING[name.value], node) and \
            is_probably_builtin(name):
             self.fix_simple_name(name)
     elif name and attr:
         # Note that this will fix a dotted name that was never imported.  This will probably not matter.
         self.fix_dotted_name(node)
     elif imported and imported.type == syms.import_as_names:
         self.fix_submod_import(imported=imported.children,
                                node=node,
                                name=name.value)
Ejemplo n.º 13
0
 def transform(self, node, results):
     from_import = results.get("from_import")
     from_import_submod = results.get("from_import_submod")
     name_import = results.get("name_import")
     dotted_name = results.get("dotted_name")
     name = results.get("name")
     names = results.get("names")
     attr = results.get("attr")
     imported = results.get("imported")
     if names:
         for name in names:
             if name.type == token.NAME:
                 self.fix_simple_name(name)
             elif name.type == syms.dotted_as_name:
                 self.fix_simple_name(name.children[0]) if name.children[0].type == token.NAME else \
                 self.fix_dotted_name(name.children[0])
             elif name.type == syms.dotted_name:
                 self.fix_dotted_name(name)
     elif from_import_submod:
         renamed = results.get("renamed")
         new_name, new_attr = self.get_dotted_import_replacement(name, attr, renamed=renamed)
         if new_attr is not None:
             name.replace(new_name)
             attr.replace(new_attr)
         else:
             children = [Name("import"), new_name]
             node.replace(Node(syms.import_name, children, prefix=node.prefix))
     elif dotted_name:
         self.fix_dotted_name(dotted_name)
     elif name_import or from_import:
         self.fix_simple_name(name)
     elif name and not attr:
         if does_tree_import(None, MAPPING[name.value], node) and \
            is_probably_builtin(name):
             self.fix_simple_name(name)
     elif name and attr:
         # Note that this will fix a dotted name that was never imported.  This will probably not matter.
         self.fix_dotted_name(node)
     elif imported and imported.type == syms.import_as_names:
         self.fix_submod_import(imported=imported.children, node=node, name=name.value)
Ejemplo n.º 14
0
 def transform(self, node, results):
     if is_probably_builtin(node):
         node.value = u"int"
         node.changed()
Ejemplo n.º 15
0
 def match(self, node):
     if node.value == 'file' and is_probably_builtin(node):
         return True
     return False
Ejemplo n.º 16
0
 def transform(self, node, results):
     if is_probably_builtin(node):
         touch_import('syx', 'round', node)
         return node
Ejemplo n.º 17
0
 def transform(self, node, results):
     if is_probably_builtin(node):
         node.value = u"int"
         node.changed()
Ejemplo n.º 18
0
 def transform(self, node, results):
     if self.should_skip(node):
         return
     if is_probably_builtin(node):
         libmodernize.touch_import(u'six', u'unichr', node)