Back to index

python3.2  3.2.2
Functions | Variables
lib2to3.fixer_util Namespace Reference

Functions

def KeywordArg
 Common node-construction "macros".
def LParen
def RParen
def Assign
def Name
def Attr
def Comma
def Dot
def ArgList
def Call
def Newline
def BlankLine
def Number
def Subscript
def String
def ListComp
def FromImport
def is_tuple
 Determine whether a node represents a given literal.
def is_list
def parenthesize
 Misc.
def attr_chain
def in_special_context
def is_probably_builtin
def find_indentation
def make_suite
 The following functions are to find bindings in a suite.
def find_root
def does_tree_import
def is_import
def touch_import
def find_binding
def _find
def _is_import_binding

Variables

tuple consuming_calls
string p0
string p1
string p2
 pats_built = False
tuple _def_syms = set([syms.classdef, syms.funcdef])
tuple _block_syms = set([syms.funcdef, syms.classdef, syms.trailer])

Detailed Description

Utility functions, node construction macros, etc.

Function Documentation

def lib2to3.fixer_util._find (   name,
  node 
) [private]

Definition at line 383 of file fixer_util.py.

00383 
00384 def _find(name, node):
00385     nodes = [node]
00386     while nodes:
00387         node = nodes.pop()
00388         if node.type > 256 and node.type not in _block_syms:
00389             nodes.extend(node.children)
00390         elif node.type == token.NAME and node.value == name:
00391             return node
00392     return None

Here is the caller graph for this function:

def lib2to3.fixer_util._is_import_binding (   node,
  name,
  package = None 
) [private]
Will reuturn node if node will import name, or node
    will import * from package.  None is returned otherwise.
    See test cases for examples. 

Definition at line 393 of file fixer_util.py.

00393 
00394 def _is_import_binding(node, name, package=None):
00395     """ Will reuturn node if node will import name, or node
00396         will import * from package.  None is returned otherwise.
00397         See test cases for examples. """
00398 
00399     if node.type == syms.import_name and not package:
00400         imp = node.children[1]
00401         if imp.type == syms.dotted_as_names:
00402             for child in imp.children:
00403                 if child.type == syms.dotted_as_name:
00404                     if child.children[2].value == name:
00405                         return node
00406                 elif child.type == token.NAME and child.value == name:
00407                     return node
00408         elif imp.type == syms.dotted_as_name:
00409             last = imp.children[-1]
00410             if last.type == token.NAME and last.value == name:
00411                 return node
00412         elif imp.type == token.NAME and imp.value == name:
00413             return node
00414     elif node.type == syms.import_from:
00415         # str(...) is used to make life easier here, because
00416         # from a.b import parses to ['import', ['a', '.', 'b'], ...]
00417         if package and str(node.children[1]).strip() != package:
00418             return None
00419         n = node.children[3]
00420         if package and _find("as", n):
00421             # See test_from_import_as for explanation
00422             return None
00423         elif n.type == syms.import_as_names and _find(name, n):
00424             return node
00425         elif n.type == syms.import_as_name:
00426             child = n.children[2]
00427             if child.type == token.NAME and child.value == name:
00428                 return node
00429         elif n.type == token.NAME and n.value == name:
00430             return node
00431         elif package and n.type == token.STAR:
00432             return node
00433     return None

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_util.ArgList (   args,
  lparen = LParen(),
  rparen = RParen() 
)
A parenthesised argument list, used by Call()

Definition at line 54 of file fixer_util.py.

00054 
00055 def ArgList(args, lparen=LParen(), rparen=RParen()):
00056     """A parenthesised argument list, used by Call()"""
00057     node = Node(syms.trailer, [lparen.clone(), rparen.clone()])
00058     if args:
00059         node.insert_child(1, Node(syms.arglist, args))
00060     return node

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_util.Assign (   target,
  source 
)
Build an assignment statement

Definition at line 27 of file fixer_util.py.

00027 
00028 def Assign(target, source):
00029     """Build an assignment statement"""
00030     if not isinstance(target, list):
00031         target = [target]
00032     if not isinstance(source, list):
00033         source.prefix = " "
00034         source = [source]
00035 
00036     return Node(syms.atom,
00037                 target + [Leaf(token.EQUAL, "=", prefix=" ")] + source)

def lib2to3.fixer_util.Attr (   obj,
  attr 
)
A node tuple for obj.attr

Definition at line 42 of file fixer_util.py.

00042 
00043 def Attr(obj, attr):
00044     """A node tuple for obj.attr"""
00045     return [obj, Node(syms.trailer, [Dot(), attr])]

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_util.attr_chain (   obj,
  attr 
)
Follow an attribute chain.

If you have a chain of objects where a.foo -> b, b.foo-> c, etc,
use this to iterate over all objects in the chain. Iteration is
terminated by getattr(x, attr) is None.

Args:
    obj: the starting object
    attr: the name of the chaining attribute

Yields:
    Each successive object in the chain.

Definition at line 170 of file fixer_util.py.

00170 
00171 def attr_chain(obj, attr):
00172     """Follow an attribute chain.
00173 
00174     If you have a chain of objects where a.foo -> b, b.foo-> c, etc,
00175     use this to iterate over all objects in the chain. Iteration is
00176     terminated by getattr(x, attr) is None.
00177 
00178     Args:
00179         obj: the starting object
00180         attr: the name of the chaining attribute
00181 
00182     Yields:
00183         Each successive object in the chain.
00184     """
00185     next = getattr(obj, attr)
00186     while next:
00187         yield next
00188         next = getattr(next, attr)

Here is the call graph for this function:

Here is the caller graph for this function:

A blank line

Definition at line 72 of file fixer_util.py.

00072 
00073 def BlankLine():
00074     """A blank line"""
00075     return Leaf(token.NEWLINE, "")

Here is the caller graph for this function:

def lib2to3.fixer_util.Call (   func_name,
  args = None,
  prefix = None 
)
A function call

Definition at line 61 of file fixer_util.py.

00061 
00062 def Call(func_name, args=None, prefix=None):
00063     """A function call"""
00064     node = Node(syms.power, [func_name, ArgList(args)])
00065     if prefix is not None:
00066         node.prefix = prefix
00067     return node

Here is the call graph for this function:

A comma leaf

Definition at line 46 of file fixer_util.py.

00046 
00047 def Comma():
00048     """A comma leaf"""
00049     return Leaf(token.COMMA, ",")

Here is the caller graph for this function:

def lib2to3.fixer_util.does_tree_import (   package,
  name,
  node 
)
Returns true if name is imported from package at the
    top level of the tree which node belongs to.
    To cover the case of an import like 'import foo', use
    None for the package and 'foo' for the name. 

Definition at line 282 of file fixer_util.py.

00282 
00283 def does_tree_import(package, name, node):
00284     """ Returns true if name is imported from package at the
00285         top level of the tree which node belongs to.
00286         To cover the case of an import like 'import foo', use
00287         None for the package and 'foo' for the name. """
00288     binding = find_binding(name, find_root(node), package)
00289     return bool(binding)

Here is the call graph for this function:

Here is the caller graph for this function:

A period (.) leaf

Definition at line 50 of file fixer_util.py.

00050 
00051 def Dot():
00052     """A period (.) leaf"""
00053     return Leaf(token.DOT, ".")

Here is the caller graph for this function:

def lib2to3.fixer_util.find_binding (   name,
  node,
  package = None 
)
Returns the node which binds variable name, otherwise None.
    If optional argument package is supplied, only imports will
    be returned.
    See test cases for examples.

Definition at line 340 of file fixer_util.py.

00340 
00341 def find_binding(name, node, package=None):
00342     """ Returns the node which binds variable name, otherwise None.
00343         If optional argument package is supplied, only imports will
00344         be returned.
00345         See test cases for examples."""
00346     for child in node.children:
00347         ret = None
00348         if child.type == syms.for_stmt:
00349             if _find(name, child.children[1]):
00350                 return child
00351             n = find_binding(name, make_suite(child.children[-1]), package)
00352             if n: ret = n
00353         elif child.type in (syms.if_stmt, syms.while_stmt):
00354             n = find_binding(name, make_suite(child.children[-1]), package)
00355             if n: ret = n
00356         elif child.type == syms.try_stmt:
00357             n = find_binding(name, make_suite(child.children[2]), package)
00358             if n:
00359                 ret = n
00360             else:
00361                 for i, kid in enumerate(child.children[3:]):
00362                     if kid.type == token.COLON and kid.value == ":":
00363                         # i+3 is the colon, i+4 is the suite
00364                         n = find_binding(name, make_suite(child.children[i+4]), package)
00365                         if n: ret = n
00366         elif child.type in _def_syms and child.children[1].value == name:
00367             ret = child
00368         elif _is_import_binding(child, name, package):
00369             ret = child
00370         elif child.type == syms.simple_stmt:
00371             ret = find_binding(name, child, package)
00372         elif child.type == syms.expr_stmt:
00373             if _find(name, child.children[0]):
00374                 ret = child
00375 
00376         if ret:
00377             if not package:
00378                 return ret
00379             if is_import(ret):
00380                 return ret
00381     return None

Here is the call graph for this function:

Here is the caller graph for this function:

Find the indentation of *node*.

Definition at line 250 of file fixer_util.py.

00250 
00251 def find_indentation(node):
00252     """Find the indentation of *node*."""
00253     while node is not None:
00254         if node.type == syms.suite and len(node.children) > 2:
00255             indent = node.children[1]
00256             if indent.type == token.INDENT:
00257                 return indent.value
00258         node = node.parent
00259     return ""

Here is the caller graph for this function:

Find the top level namespace.

Definition at line 273 of file fixer_util.py.

00273 
00274 def find_root(node):
00275     """Find the top level namespace."""
00276     # Scamper up to the top level namespace
00277     while node.type != syms.file_input:
00278         assert node.parent, "Tree is insane! root found before "\
00279                            "file_input node was found."
00280         node = node.parent
00281     return node

Here is the caller graph for this function:

def lib2to3.fixer_util.FromImport (   package_name,
  name_leafs 
)
Return an import statement in the form:
    from package import name_leafs

Definition at line 113 of file fixer_util.py.

00113 
00114 def FromImport(package_name, name_leafs):
00115     """ Return an import statement in the form:
00116         from package import name_leafs"""
00117     # XXX: May not handle dotted imports properly (eg, package_name='foo.bar')
00118     #assert package_name == '.' or '.' not in package_name, "FromImport has "\
00119     #       "not been tested with dotted package names -- use at your own "\
00120     #       "peril!"
00121 
00122     for leaf in name_leafs:
00123         # Pull the leaves out of their old tree
00124         leaf.remove()
00125 
00126     children = [Leaf(token.NAME, "from"),
00127                 Leaf(token.NAME, package_name, prefix=" "),
00128                 Leaf(token.NAME, "import", prefix=" "),
00129                 Node(syms.import_as_names, name_leafs)]
00130     imp = Node(syms.import_from, children)
00131     return imp
00132 

Here is the caller graph for this function:

Returns true if node is in an environment where all that is required
    of it is being itterable (ie, it doesn't matter if it returns a list
    or an itterator).
    See test_map_nochange in test_fixers.py for some examples and tests.

Definition at line 208 of file fixer_util.py.

00208 
00209 def in_special_context(node):
00210     """ Returns true if node is in an environment where all that is required
00211         of it is being itterable (ie, it doesn't matter if it returns a list
00212         or an itterator).
00213         See test_map_nochange in test_fixers.py for some examples and tests.
00214         """
00215     global p0, p1, p2, pats_built
00216     if not pats_built:
00217         p1 = patcomp.compile_pattern(p1)
00218         p0 = patcomp.compile_pattern(p0)
00219         p2 = patcomp.compile_pattern(p2)
00220         pats_built = True
00221     patterns = [p0, p1, p2]
00222     for pattern, parent in zip(patterns, attr_chain(node, "parent")):
00223         results = {}
00224         if pattern.match(parent, results) and results["node"] is node:
00225             return True
00226     return False

Here is the call graph for this function:

Here is the caller graph for this function:

Returns true if the node is an import statement.

Definition at line 290 of file fixer_util.py.

00290 
00291 def is_import(node):
00292     """Returns true if the node is an import statement."""
00293     return node.type in (syms.import_name, syms.import_from)

Here is the caller graph for this function:

Does the node represent a list literal?

Definition at line 149 of file fixer_util.py.

00149 
00150 def is_list(node):
00151     """Does the node represent a list literal?"""
00152     return (isinstance(node, Node)
00153             and len(node.children) > 1
00154             and isinstance(node.children[0], Leaf)
00155             and isinstance(node.children[-1], Leaf)
00156             and node.children[0].value == "["
00157             and node.children[-1].value == "]")
00158 

Here is the caller graph for this function:

Check that something isn't an attribute or function name etc.

Definition at line 227 of file fixer_util.py.

00227 
00228 def is_probably_builtin(node):
00229     """
00230     Check that something isn't an attribute or function name etc.
00231     """
00232     prev = node.prev_sibling
00233     if prev is not None and prev.type == token.DOT:
00234         # Attribute lookup.
00235         return False
00236     parent = node.parent
00237     if parent.type in (syms.funcdef, syms.classdef):
00238         return False
00239     if parent.type == syms.expr_stmt and parent.children[0] is node:
00240         # Assignment.
00241         return False
00242     if parent.type == syms.parameters or \
00243             (parent.type == syms.typedargslist and (
00244             (prev is not None and prev.type == token.COMMA) or
00245             parent.children[0] is node
00246             )):
00247         # The name of an argument.
00248         return False
00249     return True

Here is the caller graph for this function:

Determine whether a node represents a given literal.

Does the node represent a tuple literal?

Definition at line 137 of file fixer_util.py.

00137 
00138 def is_tuple(node):
00139     """Does the node represent a tuple literal?"""
00140     if isinstance(node, Node) and node.children == [LParen(), RParen()]:
00141         return True
00142     return (isinstance(node, Node)
00143             and len(node.children) == 3
00144             and isinstance(node.children[0], Leaf)
00145             and isinstance(node.children[1], Node)
00146             and isinstance(node.children[2], Leaf)
00147             and node.children[0].value == "("
00148             and node.children[2].value == ")")

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.fixer_util.KeywordArg (   keyword,
  value 
)

Common node-construction "macros".

Definition at line 17 of file fixer_util.py.

00017 
00018 def KeywordArg(keyword, value):
00019     return Node(syms.argument,
00020                 [keyword, Leaf(token.EQUAL, "="), value])

def lib2to3.fixer_util.ListComp (   xp,
  fp,
  it,
  test = None 
)
A list comprehension of the form [xp for fp in it if test].

If test is None, the "if test" part is omitted.

Definition at line 89 of file fixer_util.py.

00089 
00090 def ListComp(xp, fp, it, test=None):
00091     """A list comprehension of the form [xp for fp in it if test].
00092 
00093     If test is None, the "if test" part is omitted.
00094     """
00095     xp.prefix = ""
00096     fp.prefix = " "
00097     it.prefix = " "
00098     for_leaf = Leaf(token.NAME, "for")
00099     for_leaf.prefix = " "
00100     in_leaf = Leaf(token.NAME, "in")
00101     in_leaf.prefix = " "
00102     inner_args = [for_leaf, fp, in_leaf, it]
00103     if test:
00104         test.prefix = " "
00105         if_leaf = Leaf(token.NAME, "if")
00106         if_leaf.prefix = " "
00107         inner_args.append(Node(syms.comp_if, [if_leaf, test]))
00108     inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
00109     return Node(syms.atom,
00110                        [Leaf(token.LBRACE, "["),
00111                         inner,
00112                         Leaf(token.RBRACE, "]")])

Definition at line 21 of file fixer_util.py.

00021 
00022 def LParen():
00023     return Leaf(token.LPAR, "(")

Here is the caller graph for this function:

The following functions are to find bindings in a suite.

Definition at line 264 of file fixer_util.py.

00264 
00265 def make_suite(node):
00266     if node.type == syms.suite:
00267         return node
00268     node = node.clone()
00269     parent, node.parent = node.parent, None
00270     suite = Node(syms.suite, [node])
00271     suite.parent = parent
00272     return suite

Here is the caller graph for this function:

def lib2to3.fixer_util.Name (   name,
  prefix = None 
)
Return a NAME leaf

Definition at line 38 of file fixer_util.py.

00038 
00039 def Name(name, prefix=None):
00040     """Return a NAME leaf"""
00041     return Leaf(token.NAME, name, prefix=prefix)

A newline literal

Definition at line 68 of file fixer_util.py.

00068 
00069 def Newline():
00070     """A newline literal"""
00071     return Leaf(token.NEWLINE, "\n")

Here is the caller graph for this function:

def lib2to3.fixer_util.Number (   n,
  prefix = None 
)

Definition at line 76 of file fixer_util.py.

00076 
00077 def Number(n, prefix=None):
00078     return Leaf(token.NUMBER, n, prefix=prefix)

Misc.

Definition at line 163 of file fixer_util.py.

00163 
00164 def parenthesize(node):
00165     return Node(syms.atom, [LParen(), node, RParen()])
00166 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 24 of file fixer_util.py.

00024 
00025 def RParen():
00026     return Leaf(token.RPAR, ")")

Here is the caller graph for this function:

def lib2to3.fixer_util.String (   string,
  prefix = None 
)
A string leaf

Definition at line 85 of file fixer_util.py.

00085 
00086 def String(string, prefix=None):
00087     """A string leaf"""
00088     return Leaf(token.STRING, string, prefix=prefix)

def lib2to3.fixer_util.Subscript (   index_node)
A numeric or string subscript

Definition at line 79 of file fixer_util.py.

00079 
00080 def Subscript(index_node):
00081     """A numeric or string subscript"""
00082     return Node(syms.trailer, [Leaf(token.LBRACE, "["),
00083                                index_node,
00084                                Leaf(token.RBRACE, "]")])

def lib2to3.fixer_util.touch_import (   package,
  name,
  node 
)
Works like `does_tree_import` but adds an import statement
    if it was not imported. 

Definition at line 294 of file fixer_util.py.

00294 
00295 def touch_import(package, name, node):
00296     """ Works like `does_tree_import` but adds an import statement
00297         if it was not imported. """
00298     def is_import_stmt(node):
00299         return (node.type == syms.simple_stmt and node.children and
00300                 is_import(node.children[0]))
00301 
00302     root = find_root(node)
00303 
00304     if does_tree_import(package, name, root):
00305         return
00306 
00307     # figure out where to insert the new import.  First try to find
00308     # the first import and then skip to the last one.
00309     insert_pos = offset = 0
00310     for idx, node in enumerate(root.children):
00311         if not is_import_stmt(node):
00312             continue
00313         for offset, node2 in enumerate(root.children[idx:]):
00314             if not is_import_stmt(node2):
00315                 break
00316         insert_pos = idx + offset
00317         break
00318 
00319     # if there are no imports where we can insert, find the docstring.
00320     # if that also fails, we stick to the beginning of the file
00321     if insert_pos == 0:
00322         for idx, node in enumerate(root.children):
00323             if (node.type == syms.simple_stmt and node.children and
00324                node.children[0].type == token.STRING):
00325                 insert_pos = idx + 1
00326                 break
00327 
00328     if package is None:
00329         import_ = Node(syms.import_name, [
00330             Leaf(token.NAME, "import"),
00331             Leaf(token.NAME, name, prefix=" ")
00332         ])
00333     else:
00334         import_ = FromImport(package, [Leaf(token.NAME, name, prefix=" ")])
00335 
00336     children = [import_, Newline()]
00337     root.insert_child(insert_pos, Node(syms.simple_stmt, children))
00338 

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 382 of file fixer_util.py.

Definition at line 339 of file fixer_util.py.

Initial value:
00001 set(["sorted", "list", "set", "any", "all", "tuple", "sum",
00002                        "min", "max"])

Definition at line 167 of file fixer_util.py.

Initial value:
00001 """for_stmt< 'for' any 'in' node=any ':' any* >
00002         | comp_for< 'for' any 'in' node=any any* >
00003      """

Definition at line 189 of file fixer_util.py.

Initial value:
00001 """
00002 power<
00003     ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' |
00004       'any' | 'all' | (any* trailer< '.' 'join' >) )
00005     trailer< '(' node=any ')' >
00006     any*
00007 >
00008 """

Definition at line 192 of file fixer_util.py.

Initial value:
00001 """
00002 power<
00003     'sorted'
00004     trailer< '(' arglist<node=any any*> ')' >
00005     any*
00006 >
00007 """

Definition at line 200 of file fixer_util.py.

Definition at line 207 of file fixer_util.py.