1.1 --- a/lib/builtins.py Mon Jun 13 00:18:13 2011 +0200
1.2 +++ b/lib/builtins.py Mon Jun 13 20:59:00 2011 +0200
1.3 @@ -1,9 +1,7 @@
1.4 #!/usr/bin/env python
1.5
1.6 """
1.7 -Simple built-in classes and functions. Objects which provide code that shall
1.8 -always be compiled should provide docstrings. Objects without code should be
1.9 -provided by native library code.
1.10 +Simple built-in classes and functions.
1.11
1.12 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk>
1.13
1.14 @@ -19,10 +17,24 @@
1.15
1.16 You should have received a copy of the GNU General Public License along with
1.17 this program. If not, see <http://www.gnu.org/licenses/>.
1.18 +
1.19 +--------
1.20 +
1.21 +Objects which provide code that shall always be compiled should provide
1.22 +docstrings. Objects without code should be provided by native library code.
1.23 +
1.24 +Classes without docstrings do not have instantiators generated for them.
1.25 +
1.26 +Methods defined in classes are generated if they have docstrings, regardless of
1.27 +whether their classes have docstrings.
1.28 """
1.29
1.30 +import native
1.31 +
1.32 class object:
1.33 - def __init__(self): pass
1.34 + def __init__(self):
1.35 + "No-operation."
1.36 + pass
1.37 def __bool__(self): pass
1.38
1.39 class basestring(object):
1.40 @@ -34,29 +46,57 @@
1.41 IndexError
1.42
1.43 def __getslice__(self, start, end=None): pass
1.44 - def __iadd__(self, other): pass
1.45 - def __add__(self, other): pass
1.46 - def __radd__(self, other): pass
1.47 +
1.48 + def __iadd__(self, other):
1.49 + "Return a new string for the operation."
1.50 + return _binary_op(self, other, native._str_add)
1.51 +
1.52 + __add__ = __radd__ = __iadd__
1.53 +
1.54 def __mul__(self, other): pass
1.55 def __rmul__(self, other): pass
1.56 def __mod__(self, other): pass
1.57 def __rmod__(self, other): pass
1.58 - def __lt__(self, other): pass
1.59 - def __gt__(self, other): pass
1.60 - def __le__(self, other): pass
1.61 - def __ge__(self, other): pass
1.62 - def __eq__(self, other): pass
1.63 - def __ne__(self, other): pass
1.64 +
1.65 + def __lt__(self, other):
1.66 + "Return a new boolean for the comparison."
1.67 + return _binary_op(self, other, native._str_lt)
1.68 +
1.69 + def __gt__(self, other):
1.70 + "Return a new boolean for the comparison."
1.71 + return _binary_op(self, other, native._str_gt)
1.72 +
1.73 + def __le__(self, other):
1.74 + "Return a new boolean for the comparison."
1.75 + return not self.__gt__(other)
1.76 +
1.77 + def __ge__(self, other):
1.78 + "Return a new boolean for the comparison."
1.79 + return not self.__lt__(other)
1.80 +
1.81 + def __eq__(self, other):
1.82 + "Return a new boolean for the comparison."
1.83 + return _binary_op(self, other, native._str_eq)
1.84 +
1.85 + def __ne__(self, other):
1.86 + "Return a new boolean for the comparison."
1.87 + return not self.__eq__(other)
1.88 +
1.89 def __len__(self): pass
1.90 def __str__(self): pass
1.91 - def __bool__(self): pass
1.92 +
1.93 + def __bool__(self):
1.94 + return not native._str_eq(self, "")
1.95 +
1.96 def join(self, l): pass
1.97 def split(self, s): pass
1.98 def startswith(self, s): pass
1.99 def endswith(self, s): pass
1.100
1.101 class bool(object):
1.102 - def __bool__(self): pass
1.103 + def __bool__(self):
1.104 + "Identity operation."
1.105 + return self
1.106 def __str__(self): pass
1.107
1.108 class buffer(object):
1.109 @@ -117,45 +157,112 @@
1.110
1.111 class int(object):
1.112 def __init__(self, number_or_string=None): pass
1.113 - def __iadd__(self, other): pass
1.114 - def __isub__(self, other): pass
1.115 - def __iand__(self, other): pass
1.116 - def __ior__(self, other): pass
1.117 - def __add__(self, other): pass
1.118 - def __radd__(self, other): pass
1.119 - def __sub__(self, other): pass
1.120 - def __rsub__(self, other): pass
1.121 - def __mul__(self, other): pass
1.122 - def __rmul__(self, other): pass
1.123 - def __div__(self, other): pass
1.124 - def __rdiv__(self, other): pass
1.125 +
1.126 + def __iadd__(self, other):
1.127 + "Return a new int for the operation."
1.128 + return _binary_op(self, other, native._int_add)
1.129 +
1.130 + def __isub__(self, other):
1.131 + "Return a new int for the operation."
1.132 + return _binary_op(self, other, native._int_sub)
1.133 +
1.134 + def __imul__(self, other):
1.135 + "Return a new int for the operation."
1.136 + return _binary_op(self, other, native._int_mul)
1.137 +
1.138 + def __idiv__(self, other):
1.139 + "Return a new int for the operation."
1.140 + return _binary_op(self, other, native._int_div)
1.141 +
1.142 + def __imod__(self, other):
1.143 + "Return a new int for the operation."
1.144 + return _binary_op(self, other, native._int_mod)
1.145 +
1.146 + def __ipow__(self, other):
1.147 + "Return a new int for the operation."
1.148 + return _binary_op(self, other, native._int_pow)
1.149 +
1.150 + def __iand__(self, other):
1.151 + "Return a new int for the operation."
1.152 + return _binary_op(self, other, native._int_and)
1.153 +
1.154 + def __ior__(self, other):
1.155 + "Return a new int for the operation."
1.156 + return _binary_op(self, other, native._int_or)
1.157 +
1.158 + def __ixor__(self, other):
1.159 + "Return a new int for the operation."
1.160 + return _binary_op(self, other, native._int_xor)
1.161 +
1.162 + __add__ = __radd__ = __iadd__
1.163 + __sub__ = __isub__
1.164 +
1.165 + def __rsub__(self, other):
1.166 + "Return a new int for the operation."
1.167 + return _binary_op(self, other, native._int_rsub)
1.168 +
1.169 + __mul__ = __rmul__ = __imul__
1.170 + __div__ = __idiv__
1.171 +
1.172 + def __rdiv__(self, other):
1.173 + "Return a new int for the operation."
1.174 + return _binary_op(self, other, native._int_rdiv)
1.175 +
1.176 def __floordiv__(self, other): pass
1.177 def __rfloordiv__(self, other): pass
1.178 - def __mod__(self, other): pass
1.179 - def __rmod__(self, other): pass
1.180 - def __pow__(self, other): pass
1.181 - def __rpow__(self, other): pass
1.182 - def __and__(self, other): pass
1.183 - def __rand__(self, other): pass
1.184 - def __or__(self, other): pass
1.185 - def __ror__(self, other): pass
1.186 - def __xor__(self, other): pass
1.187 - def __rxor__(self, other): pass
1.188 - def __lt__(self, other): pass
1.189 - def __gt__(self, other): pass
1.190 - def __le__(self, other): pass
1.191 - def __ge__(self, other): pass
1.192 - def __eq__(self, other): pass
1.193 - def __ne__(self, other): pass
1.194 +
1.195 + __mod__ = __imod__
1.196 +
1.197 + def __rmod__(self, other):
1.198 + "Return a new int for the operation."
1.199 + return _binary_op(self, other, native._int_rmod)
1.200 +
1.201 + __pow__ = __ipow__
1.202 +
1.203 + def __rpow__(self, other):
1.204 + "Return a new int for the operation."
1.205 + return _binary_op(self, other, native._int_rpow)
1.206 +
1.207 + __and__ = __rand__ = __iand__
1.208 + __or__ = __ror__ = __ior__
1.209 + __xor__ = __rxor__ = __ixor__
1.210 +
1.211 + def __lt__(self, other):
1.212 + "Return a new boolean for the comparison."
1.213 + return _binary_op(self, other, native._int_lt)
1.214 +
1.215 + def __gt__(self, other):
1.216 + "Return a new boolean for the comparison."
1.217 + return _binary_op(self, other, native._int_gt)
1.218 +
1.219 + def __le__(self, other):
1.220 + "Return a new boolean for the comparison."
1.221 + return not self.__gt__(other)
1.222 +
1.223 + def __ge__(self, other):
1.224 + "Return a new boolean for the comparison."
1.225 + return not self.__lt__(other)
1.226 +
1.227 + def __eq__(self, other):
1.228 + "Return a new boolean for the comparison."
1.229 + return _binary_op(self, other, native._int_eq)
1.230 +
1.231 + def __ne__(self, other):
1.232 + "Return a new boolean for the comparison."
1.233 + return not self.__eq__(other)
1.234 +
1.235 def __neg__(self): pass
1.236 def __pos__(self): pass
1.237 def __str__(self): pass
1.238 - def __bool__(self): pass
1.239 def __lshift__(self): pass
1.240 def __rlshift__(self): pass
1.241 def __rshift__(self): pass
1.242 def __rrshift__(self): pass
1.243
1.244 + def __bool__(self):
1.245 + "Return whether this int is non-zero."
1.246 + return not native._int_eq(self, 0)
1.247 +
1.248 class list(object):
1.249
1.250 "Implementation of list."
1.251 @@ -557,6 +664,15 @@
1.252
1.253 # Utility functions.
1.254
1.255 +def _binary_op(self, other, op):
1.256 +
1.257 + "Test the type of 'other' and perform 'op'."
1.258 +
1.259 + if self.__class__ is other.__class__:
1.260 + return op(self, other)
1.261 + else:
1.262 + return NotImplemented
1.263 +
1.264 def _get_absolute_index(index, length):
1.265
1.266 """