]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/fixes/fix_set_literal.py
2 Optional fixer to transform set() calls to set literals.
5 # Author: Benjamin Peterson
7 from lib2to3
import fixer_base
, pytree
8 from lib2to3
.fixer_util
import token
, syms
12 class FixSetLiteral(fixer_base
.BaseFix
):
17 PATTERN
= """power< 'set' trailer< '('
18 (atom=atom< '[' (items=listmaker< any ((',' any)* [',']) >
22 atom< '(' items=testlist_gexp< any ((',' any)* [',']) > ')' >
27 def transform(self
, node
, results
):
28 single
= results
.get("single")
30 # Make a fake listmaker
31 fake
= pytree
.Node(syms
.listmaker
, [single
.clone()])
35 items
= results
["items"]
37 # Build the contents of the literal
38 literal
= [pytree
.Leaf(token
.LBRACE
, u
"{")]
39 literal
.extend(n
.clone() for n
in items
.children
)
40 literal
.append(pytree
.Leaf(token
.RBRACE
, u
"}"))
41 # Set the prefix of the right brace to that of the ')' or ']'
42 literal
[-1].prefix
= items
.next_sibling
.prefix
43 maker
= pytree
.Node(syms
.dictsetmaker
, literal
)
44 maker
.prefix
= node
.prefix
46 # If the original was a one tuple, we need to remove the extra comma.
47 if len(maker
.children
) == 4:
50 maker
.children
[-1].prefix
= n
.prefix
52 # Finally, replace the set call with our shiny new literal.