1.1 --- a/lib/__builtins__/int.py Fri Dec 02 21:16:06 2016 +0100
1.2 +++ b/lib/__builtins__/int.py Fri Dec 02 22:26:53 2016 +0100
1.3 @@ -19,7 +19,7 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from __builtins__.operator import _binary_op, _negate
1.8 +from __builtins__.operator import _negate
1.9 import native
1.10
1.11 class int(object):
1.12 @@ -30,65 +30,107 @@
1.13
1.14 "Initialise the integer with the given 'number_or_string'."
1.15
1.16 - if isinstance(number_or_string, int):
1.17 + if native._isinstance(number_or_string, int):
1.18 self.__data__ = number_or_string.__data__
1.19 else:
1.20 # NOTE: To be implemented.
1.21 self.__data__ = None
1.22
1.23 def __hash__(self):
1.24 +
1.25 "Return a value for hashing purposes."
1.26 +
1.27 return self
1.28
1.29 + def _binary_op(self, op, other):
1.30 +
1.31 + "Perform 'op' on this int and 'other' if appropriate."
1.32 +
1.33 + if native._isinstance(other, int):
1.34 + return op(self.__data__, other.__data__)
1.35 + else:
1.36 + return NotImplemented
1.37 +
1.38 + def _binary_op_rev(self, op, other):
1.39 +
1.40 + "Perform 'op' on 'other' and this int if appropriate."
1.41 +
1.42 + if native._isinstance(other, int):
1.43 + return op(other.__data__, self.__data__)
1.44 + else:
1.45 + return NotImplemented
1.46 +
1.47 def __iadd__(self, other):
1.48 - "Return a new int for the operation."
1.49 - return _binary_op(self, other, native._int_add)
1.50 +
1.51 + "Return a new int for the addition of this int and 'other'."
1.52 +
1.53 + return self._binary_op(native._int_add, other)
1.54
1.55 def __isub__(self, other):
1.56 - "Return a new int for the operation."
1.57 - return _binary_op(self, other, native._int_sub)
1.58 +
1.59 + "Return a new int for the subtraction of this int and 'other'."
1.60 +
1.61 + return self._binary_op(native._int_sub, other)
1.62
1.63 def __imul__(self, other):
1.64 - "Return a new int for the operation."
1.65 - return _binary_op(self, other, native._int_mul)
1.66 +
1.67 + "Return a new int for the multiplication of this int and 'other'."
1.68 +
1.69 + return self._binary_op(native._int_mul, other)
1.70
1.71 def __idiv__(self, other):
1.72 - "Return a new int for the operation."
1.73 - return _binary_op(self, other, native._int_div)
1.74 +
1.75 + "Return a new int for the division of this int and 'other'."
1.76 +
1.77 + return self._binary_op(native._int_div, other)
1.78
1.79 def __imod__(self, other):
1.80 - "Return a new int for the operation."
1.81 - return _binary_op(self, other, native._int_mod)
1.82 +
1.83 + "Return a new int for the modulo of this int by 'other'."
1.84 +
1.85 + return self._binary_op(native._int_mod, other)
1.86
1.87 def __ipow__(self, other):
1.88 - "Return a new int for the operation."
1.89 - return _binary_op(self, other, native._int_pow)
1.90 +
1.91 + "Return a new int for the exponentiation of this int by 'other'."
1.92 +
1.93 + return self._binary_op(native._int_pow, other)
1.94
1.95 def __iand__(self, other):
1.96 - "Return a new int for the operation."
1.97 - return _binary_op(self, other, native._int_and)
1.98 +
1.99 + "Return a new int for the binary-and of this int and 'other'."
1.100 +
1.101 + return self._binary_op(native._int_and, other)
1.102
1.103 def __ior__(self, other):
1.104 - "Return a new int for the operation."
1.105 - return _binary_op(self, other, native._int_or)
1.106 +
1.107 + "Return a new int for the binary-or of this int and 'other'."
1.108 +
1.109 + return self._binary_op(native._int_or, other)
1.110
1.111 def __ixor__(self, other):
1.112 - "Return a new int for the operation."
1.113 - return _binary_op(self, other, native._int_xor)
1.114 +
1.115 + "Return a new int for the exclusive-or of this int and 'other'."
1.116 +
1.117 + return self._binary_op(native._int_xor, other)
1.118
1.119 __add__ = __radd__ = __iadd__
1.120 __sub__ = __isub__
1.121
1.122 def __rsub__(self, other):
1.123 - "Return a new int for the operation."
1.124 - return _binary_op(other, self, native._int_sub)
1.125 +
1.126 + "Return a new int for the subtraction of this int from 'other'."
1.127 +
1.128 + return self._binary_op_rev(native._int_sub, other)
1.129
1.130 __mul__ = __rmul__ = __imul__
1.131 __div__ = __idiv__
1.132
1.133 def __rdiv__(self, other):
1.134 - "Return a new int for the operation."
1.135 - return _binary_op(other, self, native._int_div)
1.136 +
1.137 + "Return a new int for the division of this int into 'other'."
1.138 +
1.139 + return self._binary_op_rev(native._int_div, other)
1.140
1.141 def __floordiv__(self, other): pass
1.142 def __rfloordiv__(self, other): pass
1.143 @@ -97,56 +139,78 @@
1.144 __mod__ = __imod__
1.145
1.146 def __rmod__(self, other):
1.147 - "Return a new int for the operation."
1.148 - return _binary_op(other, self, native._int_mod)
1.149 +
1.150 + "Return a new int for the modulo of 'other' by this int."
1.151 +
1.152 + return self._binary_op_rev(native._int_mod, other)
1.153
1.154 __pow__ = __ipow__
1.155
1.156 def __rpow__(self, other):
1.157 - "Return a new int for the operation."
1.158 - return _binary_op(other, self, native._int_pow)
1.159 +
1.160 + "Return a new int for the exponentiation of 'other' by this int."
1.161 +
1.162 + return self._binary_op_rev(native._int_pow, other)
1.163
1.164 __and__ = __rand__ = __iand__
1.165 __or__ = __ror__ = __ior__
1.166 __xor__ = __rxor__ = __ixor__
1.167
1.168 def __lt__(self, other):
1.169 - "Return a new boolean for the comparison."
1.170 - return _binary_op(self, other, native._int_lt)
1.171 +
1.172 + "Return whether this int is less than 'other'."
1.173 +
1.174 + return self._binary_op(native._int_lt, other)
1.175
1.176 def __gt__(self, other):
1.177 - "Return a new boolean for the comparison."
1.178 - return _binary_op(self, other, native._int_gt)
1.179 +
1.180 + "Return whether this int is greater than 'other'."
1.181 +
1.182 + return self._binary_op(native._int_gt, other)
1.183
1.184 def __le__(self, other):
1.185 - "Return a new boolean for the comparison."
1.186 +
1.187 + "Return whether this int is less than or equal to 'other'."
1.188 +
1.189 return _negate(self.__gt__(other))
1.190
1.191 def __ge__(self, other):
1.192 - "Return a new boolean for the comparison."
1.193 +
1.194 + "Return whether this int is greater than or equal to 'other'."
1.195 +
1.196 return _negate(self.__lt__(other))
1.197
1.198 def __eq__(self, other):
1.199 - "Return a new boolean for the comparison."
1.200 - return _binary_op(self, other, native._int_eq)
1.201 +
1.202 + "Return whether this int is equal to 'other'."
1.203 +
1.204 + return self._binary_op(native._int_eq, other)
1.205
1.206 def __ne__(self, other):
1.207 - "Return a new boolean for the comparison."
1.208 +
1.209 + "Return whether this int is not equal to 'other'."
1.210 +
1.211 return _negate(self.__eq__(other))
1.212
1.213 def __invert__(self): pass
1.214
1.215 def __neg__(self):
1.216 +
1.217 "Apply the unary negation operator."
1.218 - return native._int_neg(self)
1.219 +
1.220 + return native._int_neg(self.__data__)
1.221
1.222 def __pos__(self):
1.223 +
1.224 "Apply the unary positive operator."
1.225 +
1.226 return self
1.227
1.228 def __str__(self):
1.229 +
1.230 "Return a string representation."
1.231 - return native._int_str(self)
1.232 +
1.233 + return native._int_str(self.__data__)
1.234
1.235 __repr__ = __str__
1.236
1.237 @@ -158,7 +222,10 @@
1.238 def __irshift__(self): pass
1.239
1.240 def __bool__(self):
1.241 +
1.242 "Return whether this int is non-zero."
1.243 - return native._int_ne(self, 0)
1.244 +
1.245 + zero = 0
1.246 + return native._int_ne(self.__data__, zero.__data__)
1.247
1.248 # vim: tabstop=4 expandtab shiftwidth=4