Skip to content

Commit 3d6c5ed

Browse files
committed
Improve division_safe fixer
* Now it does nothing if there's an existing __future__.division import
1 parent a9490b9 commit 3d6c5ed

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

future/tests/test_futurize.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,15 @@ def test_shebang_comment(self):
8484
# and more comments
8585
8686
import math
87-
1 / 5
8887
print 'Hello!'
8988
"""
9089
after = """
9190
#!/usr/bin/env python
9291
# some comments
9392
# and more comments
94-
from __future__ import division
9593
from __future__ import print_function
9694
9795
import math
98-
1 / 5
9996
print('Hello!')
10097
"""
10198
self.convert_check(before, after)
@@ -111,18 +108,15 @@ def test_shebang_docstring(self):
111108
a doc string
112109
"""
113110
import math
114-
1 / 5
115111
print 'Hello!'
116112
'''
117113
after = '''
118114
#!/usr/bin/env python
119115
"""
120116
a doc string
121117
"""
122-
from __future__ import division
123118
from __future__ import print_function
124119
import math
125-
1 / 5
126120
print('Hello!')
127121
'''
128122
self.convert_check(before, after)

libfuturize/fixes/fix_division_safe.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
at the top and changes any old-style divisions to be calls to
99
past.utils.old_div so the code runs as before on Py2.6/2.7 and has the same
1010
behaviour on Py3.
11+
12+
If "from __future__ import division" is already in effect, this fixer does
13+
nothing.
1114
"""
1215

1316
from lib2to3 import fixer_base
14-
from lib2to3.fixer_util import syms
15-
from libfuturize.fixer_util import token, future_import, touch_import_top
17+
from lib2to3.fixer_util import syms, does_tree_import
18+
from libfuturize.fixer_util import (token, future_import, touch_import_top,
19+
wrap_in_fn_call)
1620

1721

1822
def match_division(node):
@@ -35,10 +39,17 @@ class FixDivisionSafe(fixer_base.BaseFix):
3539
term<(not('/') any)+ '/' ((not('/') any))>
3640
"""
3741

42+
def start_tree(self, tree, name):
43+
"""
44+
Skip this fixer if "__future__.division" is already imported.
45+
"""
46+
super(FixDivisionSafe, self).start_tree(tree, name)
47+
self.skip = "division" in tree.future_features
48+
3849
def match(self, node):
3950
u"""
4051
Since the tree needs to be fixed once and only once if and only if it
41-
matches, then we can start discarding matches after we make the first.
52+
matches, we can start discarding matches after the first.
4253
"""
4354
if (node.type == self.syms.term and
4455
len(node.children) == 3 and
@@ -49,7 +60,10 @@ def match(self, node):
4960
return False
5061

5162
def transform(self, node, results):
63+
if self.skip:
64+
return
5265
future_import(u"division", node)
66+
5367
touch_import_top(u'past.utils', u'old_div', node)
5468
expr1, expr2 = results[0].clone(), results[1].clone()
5569
# Strip any leading space for the first number:

0 commit comments

Comments
 (0)