summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/fixer_util.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/fixer_util.py')
-rw-r--r--Lib/lib2to3/fixer_util.py109
1 files changed, 44 insertions, 65 deletions
diff --git a/Lib/lib2to3/fixer_util.py b/Lib/lib2to3/fixer_util.py
index c2a3a47..78fdf26 100644
--- a/Lib/lib2to3/fixer_util.py
+++ b/Lib/lib2to3/fixer_util.py
@@ -1,6 +1,8 @@
"""Utility functions, node construction macros, etc."""
# Author: Collin Winter
+from itertools import islice
+
# Local imports
from .pgen2 import token
from .pytree import Leaf, Node
@@ -14,24 +16,24 @@ from . import patcomp
def KeywordArg(keyword, value):
return Node(syms.argument,
- [keyword, Leaf(token.EQUAL, "="), value])
+ [keyword, Leaf(token.EQUAL, u"="), value])
def LParen():
- return Leaf(token.LPAR, "(")
+ return Leaf(token.LPAR, u"(")
def RParen():
- return Leaf(token.RPAR, ")")
+ return Leaf(token.RPAR, u")")
def Assign(target, source):
"""Build an assignment statement"""
if not isinstance(target, list):
target = [target]
if not isinstance(source, list):
- source.prefix = " "
+ source.prefix = u" "
source = [source]
return Node(syms.atom,
- target + [Leaf(token.EQUAL, "=", prefix=" ")] + source)
+ target + [Leaf(token.EQUAL, u"=", prefix=u" ")] + source)
def Name(name, prefix=None):
"""Return a NAME leaf"""
@@ -43,11 +45,11 @@ def Attr(obj, attr):
def Comma():
"""A comma leaf"""
- return Leaf(token.COMMA, ",")
+ return Leaf(token.COMMA, u",")
def Dot():
"""A period (.) leaf"""
- return Leaf(token.DOT, ".")
+ return Leaf(token.DOT, u".")
def ArgList(args, lparen=LParen(), rparen=RParen()):
"""A parenthesised argument list, used by Call()"""
@@ -65,20 +67,20 @@ def Call(func_name, args=None, prefix=None):
def Newline():
"""A newline literal"""
- return Leaf(token.NEWLINE, "\n")
+ return Leaf(token.NEWLINE, u"\n")
def BlankLine():
"""A blank line"""
- return Leaf(token.NEWLINE, "")
+ return Leaf(token.NEWLINE, u"")
def Number(n, prefix=None):
return Leaf(token.NUMBER, n, prefix=prefix)
def Subscript(index_node):
"""A numeric or string subscript"""
- return Node(syms.trailer, [Leaf(token.LBRACE, "["),
+ return Node(syms.trailer, [Leaf(token.LBRACE, u"["),
index_node,
- Leaf(token.RBRACE, "]")])
+ Leaf(token.RBRACE, u"]")])
def String(string, prefix=None):
"""A string leaf"""
@@ -89,24 +91,24 @@ def ListComp(xp, fp, it, test=None):
If test is None, the "if test" part is omitted.
"""
- xp.prefix = ""
- fp.prefix = " "
- it.prefix = " "
- for_leaf = Leaf(token.NAME, "for")
- for_leaf.prefix = " "
- in_leaf = Leaf(token.NAME, "in")
- in_leaf.prefix = " "
+ xp.prefix = u""
+ fp.prefix = u" "
+ it.prefix = u" "
+ for_leaf = Leaf(token.NAME, u"for")
+ for_leaf.prefix = u" "
+ in_leaf = Leaf(token.NAME, u"in")
+ in_leaf.prefix = u" "
inner_args = [for_leaf, fp, in_leaf, it]
if test:
- test.prefix = " "
- if_leaf = Leaf(token.NAME, "if")
- if_leaf.prefix = " "
+ test.prefix = u" "
+ if_leaf = Leaf(token.NAME, u"if")
+ if_leaf.prefix = u" "
inner_args.append(Node(syms.comp_if, [if_leaf, test]))
inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
return Node(syms.atom,
- [Leaf(token.LBRACE, "["),
+ [Leaf(token.LBRACE, u"["),
inner,
- Leaf(token.RBRACE, "]")])
+ Leaf(token.RBRACE, u"]")])
def FromImport(package_name, name_leafs):
""" Return an import statement in the form:
@@ -120,36 +122,13 @@ def FromImport(package_name, name_leafs):
# Pull the leaves out of their old tree
leaf.remove()
- children = [Leaf(token.NAME, "from"),
- Leaf(token.NAME, package_name, prefix=" "),
- Leaf(token.NAME, "import", prefix=" "),
+ children = [Leaf(token.NAME, u"from"),
+ Leaf(token.NAME, package_name, prefix=u" "),
+ Leaf(token.NAME, u"import", prefix=u" "),
Node(syms.import_as_names, name_leafs)]
imp = Node(syms.import_from, children)
return imp
-def ImportAndCall(node, results, names):
- """Returns an import statement and calls a method
- of the module:
-
- import module
- module.name()"""
- obj = results["obj"].clone()
- if obj.type == syms.arglist:
- newarglist = obj.clone()
- else:
- newarglist = Node(syms.arglist, [obj.clone()])
- after = results["after"]
- if after:
- after = [n.clone() for n in after]
- new = Node(syms.power,
- Attr(Name(names[0]), Name(names[1])) +
- [Node(syms.trailer,
- [results["lpar"].clone(),
- newarglist,
- results["rpar"].clone()])] + after)
- new.prefix = node.prefix
- return new
-
###########################################################
### Determine whether a node represents a given literal
@@ -164,8 +143,8 @@ def is_tuple(node):
and isinstance(node.children[0], Leaf)
and isinstance(node.children[1], Node)
and isinstance(node.children[2], Leaf)
- and node.children[0].value == "("
- and node.children[2].value == ")")
+ and node.children[0].value == u"("
+ and node.children[2].value == u")")
def is_list(node):
"""Does the node represent a list literal?"""
@@ -173,8 +152,8 @@ def is_list(node):
and len(node.children) > 1
and isinstance(node.children[0], Leaf)
and isinstance(node.children[-1], Leaf)
- and node.children[0].value == "["
- and node.children[-1].value == "]")
+ and node.children[0].value == u"["
+ and node.children[-1].value == u"]")
###########################################################
@@ -185,8 +164,8 @@ def parenthesize(node):
return Node(syms.atom, [LParen(), node, RParen()])
-consuming_calls = {"sorted", "list", "set", "any", "all", "tuple", "sum",
- "min", "max", "enumerate"}
+consuming_calls = set(["sorted", "list", "set", "any", "all", "tuple", "sum",
+ "min", "max", "enumerate"])
def attr_chain(obj, attr):
"""Follow an attribute chain.
@@ -276,7 +255,7 @@ def find_indentation(node):
if indent.type == token.INDENT:
return indent.value
node = node.parent
- return ""
+ return u""
###########################################################
### The following functions are to find bindings in a suite
@@ -347,17 +326,17 @@ def touch_import(package, name, node):
if package is None:
import_ = Node(syms.import_name, [
- Leaf(token.NAME, "import"),
- Leaf(token.NAME, name, prefix=" ")
+ Leaf(token.NAME, u"import"),
+ Leaf(token.NAME, name, prefix=u" ")
])
else:
- import_ = FromImport(package, [Leaf(token.NAME, name, prefix=" ")])
+ import_ = FromImport(package, [Leaf(token.NAME, name, prefix=u" ")])
children = [import_, Newline()]
root.insert_child(insert_pos, Node(syms.simple_stmt, children))
-_def_syms = {syms.classdef, syms.funcdef}
+_def_syms = set([syms.classdef, syms.funcdef])
def find_binding(name, node, package=None):
""" Returns the node which binds variable name, otherwise None.
If optional argument package is supplied, only imports will
@@ -400,7 +379,7 @@ def find_binding(name, node, package=None):
return ret
return None
-_block_syms = {syms.funcdef, syms.classdef, syms.trailer}
+_block_syms = set([syms.funcdef, syms.classdef, syms.trailer])
def _find(name, node):
nodes = [node]
while nodes:
@@ -412,7 +391,7 @@ def _find(name, node):
return None
def _is_import_binding(node, name, package=None):
- """ Will return node if node will import name, or node
+ """ Will reuturn node if node will import name, or node
will import * from package. None is returned otherwise.
See test cases for examples. """
@@ -432,12 +411,12 @@ def _is_import_binding(node, name, package=None):
elif imp.type == token.NAME and imp.value == name:
return node
elif node.type == syms.import_from:
- # str(...) is used to make life easier here, because
+ # unicode(...) is used to make life easier here, because
# from a.b import parses to ['import', ['a', '.', 'b'], ...]
- if package and str(node.children[1]).strip() != package:
+ if package and unicode(node.children[1]).strip() != package:
return None
n = node.children[3]
- if package and _find("as", n):
+ if package and _find(u"as", n):
# See test_from_import_as for explanation
return None
elif n.type == syms.import_as_names and _find(name, n):