示例#1
0
 def transform(self, node, results):
     future_import(u"division", node)
     touch_import_top(u'past.utils', u'old_div', node)
     expr1, expr2 = results[0].clone(), results[1].clone()
     # Strip any leading space for the first number:
     expr1.prefix = u''
     return wrap_in_fn_call("old_div", expr1, expr2, prefix=node.prefix)
 def transform(self, node, results):
     future_import(u"division", node)
     touch_import_top(u'past.utils', u'old_div', node)
     expr1, expr2 = results[0].clone(), results[1].clone()
     # Strip any leading space for the first number:
     expr1.prefix = u''
     return wrap_in_fn_call("old_div", expr1, expr2, prefix=node.prefix)
示例#3
0
    def transform(self, node, results):
        head = results["head"]
        method = results["method"][0]  # Extract node for method name
        tail = results["tail"]
        syms = self.syms
        method_name = method.value
        isiter = method_name.startswith(u"iter")
        isview = method_name.startswith(u"view")
        head = [n.clone() for n in head]
        tail = [n.clone() for n in tail]
        # no changes neccessary if the call is in a special context
        special = not tail and self.in_special_context(node, isiter)
        new = pytree.Node(syms.power, head)
        new.prefix = u""
        if isiter or isview:
            # replace the method with the six function
            # e.g. d.iteritems() -> from six import iteritems\n iteritems(d)
            new = Call(Name(method_name), [new])
            touch_import_top('six', method_name, node)
        elif special:
            # it is not neccessary to change this case
            return node
        elif method_name in ("items", "values"):
            # ensure to return a list in python 3
            new = Call(Name(u"list" + method_name), [new])
            touch_import_top('future.utils', 'list' + method_name, node)
        else:
            # method_name is "keys"; removed it and cast the dict to list
            new = Call(Name(u"list"), [new])

        if tail:
            new = pytree.Node(syms.power, [new] + tail)
        new.prefix = node.prefix
        return new
示例#4
0
 def transform(self, node, results):
     # transform_member() in lib2to3/fixes/fix_urllib.py breaks node so find_root(node)
     # no longer works after the super() call below. So we find the root first:
     root = find_root(node)
     result = super(FixFutureStandardLibraryUrllib, self).transform(node, results)
     # TODO: add a blank line between any __future__ imports and this?
     touch_import_top(u'future', u'standard_library', root)
     return result
 def transform(self, node, results):
     # transform_member() in lib2to3/fixes/fix_urllib.py breaks node so find_root(node)
     # no longer works after the super() call below. So we find the root first:
     root = find_root(node)
     result = super(FixFutureStandardLibraryUrllib, self).transform(node, results)
     # TODO: add a blank line between any __future__ imports and this?
     touch_import_top(u'future', u'standard_library', root)
     return result
示例#6
0
 def transform(self, node, results):
     colon = results[u"colon"]
     idx = node.children.index(colon)
     if (node.children[idx-2].value == '(' and
         node.children[idx-1].value == ')'):
         del node.children[idx-2:idx]
         idx -= 2
     insert_object(node, idx)
     touch_import_top(u'builtins', 'object', node)
示例#7
0
 def transform(self, node, results):
     colon = results[u"colon"]
     idx = node.children.index(colon)
     if node.children[idx -
                      2].value == "(" and node.children[idx -
                                                        1].value == ")":
         del node.children[idx - 2:idx]
         idx -= 2
     insert_object(node, idx)
     touch_import_top(u"builtins", "object", node)
示例#8
0
 def transform(self, node, results):
     if node.type == token.STRING:
         touch_import_top(u'past.types', u'oldstr', node)
         if _literal_re.match(node.value):
             new = node.clone()
             # Strip any leading space or comments:
             # TODO: check: do we really want to do this?
             new.prefix = u''
             new.value = u'b' + new.value
             wrapped = wrap_in_fn_call("oldstr", [new], prefix=node.prefix)
             return wrapped
 def transform(self, node, results):
     if node.type == token.STRING:
         touch_import_top(u'past.types', u'oldstr', node)
         if _literal_re.match(node.value):
             new = node.clone()
             # Strip any leading space or comments:
             # TODO: check: do we really want to do this?
             new.prefix = u''
             new.value = u'b' + new.value
             wrapped = wrap_in_fn_call("oldstr", [new], prefix=node.prefix)
             return wrapped
示例#10
0
    def transform(self, node, results):
        if self.skip:
            return
        future_import(u"division", node)

        expr1, expr2 = results[0].clone(), results[1].clone()
        # Strip any leading space for the first number:
        expr1.prefix = u''
        # if expr1 or expr2 are obviously floats, we don't need to wrap in
        # old_div, as the behavior of division between any number and a float
        # should be the same in 2 or 3
        if _is_floaty(expr1) or _is_floaty(expr2):
            return
        touch_import_top(u'past.utils', u'old_div', node)
        return wrap_in_fn_call("old_div", (expr1, expr2), prefix=node.prefix)
示例#11
0
    def transform(self, node, results):
        import_mod = results.get("module_name")
        if import_mod:
            mod_name = import_mod.value
            if len(self.mapping[mod_name]) > 1:
                new_name1, new_name2 = map(str, self.mapping[mod_name])
                # import_mod.replace(Name(new_name, prefix=import_mod.prefix))
                children = [
                    Leaf(token.NAME, new_name1, prefix=u" "),
                    Leaf(token.NAME, u"as", prefix=u" "),
                    Leaf(token.NAME, new_name2, prefix=u" ")
                ]
                imp = Node(syms.dotted_as_name, children)
            else:
                new_name = self.mapping[mod_name][0]
                imp = Name(new_name, prefix=import_mod.prefix)
                new_name2 = new_name

            import_mod.replace(imp)

            if "name_import" in results:
                # If it's not a "from x import x, y" or "import x as y" import,
                # marked its usage to be replaced.
                # TODO: fix this so that each module is imported only once.
                self.replace[mod_name] = new_name2
            if "multiple_imports" in results:
                # This is a nasty hack to fix multiple imports on a line (e.g.,
                # "import StringIO, urlparse"). The problem is that I can't
                # figure out an easy way to make a pattern recognize the keys of
                # MAPPING randomly sprinkled in an import statement.
                results = self.match(node)
                if results:
                    self.transform(node, results)
        else:
            # Replace usage of the module.
            bare_name = results["bare_with_attr"][0]
            new_name = self.replace.get(bare_name.value)
            if new_name:
                bare_name.replace(Name(new_name, prefix=bare_name.prefix))
        touch_import_top(u'future', u'standard_library', node)
    def transform(self, node, results):
        import_mod = results.get("module_name")
        if import_mod:
            mod_name = import_mod.value
            if len(self.mapping[mod_name]) > 1:
                new_name1, new_name2 = map(str, self.mapping[mod_name])
                # import_mod.replace(Name(new_name, prefix=import_mod.prefix))
                children = [Leaf(token.NAME, new_name1, prefix=u" "),
                            Leaf(token.NAME, u"as", prefix=u" "),
                            Leaf(token.NAME, new_name2, prefix=u" ")]
                imp = Node(syms.dotted_as_name, children)
            else:
                new_name = self.mapping[mod_name][0]
                imp = Name(new_name, prefix=import_mod.prefix)
                new_name2 = new_name

            import_mod.replace(imp)

            if "name_import" in results:
                # If it's not a "from x import x, y" or "import x as y" import,
                # marked its usage to be replaced.
                # TODO: fix this so that each module is imported only once.
                self.replace[mod_name] = new_name2
            if "multiple_imports" in results:
                # This is a nasty hack to fix multiple imports on a line (e.g.,
                # "import StringIO, urlparse"). The problem is that I can't
                # figure out an easy way to make a pattern recognize the keys of
                # MAPPING randomly sprinkled in an import statement.
                results = self.match(node)
                if results:
                    self.transform(node, results)
        else:
            # Replace usage of the module.
            bare_name = results["bare_with_attr"][0]
            new_name = self.replace.get(bare_name.value)
            if new_name:
                bare_name.replace(Name(new_name, prefix=bare_name.prefix))
        touch_import_top(u'future', u'standard_library', node)
 def transform(self, node, results):
     if self.skip:
         return
     future_import(u"division", node)
     touch_import_top(u'past.utils', u'old_div', node)
     return wrap_in_fn_call("old_div", results, prefix=node.prefix)
示例#14
0
 def transform(self, node, results):
     result = super(FixXrangeWithImport, self).transform(node, results)
     touch_import_top('builtins', 'range', node)
     return result
示例#15
0
 def transform(self, node, results):
     touch_import_top(u'future', u'standard_library', node)
 def transform(self, node, results):
     # import_str = """(ascii, bytes, chr, dict, filter, hex, input,
     #                      int, list, map, next, object, oct, open, pow,
     #                      range, round, str, super, zip)"""
     touch_import_top(u'future.builtins', '*', node)
示例#17
0
 def transform(self, node, results):
     touch_import_top(u"future", u"standard_library", node)
 def transform(self, node, results):
     result = super(FixFutureStandardLibrary, self).transform(node, results)
     # TODO: add a blank line between any __future__ imports and this?
     touch_import_top(u'future', u'standard_library', node)
     return result
示例#19
0
 def transform(self, node, results):
     touch_import_top(u'builtins', 'object', node)
 def transform(self, node, results):
     if self.skip:
         return
     future_import(u"division", node)
     touch_import_top(u"past.utils", u"old_div", node)
     return wrap_in_fn_call("old_div", results, prefix=node.prefix)
示例#21
0
 def transform(self, node, results):
     touch_import_top(u'future.builtins', 'object', node)
示例#22
0
 def transform(self, node, results):
     name = results["name"]
     touch_import_top(u'builtins', name.value, node)
示例#23
0
 def transform(self, node, results):
     touch_import_top(u"future.builtins", "object", node)
示例#24
0
 def transform(self, node, results):
     result = super(FixXrangeWithImport, self).transform(node, results)
     touch_import_top("builtins", "range", node)
     return result
示例#25
0
 def transform(self, node, results):
     # import pdb; pdb.set_trace()
     name = results["name"]
     touch_import_top(u'future.builtins', name.value, node)
 def transform(self, node, results):
     # import_str = """(ascii, bytes, chr, dict, filter, hex, input,
     #                      int, list, map, next, object, oct, open, pow,
     #                      range, round, str, super, zip)"""
     touch_import_top(u'builtins', '*', node)
示例#27
0
 def transform(self, node, results):
     touch_import_top(u"builtins", "object", node)
示例#28
0
 def transform(self, node, results):
     # TODO: add a blank line between any __future__ imports and this?
     touch_import_top(u'future', u'standard_library', node)
 def transform(self, node, results):
     if self.skip:
         return
     future_import(u"division", node)
     touch_import_top(u'past.utils', u'old_div', node)
     return results
示例#30
0
 def transform(self, node, results):
     result = super(FixFutureStandardLibrary, self).transform(node, results)
     # TODO: add a blank line between any __future__ imports and this?
     touch_import_top(u'future', u'standard_library', node)
     return result
 def transform(self, node, results):
     if self.skip:
         return
     future_import(u"division", node)
     touch_import_top(u'past.utils', u'old_div', node)
     return results
 def transform(self, node, results):
     result = super(FixXrangeWithImport, self).transform(node, results)
     touch_import_top('future.builtins', 'range', node)
     return result
 def transform(self, node, results):
     # TODO: add a blank line between any __future__ imports and this?
     touch_import_top(u'future', u'standard_library', node)
 def transform(self, node, results):
     name = results["name"]
     touch_import_top(u'future.builtins', name.value, node)
 def transform(self, node, results):
     touch_import_top(u"past.builtins", "basestring", node)
示例#36
0
 def transform(self, node, results):
     touch_import_top(u'future', u'standard_library', node)
示例#37
0
    def transform(self, node, results):
        syms = self.syms

        exc = results["exc"].clone()
        if exc.type == token.STRING:
            msg = "Python 3 does not support string exceptions"
            self.cannot_convert(node, msg)
            return

        # Python 2 supports
        #  raise ((((E1, E2), E3), E4), E5), V
        # as a synonym for
        #  raise E1, V
        # Since Python 3 will not support this, we recurse down any tuple
        # literals, always taking the first element.
        if is_tuple(exc):
            while is_tuple(exc):
                # exc.children[1:-1] is the unparenthesized tuple
                # exc.children[1].children[0] is the first element of the tuple
                exc = exc.children[1].children[0].clone()
            exc.prefix = u" "

        if "tb" in results:
            tb = results["tb"].clone()
        else:
            tb = None

        if "val" in results:
            val = results["val"].clone()
            if is_tuple(val):
                # Assume that exc is a subclass of Exception and call exc(*val).
                args = [c.clone() for c in val.children[1:-1]]
                exc = Call(exc, args)
            elif val.type in (token.NUMBER, token.STRING):
                # Handle numeric and string literals specially, e.g.
                # "raise Exception, 5" -> "raise Exception(5)".
                val.prefix = u""
                exc = Call(exc, [val])
            elif val.type == token.NAME and val.value == u"None":
                # Handle None specially, e.g.
                # "raise Exception, None" -> "raise Exception".
                pass
            else:
                # val is some other expression. If val evaluates to an instance
                # of exc, it should just be raised. If val evaluates to None,
                # a default instance of exc should be raised (as above). If val
                # evaluates to a tuple, exc(*val) should be called (as
                # above). Otherwise, exc(val) should be called. We can only
                # tell what to do at runtime, so defer to future.utils.raise_(),
                # which handles all of these cases.
                touch_import_top(u"future.utils", u"raise_", node)
                exc.prefix = u""
                args = [exc, Comma(), val]
                if tb is not None:
                    args += [Comma(), tb]
                return Call(Name(u"raise_"), args)

        if tb is not None:
            tb.prefix = ""
            exc_list = Attr(exc, Name('with_traceback')) + [ArgList([tb])]
        else:
            exc_list = [exc]

        return pytree.Node(syms.raise_stmt, [Name(u"raise")] + exc_list,
                           prefix=node.prefix)
示例#38
0
 def transform(self, node, results):
     touch_import_top(u'builtins', 'zip', node)
示例#39
0
 def transform(self, node, results):
     touch_import_top(u'past.builtins', 'basestring', node)