Skip to content

Commit 3c44a1e

Browse files
authored
Merge pull request #386 from jmadler/master
Fix bugs introduced in #368
2 parents 8b78b5e + 2231ae1 commit 3c44a1e

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/libfuturize/fixes/fix_division_safe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def start_tree(self, tree, name):
8484
Skip this fixer if "__future__.division" is already imported.
8585
"""
8686
super(FixDivisionSafe, self).start_tree(tree, name)
87-
self.skip = "division" not in tree.future_features
87+
self.skip = "division" in tree.future_features
8888

8989
def match(self, node):
9090
u"""

tests/test_future/test_futurize.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,49 +1182,73 @@ def test_safe_division(self):
11821182
"""
11831183
before = """
11841184
import random
1185+
class fraction(object):
1186+
numer = 0
1187+
denom = 0
1188+
def __init__(self, numer, denom):
1189+
self.numer = numer
1190+
self.denom = denom
1191+
1192+
def total_count(self):
1193+
return self.numer * 50
1194+
11851195
x = 3 / 2
11861196
y = 3. / 2
11871197
foo = range(100)
11881198
assert x == 1 and isinstance(x, int)
11891199
assert y == 1.5 and isinstance(y, float)
11901200
a = 1 + foo[len(foo) / 2]
11911201
b = 1 + foo[len(foo) * 3 / 4]
1192-
assert a == 50
1193-
assert b == 75
1202+
assert a == 51
1203+
assert b == 76
11941204
r = random.randint(0, 1000) * 1.0 / 1000
11951205
output = { "SUCCESS": 5, "TOTAL": 10 }
11961206
output["SUCCESS"] * 100 / output["TOTAL"]
1197-
obj = foo
1207+
obj = fraction(1, 50)
11981208
val = float(obj.numer) / obj.denom * 1e-9
1199-
mount.bytes_free * mount.free_size / bytes_in_gb
1200-
obj.total_count() * threshold / 100
1201-
100 * abs(obj.width - original_width) / float(max(obj.width, original_width))
1202-
100 * abs(obj.width - original_width) / max(obj.width, original_width)
1203-
float(target_width) * float(original_height) / float(original_width)
1209+
obj.numer * obj.denom / val
1210+
obj.total_count() * val / 100
1211+
original_numer = 1
1212+
original_denom = 50
1213+
100 * abs(obj.numer - original_numer) / float(max(obj.denom, original_denom))
1214+
100 * abs(obj.numer - original_numer) / max(obj.denom, original_denom)
1215+
float(original_numer) * float(original_denom) / float(obj.numer)
12041216
"""
12051217
after = """
12061218
from __future__ import division
12071219
from past.utils import old_div
12081220
import random
1221+
class fraction(object):
1222+
numer = 0
1223+
denom = 0
1224+
def __init__(self, numer, denom):
1225+
self.numer = numer
1226+
self.denom = denom
1227+
1228+
def total_count(self):
1229+
return self.numer * 50
1230+
12091231
x = old_div(3, 2)
12101232
y = 3. / 2
1211-
foo = range(100)
1233+
foo = list(range(100))
12121234
assert x == 1 and isinstance(x, int)
12131235
assert y == 1.5 and isinstance(y, float)
12141236
a = 1 + foo[old_div(len(foo), 2)]
12151237
b = 1 + foo[old_div(len(foo) * 3, 4)]
1216-
assert a == 50
1217-
assert b == 75
1238+
assert a == 51
1239+
assert b == 76
12181240
r = old_div(random.randint(0, 1000) * 1.0, 1000)
12191241
output = { "SUCCESS": 5, "TOTAL": 10 }
1220-
output["SUCCESS"] * 100 / output["TOTAL"]
1221-
obj = foo
1242+
old_div(output["SUCCESS"] * 100, output["TOTAL"])
1243+
obj = fraction(1, 50)
12221244
val = float(obj.numer) / obj.denom * 1e-9
1223-
old_div(mount.bytes_free * mount.free_size, bytes_in_gb)
1224-
old_div(obj.total_count() * threshold, 100)
1225-
100 * abs(obj.width - original_width) / float(max(obj.width, original_width))
1226-
100 * abs(obj.width - original_width), max(obj.width, original_width))
1227-
float(target_width) * float(original_height) / float(original_width)
1245+
old_div(obj.numer * obj.denom, val)
1246+
old_div(obj.total_count() * val, 100)
1247+
original_numer = 1
1248+
original_denom = 50
1249+
100 * abs(obj.numer - original_numer) / float(max(obj.denom, original_denom))
1250+
old_div(100 * abs(obj.numer - original_numer), max(obj.denom, original_denom))
1251+
float(original_numer) * float(original_denom) / float(obj.numer)
12281252
"""
12291253
self.convert_check(before, after)
12301254

0 commit comments

Comments
 (0)