]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/fixes/fix_map.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / lib2to3 / fixes / fix_map.py
CommitLineData
4710c53d 1# Copyright 2007 Google, Inc. All Rights Reserved.\r
2# Licensed to PSF under a Contributor Agreement.\r
3\r
4"""Fixer that changes map(F, ...) into list(map(F, ...)) unless there\r
5exists a 'from future_builtins import map' statement in the top-level\r
6namespace.\r
7\r
8As a special case, map(None, X) is changed into list(X). (This is\r
9necessary because the semantics are changed in this case -- the new\r
10map(None, X) is equivalent to [(x,) for x in X].)\r
11\r
12We avoid the transformation (except for the special case mentioned\r
13above) if the map() call is directly contained in iter(<>), list(<>),\r
14tuple(<>), sorted(<>), ...join(<>), or for V in <>:.\r
15\r
16NOTE: This is still not correct if the original code was depending on\r
17map(F, X, Y, ...) to go on until the longest argument is exhausted,\r
18substituting None for missing values -- like zip(), it now stops as\r
19soon as the shortest argument is exhausted.\r
20"""\r
21\r
22# Local imports\r
23from ..pgen2 import token\r
24from .. import fixer_base\r
25from ..fixer_util import Name, Call, ListComp, in_special_context\r
26from ..pygram import python_symbols as syms\r
27\r
28class FixMap(fixer_base.ConditionalFix):\r
29 BM_compatible = True\r
30\r
31 PATTERN = """\r
32 map_none=power<\r
33 'map'\r
34 trailer< '(' arglist< 'None' ',' arg=any [','] > ')' >\r
35 >\r
36 |\r
37 map_lambda=power<\r
38 'map'\r
39 trailer<\r
40 '('\r
41 arglist<\r
42 lambdef< 'lambda'\r
43 (fp=NAME | vfpdef< '(' fp=NAME ')'> ) ':' xp=any\r
44 >\r
45 ','\r
46 it=any\r
47 >\r
48 ')'\r
49 >\r
50 >\r
51 |\r
52 power<\r
53 'map' trailer< '(' [arglist=any] ')' >\r
54 >\r
55 """\r
56\r
57 skip_on = 'future_builtins.map'\r
58\r
59 def transform(self, node, results):\r
60 if self.should_skip(node):\r
61 return\r
62\r
63 if node.parent.type == syms.simple_stmt:\r
64 self.warning(node, "You should use a for loop here")\r
65 new = node.clone()\r
66 new.prefix = u""\r
67 new = Call(Name(u"list"), [new])\r
68 elif "map_lambda" in results:\r
69 new = ListComp(results["xp"].clone(),\r
70 results["fp"].clone(),\r
71 results["it"].clone())\r
72 else:\r
73 if "map_none" in results:\r
74 new = results["arg"].clone()\r
75 else:\r
76 if "arglist" in results:\r
77 args = results["arglist"]\r
78 if args.type == syms.arglist and \\r
79 args.children[0].type == token.NAME and \\r
80 args.children[0].value == "None":\r
81 self.warning(node, "cannot convert map(None, ...) "\r
82 "with multiple arguments because map() "\r
83 "now truncates to the shortest sequence")\r
84 return\r
85 if in_special_context(node):\r
86 return None\r
87 new = node.clone()\r
88 new.prefix = u""\r
89 new = Call(Name(u"list"), [new])\r
90 new.prefix = node.prefix\r
91 return new\r