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,39 +115,40 @@ 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
132- def __div__ (self , other ):
133- value = long (self ) / other
134- if isint (value ):
135- return newint (value )
136- else :
137- return value
138-
139- def __rdiv__ (self , other ):
140- value = other / long (self )
141- if isint (value ):
142- return newint (value )
143- else :
144- return value
145-
146- def __idiv__ (self , other ):
147- # long has no __idiv__ method. Use __itruediv__ and cast back to newint:
148- value = self .__itruediv__ (other )
149- if isinstance (other , (int , long )):
150- return newint (value )
151- else :
152- return value
130+ #def __div__(self, other):
131+ #value = long(self) / other
132+ #if isint(value):
133+ #return newint(value)
134+ #else:
135+ #return value
136+
137+ #def __rdiv__(self, other):
138+ #value = other / long(self)
139+ #if isint(value):
140+ #return newint(value)
141+ #else:
142+ #return value
143+
144+ #def __idiv__(self, other):
145+ ## long has no __idiv__ method. Use __itruediv__ and cast back to
146+ ## newint:
147+ #value = self.__itruediv__(other)
148+ #if isinstance(other, (int, long)):
149+ #return newint(value)
150+ #else:
151+ #return value
153152
154153 def __truediv__ (self , other ):
155154 value = super (newint , self ).__truediv__ (other )
@@ -199,45 +198,52 @@ def __rdivmod__(self, other):
199198 return (newint (value [0 ]), newint (value [1 ]))
200199
201200 def __pow__ (self , other ):
202- return newint (super (newint , self ).__pow__ (other ))
201+ value = super (newint , self ).__pow__ (other )
202+ if value is NotImplemented :
203+ return long (self ) ** other
204+ return newint (value )
203205
204206 def __rpow__ (self , other ):
205- return newint (super (newint , self ).__rpow__ (other ))
207+ value = super (newint , self ).__rpow__ (other )
208+ if value is NotImplemented :
209+ return other ** long (self )
210+ return newint (value )
206211
207212 def __lshift__ (self , other ):
208- return newint (super (newint , self ).__lshift__ (other ))
209-
210- def __rlshift__ (self , other ):
213+ if not isint (other ):
214+ raise TypeError (
215+ "unsupported operand type(s) for <<: '%s' and '%s'" %
216+ (type (self ).__name__ , type (other ).__name__ ))
211217 return newint (super (newint , self ).__lshift__ (other ))
212218
213219 def __rshift__ (self , other ):
214- return newint (super (newint , self ).__rshift__ (other ))
215-
216- def __rrshift__ (self , other ):
220+ if not isint (other ):
221+ raise TypeError (
222+ "unsupported operand type(s) for >>: '%s' and '%s'" %
223+ (type (self ).__name__ , type (other ).__name__ ))
217224 return newint (super (newint , self ).__rshift__ (other ))
218225
219226 def __and__ (self , other ):
227+ if not isint (other ):
228+ raise TypeError (
229+ "unsupported operand type(s) for &: '%s' and '%s'" %
230+ (type (self ).__name__ , type (other ).__name__ ))
220231 return newint (super (newint , self ).__and__ (other ))
221232
222- def __rand__ (self , other ):
223- return newint (super (newint , self ).__rand__ (other ))
224-
225233 def __or__ (self , other ):
234+ if not isint (other ):
235+ raise TypeError (
236+ "unsupported operand type(s) for |: '%s' and '%s'" %
237+ (type (self ).__name__ , type (other ).__name__ ))
226238 return newint (super (newint , self ).__or__ (other ))
227239
228- def __ror__ (self , other ):
229- return newint (super (newint , self ).__ror__ (other ))
230-
231240 def __xor__ (self , other ):
241+ if not isint (other ):
242+ raise TypeError (
243+ "unsupported operand type(s) for ^: '%s' and '%s'" %
244+ (type (self ).__name__ , type (other ).__name__ ))
232245 return newint (super (newint , self ).__xor__ (other ))
233246
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-
241247 def __neg__ (self ):
242248 return newint (super (newint , self ).__neg__ ())
243249
0 commit comments