Skip to content

Commit 42e42f7

Browse files
committed
2to3: avoid duplicate transformation of print (followup c4d9b0a)
1 parent 4f6c271 commit 42e42f7

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
from libfuturize.fixes.fix_print_with_import import FixPrintWithImport as FixPrintWithImportOrig
2-
from lib2to3.fixer_util import Node, Leaf, syms
2+
from lib2to3.fixer_util import Node, Leaf, syms, find_indentation
33

4+
import re
45

56
class FixPrintWithImport(FixPrintWithImportOrig):
67

7-
def match(self, node):
8-
isPrint = super(FixPrintWithImport, self).match(node)
9-
if isPrint and \
10-
len(node.children) == 2 and \
11-
isinstance(node.children[0], Leaf) and \
12-
isinstance(node.children[1], Node) and node.children[1].type == syms.atom and \
13-
isinstance(node.children[1].children[0], Leaf) and node.children[1].children[0].value == '(' and \
14-
isinstance(node.children[1].children[-1], Leaf) and node.children[1].children[-1].value == ')':
15-
return False
8+
def transform(self, node, results):
9+
if "fix_print_with_import" in node.prefix:
10+
return node
1611

17-
return ok
12+
r = super(FixPrintWithImport, self).transform(node, results)
13+
14+
if not r or r == node:
15+
return r
16+
17+
if not r.prefix:
18+
indentation = find_indentation(node)
19+
r.prefix = "# fix_print_with_import\n" + indentation
20+
else:
21+
r.prefix = re.sub('([ \t]*$)', r'\1# fix_print_with_import\n\1', r.prefix)
22+
23+
return r

0 commit comments

Comments
 (0)