Skip to content

Commit 2cc39c9

Browse files
committed
Fixing upcasting behaviour for some operations on integers.
1 parent 8ea9369 commit 2cc39c9

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

future/types/newint.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def __new__(cls, x=0, base=10):
3535
3636
| int(x=0) -> integer
3737
| int(x, base=10) -> integer
38-
|
38+
|
3939
| Convert a number or string to an integer, or return 0 if no arguments
4040
| are given. If x is a number, return x.__int__(). For floating point
4141
| numbers, this truncates towards zero.
42-
|
42+
|
4343
| If x is not a number or if base is given, then x must be a string,
4444
| bytes, or bytearray instance representing an integer literal in the
4545
| given base. The literal can be preceded by '+' or '-' and be surrounded
@@ -79,8 +79,8 @@ def __new__(cls, x=0, base=10):
7979
except:
8080
raise TypeError("newint argument must be a string or a number, not '{0}'".format(
8181
type(val)))
82-
83-
82+
83+
8484
def __repr__(self):
8585
"""
8686
Without the L suffix
@@ -92,20 +92,29 @@ def __repr__(self):
9292
def __add__(self, other):
9393
value = super(newint, self).__add__(other)
9494
if value is NotImplemented:
95-
# e.g. a float
95+
# e.g. a float, complex, ...
9696
return long(self) + other
9797
return newint(value)
9898

9999
def __radd__(self, other):
100100
value = super(newint, self).__radd__(other)
101+
if value is NotImplemented:
102+
# e.g. a float, complex, ...
103+
return other + long(self)
101104
return newint(value)
102105

103106
def __sub__(self, other):
104107
value = super(newint, self).__sub__(other)
108+
if value is NotImplemented:
109+
# e.g. a float, complex, ...
110+
return long(self) - other
105111
return newint(value)
106112

107113
def __rsub__(self, other):
108114
value = super(newint, self).__rsub__(other)
115+
if value is NotImplemented:
116+
# e.g. a float, complex, ...
117+
return other - long(self)
109118
return newint(value)
110119

111120
def __mul__(self, other):
@@ -120,6 +129,8 @@ def __rmul__(self, other):
120129
value = super(newint, self).__rmul__(other)
121130
if isint(value):
122131
return newint(value)
132+
if value is NotImplemented:
133+
return other * long(self)
123134
return value
124135

125136
def __div__(self, other):
@@ -129,13 +140,17 @@ def __div__(self, other):
129140
value = long(self) / other
130141
if isinstance(other, (int, long)):
131142
return newint(value)
143+
elif value is NotImplemented:
144+
return long(self) / other
132145
else:
133146
return value
134147

135148
def __rdiv__(self, other):
136149
value = other / long(self)
137150
if isinstance(other, (int, long)):
138151
return newint(value)
152+
elif value is NotImplemented:
153+
return other / long(self)
139154
else:
140155
return value
141156

@@ -175,10 +190,18 @@ def __ifloordiv__(self, other):
175190
return newint(mylong)
176191

177192
def __mod__(self, other):
178-
return newint(super(newint, self).__mod__(other))
193+
value = super(newint, self).__mod__(other)
194+
if value is NotImplemented:
195+
# e.g. a float, complex, ...
196+
return long(self) % other
197+
return newint(value)
179198

180199
def __rmod__(self, other):
181-
return newint(super(newint, self).__rmod__(other))
200+
value = super(newint, self).__rmod__(other)
201+
if value is NotImplemented:
202+
# e.g. a float, complex, ...
203+
return other % long(self)
204+
return newint(value)
182205

183206
def __divmod__(self, other):
184207
value = super(newint, self).__divmod__(other)
@@ -224,19 +247,19 @@ def __xor__(self, other):
224247
def __rxor__(self, other):
225248
return newint(super(newint, self).__rxor__(other))
226249

227-
# __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)
250+
# __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)
228251

229252
# __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)
230253

231254
def __neg__(self):
232255
return newint(super(newint, self).__neg__())
233-
256+
234257
def __pos__(self):
235258
return newint(super(newint, self).__pos__())
236-
259+
237260
def __abs__(self):
238261
return newint(super(newint, self).__abs__())
239-
262+
240263
def __invert__(self):
241264
return newint(super(newint, self).__invert__())
242265

0 commit comments

Comments
 (0)