symbian-qemu-0.9.1-12/python-win32-2.6.1/lib/lib2to3/fixes/fix_apply.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 # Copyright 2006 Google, Inc. All Rights Reserved.
       
     2 # Licensed to PSF under a Contributor Agreement.
       
     3 
       
     4 """Fixer for apply().
       
     5 
       
     6 This converts apply(func, v, k) into (func)(*v, **k)."""
       
     7 
       
     8 # Local imports
       
     9 from .. import pytree
       
    10 from ..pgen2 import token
       
    11 from .. import fixer_base
       
    12 from ..fixer_util import Call, Comma
       
    13 
       
    14 class FixApply(fixer_base.BaseFix):
       
    15 
       
    16     PATTERN = """
       
    17     power< 'apply'
       
    18         trailer<
       
    19             '('
       
    20             arglist<
       
    21                 (not argument<NAME '=' any>) func=any ','
       
    22                 (not argument<NAME '=' any>) args=any [','
       
    23                 (not argument<NAME '=' any>) kwds=any] [',']
       
    24             >
       
    25             ')'
       
    26         >
       
    27     >
       
    28     """
       
    29 
       
    30     def transform(self, node, results):
       
    31         syms = self.syms
       
    32         assert results
       
    33         func = results["func"]
       
    34         args = results["args"]
       
    35         kwds = results.get("kwds")
       
    36         prefix = node.get_prefix()
       
    37         func = func.clone()
       
    38         if (func.type not in (token.NAME, syms.atom) and
       
    39             (func.type != syms.power or
       
    40              func.children[-2].type == token.DOUBLESTAR)):
       
    41             # Need to parenthesize
       
    42             func = self.parenthesize(func)
       
    43         func.set_prefix("")
       
    44         args = args.clone()
       
    45         args.set_prefix("")
       
    46         if kwds is not None:
       
    47             kwds = kwds.clone()
       
    48             kwds.set_prefix("")
       
    49         l_newargs = [pytree.Leaf(token.STAR, "*"), args]
       
    50         if kwds is not None:
       
    51             l_newargs.extend([Comma(),
       
    52                               pytree.Leaf(token.DOUBLESTAR, "**"),
       
    53                               kwds])
       
    54             l_newargs[-2].set_prefix(" ") # that's the ** token
       
    55         # XXX Sometimes we could be cleverer, e.g. apply(f, (x, y) + t)
       
    56         # can be translated into f(x, y, *t) instead of f(*(x, y) + t)
       
    57         #new = pytree.Node(syms.power, (func, ArgList(l_newargs)))
       
    58         return Call(func, l_newargs, prefix=prefix)