|
3 | 3 |
|
4 | 4 | # Local imports
|
5 | 5 | from lib2to3 import fixer_util
|
| 6 | +from lib2to3 import pytree |
6 | 7 | from lib2to3.fixes import fix_dict
|
7 | 8 | import libmodernize
|
8 | 9 |
|
9 | 10 |
|
10 | 11 | class FixDictSix(fix_dict.FixDict):
|
11 | 12 |
|
12 |
| - def transform_iter(self, method_name, node, base): |
| 13 | + def transform_iter(self, node, results): |
13 | 14 | """Call six.(iter|view)items() and friends."""
|
| 15 | + # Make sure six is imported. |
14 | 16 | libmodernize.touch_import(None, u'six', node)
|
15 |
| - new_node = [n.clone() for n in base] |
16 |
| - new_node[0].prefix = u'' |
| 17 | + |
| 18 | + # Copy of self.transform() from lib2to3.fix_dict with some changes to |
| 19 | + # use the six.* methods. |
| 20 | + |
| 21 | + head = results['head'] |
| 22 | + method = results['method'][0] # Extract node for method name |
| 23 | + tail = results['tail'] |
| 24 | + syms = self.syms |
| 25 | + method_name = method.value |
17 | 26 | name = fixer_util.Name(u'six.' + method_name, prefix=node.prefix)
|
18 |
| - node.replace(fixer_util.Call(name, new_node)) |
| 27 | + assert method_name.startswith((u'iter', u'view')), repr(method) |
| 28 | + assert method_name[4:] in (u'keys', u'items', u'values'), repr(method) |
| 29 | + head = [n.clone() for n in head] |
| 30 | + tail = [n.clone() for n in tail] |
| 31 | + new = pytree.Node(syms.power, head) |
| 32 | + new.prefix = u'' |
| 33 | + new = fixer_util.Call(name, [new]) |
| 34 | + if tail: |
| 35 | + new = pytree.Node(syms.power, [new] + tail) |
| 36 | + new.prefix = node.prefix |
| 37 | + return new |
19 | 38 |
|
20 | 39 | def transform(self, node, results):
|
21 | 40 | method = results['method'][0]
|
22 | 41 | method_name = method.value
|
23 | 42 | if method_name in ('keys', 'items', 'values'):
|
24 | 43 | return super(FixDictSix, self).transform(node, results)
|
25 | 44 | else:
|
26 |
| - return self.transform_iter(method_name, node, results['head']) |
| 45 | + return self.transform_iter(node, results) |
27 | 46 |
|
28 | 47 | def in_special_context(self, node, isiter):
|
29 | 48 | # Redefined from parent class to make "for x in d.items()" count as
|
|
0 commit comments