44They are very similar. The most notable difference is:
55
66- representation: trailing L in Python 2 removed in Python 3
7-
87"""
9-
108from __future__ import division
119
12- from numbers import Integral
13-
1410from future .types .newbytes import newbytes
1511from 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