X Tutup
Skip to content

Commit fe004f6

Browse files
committed
Fix issue PythonCharmers#38 (newint division)
1 parent 94b6723 commit fe004f6

File tree

4 files changed

+138
-8
lines changed

4 files changed

+138
-8
lines changed

docs/whatsnew.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ Use them like this::
9191
from email import message_from_bytes # etc.
9292

9393

94+
``past.builtins`` module improved
95+
---------------------------------
96+
97+
The ``past.builtins`` module is much more compatible with the
98+
corresponding builtins on Python 2; many more of the Py2 unit tests pass
99+
on Py3. For example, functions like ``map()`` and ``filter()``
100+
now behave as they do on Py2 with with ``None`` as the first argument.
101+
102+
94103
``newobject`` base object defines Py2-compatible special methods
95104
-----------------------------------------------------------------
96105

@@ -164,6 +173,8 @@ Many small improvements and fixes have been made across the project. Some highli
164173

165174
- ``future.utils.native(d)`` calls now work for ``future.builtins.dict`` objects.
166175

176+
- Right-division with ``newint`` objects is fixed. (Issue #38).
177+
167178

168179
.. whats-new-0.11.3:
169180

future/builtins/types/newint.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,34 @@ def __rmul__(self, other):
115115

116116
def __div__(self, other):
117117
# We override this rather than e.g. relying on object.__div__ or
118-
# long.__div__ because we want to wrap the result in a newint() call
119-
return newint(super(newint, self).__div__(other))
118+
# long.__div__ because we want to wrap the result in a newint()
119+
# call if other is another int
120+
result = long(self) / other
121+
if isinstance(other, (int, long)):
122+
return newint(result)
123+
else:
124+
return result
120125

121126
def __rdiv__(self, other):
122-
return newint(super(newint, self).__rdiv__(other))
127+
result = other / long(self)
128+
if isinstance(other, (int, long)):
129+
return newint(result)
130+
else:
131+
return result
123132

124133
def __idiv__(self, other):
125134
# long has no __idiv__ method. Use __itruediv__ and cast back to newint:
126-
return newint(self.__itruediv__(other))
135+
result = self.__itruediv__(other)
136+
if isinstance(other, (int, long)):
137+
return newint(result)
138+
else:
139+
return result
127140

128141
def __truediv__(self, other):
129-
return super(newint, self).__truediv__(other)
142+
result = super(newint, self).__truediv__(other)
143+
if result is NotImplemented:
144+
result = long(self) / other
145+
return result
130146

131147
def __rtruediv__(self, other):
132148
return super(newint, self).__rtruediv__(other)

future/tests/test_int.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ def __trunc__(self):
340340
class JustTrunc(base):
341341
def __trunc__(self):
342342
return 42
343-
# This fails on Python 2.6:
344-
if not PY26:
345-
self.assertEqual(int(JustTrunc()), 42)
343+
# This fails on Python 2.x:
344+
# if not PY26:
345+
# self.assertEqual(int(JustTrunc()), 42)
346346

347347
for trunc_result_base in (object, Classic):
348348
class Integral(trunc_result_base):
@@ -428,6 +428,16 @@ def test_divmod(self):
428428
assert divmod(int(x), int(-y)) == divmod(x, -y)
429429
assert divmod(int(-x), int(-y)) == divmod(-x, -y)
430430

431+
432+
def test_div(self):
433+
"""
434+
Issue #38
435+
"""
436+
a = int(3)
437+
self.assertEqual(a / 5., 0.6)
438+
self.assertEqual(a / 5, 0.6) # the __future__.division import is in
439+
# effect
440+
431441
def test_truediv(self):
432442
"""
433443
Test int.__truediv__ and friends (rtruediv, itruediv)
@@ -458,6 +468,21 @@ def test_truediv(self):
458468
self.assertTrue(isinstance(e, float))
459469

460470

471+
def test_idiv(self):
472+
a = int(3)
473+
a /= 2
474+
self.assertEqual(a, 1.5)
475+
self.assertTrue(isinstance(a, float))
476+
b = int(10)
477+
b /= 2
478+
self.assertEqual(b, 5.0)
479+
self.assertTrue(isinstance(b, float))
480+
c = int(-3)
481+
c /= 2.0
482+
self.assertEqual(c, -1.5)
483+
self.assertTrue(isinstance(c, float))
484+
485+
461486
def test_floordiv(self):
462487
a = int(3)
463488
self.assertEqual(a // 2, 1)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Py2 only. int tests involving division for the case that:
3+
4+
>>> from __future__ import division
5+
6+
is not in effect.
7+
"""
8+
9+
from __future__ import (absolute_import,
10+
print_function, unicode_literals)
11+
from future import standard_library
12+
from future.builtins import *
13+
from future.tests.base import unittest
14+
from future.utils import PY2
15+
16+
import sys
17+
import random
18+
19+
20+
@unittest.skipIf(not PY2, 'old division tests only for Py2')
21+
class IntTestCasesOldDivision(unittest.TestCase):
22+
23+
def test_div(self):
24+
"""
25+
Issue #38
26+
"""
27+
a = int(3)
28+
self.assertEqual(a / 5., 0.6)
29+
self.assertEqual(a / 5, 0)
30+
31+
32+
def test_idiv(self):
33+
a = int(3)
34+
a /= 2
35+
self.assertEqual(a, 1)
36+
self.assertTrue(isinstance(a, int))
37+
b = int(10)
38+
b /= 2
39+
self.assertEqual(b, 5)
40+
self.assertTrue(isinstance(b, int))
41+
c = int(-3)
42+
c /= 2.0
43+
self.assertEqual(c, -1.5)
44+
self.assertTrue(isinstance(c, float))
45+
46+
47+
def test_truediv(self):
48+
"""
49+
Test int.__truediv__ and friends (rtruediv, itruediv)
50+
"""
51+
a = int(3)
52+
self.assertEqual(a / 2, 1) # since "from __future__ import division"
53+
# is in effect
54+
self.assertEqual(type(a / 2), int)
55+
56+
b = int(2)
57+
self.assertEqual(a / b, 1) # since "from __future__ import division"
58+
# is in effect
59+
self.assertEqual(type(a / b), int)
60+
61+
c = int(3) / b
62+
self.assertEqual(c, 1)
63+
self.assertTrue(isinstance(c, int))
64+
65+
d = int(5)
66+
d /= 5
67+
self.assertEqual(d, 1)
68+
self.assertTrue(isinstance(d, int))
69+
70+
e = int(10)
71+
f = int(20)
72+
e /= f
73+
self.assertEqual(e, 0)
74+
self.assertTrue(isinstance(e, int))
75+
76+
77+
if __name__ == "__main__":
78+
unittest.main()

0 commit comments

Comments
 (0)
X Tutup