Skip to content

Commit 6f92703

Browse files
committed
Merge branch 'krischer-master'
2 parents e1dd12c + d15618c commit 6f92703

File tree

2 files changed

+138
-41
lines changed

2 files changed

+138
-41
lines changed

future/tests/test_int.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,13 @@ def test_upcasting_to_floats(self):
655655
self.assertTrue(isinstance(a % 0.5, float))
656656
self.assertTrue(isinstance(0.5 % a, float))
657657

658+
# Power with floats.
659+
self.assertEqual(1.0 ** a, 1.0)
660+
self.assertTrue(isinstance(1.0 ** a, float))
661+
662+
self.assertEqual(a ** 1.0, a)
663+
self.assertTrue(isinstance(a ** 1.0, float))
664+
658665
def test_upcasting_to_complex(self):
659666
"""
660667
Integers should automatically be upcasted to complex numbers for
@@ -689,6 +696,87 @@ def test_upcasting_to_complex(self):
689696
self.assertTrue(isinstance(a / 0.5j, complex))
690697
self.assertTrue(isinstance(0.5j / a, complex))
691698

699+
# Power with floats.
700+
self.assertEqual(5.0j ** int(1), 5.0j)
701+
self.assertTrue(isinstance(5.0j ** int(1), complex))
702+
703+
self.assertEqual(a ** 1.0j, 3.0 ** 1.0j)
704+
self.assertTrue(isinstance(a ** 1.0j, complex))
705+
706+
def test_more_arithmetics(self):
707+
"""
708+
More arithmetic tests to improve test coverage.
709+
"""
710+
a = int(3)
711+
b = int(5)
712+
c = int(-5)
713+
714+
self.assertEqual(b - a, 2)
715+
self.assertTrue(isinstance(b - a, int))
716+
717+
self.assertEqual(a * b, 15)
718+
self.assertTrue(isinstance(a * b, int))
719+
720+
self.assertEqual(b % a, 2)
721+
self.assertTrue(isinstance(b % a, int))
722+
723+
self.assertEqual(a ** b, 243)
724+
self.assertTrue(isinstance(a ** b, int))
725+
726+
self.assertEqual(abs(c), 5)
727+
self.assertEqual(abs(c), b)
728+
self.assertTrue(isinstance(abs(c), int))
729+
730+
def test_bitwise_operations(self):
731+
"""
732+
Tests bitwise operations.
733+
"""
734+
a = int(3)
735+
b = int(1)
736+
737+
self.assertEqual(a >> b, 1)
738+
self.assertEqual(a >> 1, 1)
739+
self.assertTrue(isinstance(a >> b, int))
740+
self.assertTrue(isinstance(a >> 1, int))
741+
742+
self.assertEqual(a << b, 6)
743+
self.assertEqual(a << 1, 6)
744+
self.assertTrue(isinstance(a << b, int))
745+
self.assertTrue(isinstance(a << 1, int))
746+
747+
self.assertEqual(a & b, 1)
748+
self.assertEqual(a & 1, 1)
749+
self.assertTrue(isinstance(a & b, int))
750+
self.assertTrue(isinstance(a & 1, int))
751+
752+
self.assertEqual(a | b, 3)
753+
self.assertEqual(a | 1, 3)
754+
self.assertTrue(isinstance(a | b, int))
755+
self.assertTrue(isinstance(a | 1, int))
756+
757+
self.assertEqual(a ^ b, 2)
758+
self.assertEqual(a ^ 1, 2)
759+
self.assertTrue(isinstance(a ^ b, int))
760+
self.assertTrue(isinstance(a ^ 1, int))
761+
762+
self.assertEqual(~a, -4)
763+
self.assertTrue(isinstance(~a, int))
764+
765+
def test_unary_operators(self):
766+
a = int(3)
767+
b = int(-3)
768+
769+
self.assertEqual(+a, a)
770+
self.assertEqual(+a, 3)
771+
self.assertEqual(+b, b)
772+
self.assertEqual(+b, -3)
773+
self.assertTrue(isinstance(+a, int))
774+
775+
self.assertEqual(-a, b)
776+
self.assertEqual(-a, -3)
777+
self.assertEqual(-b, a)
778+
self.assertEqual(-b, 3)
779+
self.assertTrue(isinstance(-a, int))
692780

693781
if __name__ == "__main__":
694782
unittest.main()

future/types/newint.py

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
They are very similar. The most notable difference is:
55
66
- representation: trailing L in Python 2 removed in Python 3
7-
87
"""
9-
108
from __future__ import division
119

12-
from numbers import Integral
13-
1410
from future.types.newbytes import newbytes
1511
from future.utils import PY3, isint, istext, isbytes, with_metaclass
1612

@@ -36,15 +32,16 @@ def __new__(cls, x=0, base=10):
3632
| int(x=0) -> integer
3733
| int(x, base=10) -> integer
3834
|
39-
| Convert a number or string to an integer, or return 0 if no arguments
40-
| are given. If x is a number, return x.__int__(). For floating point
41-
| numbers, this truncates towards zero.
35+
| Convert a number or string to an integer, or return 0 if no
36+
| arguments are given. If x is a number, return x.__int__(). For
37+
| floating point numbers, this truncates towards zero.
4238
|
4339
| If x is not a number or if base is given, then x must be a string,
4440
| bytes, or bytearray instance representing an integer literal in the
45-
| given base. The literal can be preceded by '+' or '-' and be surrounded
46-
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
47-
| Base 0 means to interpret the base from the string as an integer literal.
41+
| given base. The literal can be preceded by '+' or '-' and be
42+
| surrounded by whitespace. The base defaults to 10. Valid bases are
43+
| 0 and 2-36. Base 0 means to interpret the base from the string as an
44+
| integer literal.
4845
| >>> int('0b100', base=0)
4946
| 4
5047
@@ -55,12 +52,14 @@ def __new__(cls, x=0, base=10):
5552
val = x
5653
else:
5754
if not isint(val):
58-
raise TypeError('__int__ returned non-int ({0})'.format(type(val)))
55+
raise TypeError('__int__ returned non-int ({0})'.format(
56+
type(val)))
5957

6058
if base != 10:
6159
# Explicit base
6260
if not (istext(val) or isbytes(val) or isinstance(val, bytearray)):
63-
raise TypeError("int() can't convert non-string with explicit base")
61+
raise TypeError(
62+
"int() can't convert non-string with explicit base")
6463
try:
6564
return super(newint, cls).__new__(cls, val, base)
6665
except TypeError:
@@ -77,9 +76,8 @@ def __new__(cls, x=0, base=10):
7776
try:
7877
return super(newint, cls).__new__(cls, newbytes(val))
7978
except:
80-
raise TypeError("newint argument must be a string or a number, not '{0}'".format(
81-
type(val)))
82-
79+
raise TypeError("newint argument must be a string or a number,"
80+
"not '{0}'".format(type(val)))
8381

8482
def __repr__(self):
8583
"""
@@ -117,34 +115,38 @@ def __mul__(self, other):
117115
value = super(newint, self).__mul__(other)
118116
if isint(value):
119117
return newint(value)
120-
if value is NotImplemented:
118+
elif value is NotImplemented:
121119
return long(self) * other
122120
return value
123121

124122
def __rmul__(self, other):
125123
value = super(newint, self).__rmul__(other)
126124
if isint(value):
127125
return newint(value)
128-
if value is NotImplemented:
126+
elif value is NotImplemented:
129127
return other * long(self)
130128
return value
131129

132130
def __div__(self, other):
131+
# We override this rather than e.g. relying on object.__div__ or
132+
# long.__div__ because we want to wrap the value in a newint()
133+
# call if other is another int
133134
value = long(self) / other
134-
if isint(value):
135+
if isinstance(other, (int, long)):
135136
return newint(value)
136137
else:
137138
return value
138139

139140
def __rdiv__(self, other):
140141
value = other / long(self)
141-
if isint(value):
142+
if isinstance(other, (int, long)):
142143
return newint(value)
143144
else:
144145
return value
145146

146147
def __idiv__(self, other):
147-
# long has no __idiv__ method. Use __itruediv__ and cast back to newint:
148+
# long has no __idiv__ method. Use __itruediv__ and cast back to
149+
# newint:
148150
value = self.__itruediv__(other)
149151
if isinstance(other, (int, long)):
150152
return newint(value)
@@ -199,45 +201,52 @@ def __rdivmod__(self, other):
199201
return (newint(value[0]), newint(value[1]))
200202

201203
def __pow__(self, other):
202-
return newint(super(newint, self).__pow__(other))
204+
value = super(newint, self).__pow__(other)
205+
if value is NotImplemented:
206+
return long(self) ** other
207+
return newint(value)
203208

204209
def __rpow__(self, other):
205-
return newint(super(newint, self).__rpow__(other))
210+
value = super(newint, self).__rpow__(other)
211+
if value is NotImplemented:
212+
return other ** long(self)
213+
return newint(value)
206214

207215
def __lshift__(self, other):
208-
return newint(super(newint, self).__lshift__(other))
209-
210-
def __rlshift__(self, other):
216+
if not isint(other):
217+
raise TypeError(
218+
"unsupported operand type(s) for <<: '%s' and '%s'" %
219+
(type(self).__name__, type(other).__name__))
211220
return newint(super(newint, self).__lshift__(other))
212221

213222
def __rshift__(self, other):
214-
return newint(super(newint, self).__rshift__(other))
215-
216-
def __rrshift__(self, other):
223+
if not isint(other):
224+
raise TypeError(
225+
"unsupported operand type(s) for >>: '%s' and '%s'" %
226+
(type(self).__name__, type(other).__name__))
217227
return newint(super(newint, self).__rshift__(other))
218228

219229
def __and__(self, other):
230+
if not isint(other):
231+
raise TypeError(
232+
"unsupported operand type(s) for &: '%s' and '%s'" %
233+
(type(self).__name__, type(other).__name__))
220234
return newint(super(newint, self).__and__(other))
221235

222-
def __rand__(self, other):
223-
return newint(super(newint, self).__rand__(other))
224-
225236
def __or__(self, other):
237+
if not isint(other):
238+
raise TypeError(
239+
"unsupported operand type(s) for |: '%s' and '%s'" %
240+
(type(self).__name__, type(other).__name__))
226241
return newint(super(newint, self).__or__(other))
227242

228-
def __ror__(self, other):
229-
return newint(super(newint, self).__ror__(other))
230-
231243
def __xor__(self, other):
244+
if not isint(other):
245+
raise TypeError(
246+
"unsupported operand type(s) for ^: '%s' and '%s'" %
247+
(type(self).__name__, type(other).__name__))
232248
return newint(super(newint, self).__xor__(other))
233249

234-
def __rxor__(self, other):
235-
return newint(super(newint, self).__rxor__(other))
236-
237-
# __radd__(self, other) __rsub__(self, other) __rmul__(self, other) __rdiv__(self, other) __rtruediv__(self, other) __rfloordiv__(self, other) __rmod__(self, other) __rdivmod__(self, other) __rpow__(self, other) __rlshift__(self, other) __rrshift__(self, other) __rand__(self, other) __rxor__(self, other) __ror__(self, other)
238-
239-
# __iadd__(self, other) __isub__(self, other) __imul__(self, other) __idiv__(self, other) __itruediv__(self, other) __ifloordiv__(self, other) __imod__(self, other) __ipow__(self, other, [modulo]) __ilshift__(self, other) __irshift__(self, other) __iand__(self, other) __ixor__(self, other) __ior__(self, other)
240-
241250
def __neg__(self):
242251
return newint(super(newint, self).__neg__())
243252

0 commit comments

Comments
 (0)