paul@61 | 1 | #!/usr/bin/env python |
paul@61 | 2 | |
paul@61 | 3 | """ |
paul@431 | 4 | Simple built-in classes and functions. |
paul@61 | 5 | |
paul@656 | 6 | Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 |
paul@656 | 7 | Paul Boddie <paul@boddie.org.uk> |
paul@61 | 8 | |
paul@61 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@61 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@61 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@61 | 12 | version. |
paul@61 | 13 | |
paul@61 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@61 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@61 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@61 | 17 | details. |
paul@61 | 18 | |
paul@61 | 19 | You should have received a copy of the GNU General Public License along with |
paul@61 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@431 | 21 | |
paul@431 | 22 | -------- |
paul@431 | 23 | |
paul@431 | 24 | Objects which provide code that shall always be compiled should provide |
paul@431 | 25 | docstrings. Objects without code should be provided by native library code. |
paul@431 | 26 | |
paul@431 | 27 | Classes without docstrings do not have instantiators generated for them. |
paul@431 | 28 | |
paul@431 | 29 | Methods defined in classes are generated if they have docstrings, regardless of |
paul@431 | 30 | whether their classes have docstrings. |
paul@61 | 31 | """ |
paul@61 | 32 | |
paul@431 | 33 | import native |
paul@431 | 34 | |
paul@61 | 35 | class object: |
paul@431 | 36 | def __init__(self): |
paul@431 | 37 | "No-operation." |
paul@431 | 38 | pass |
paul@432 | 39 | def __bool__(self): |
paul@432 | 40 | "Objects are true by default." |
paul@432 | 41 | return True |
paul@61 | 42 | |
paul@63 | 43 | class basestring(object): |
paul@63 | 44 | def __init__(self, x=None): pass |
paul@367 | 45 | def __contains__(self, value): pass |
paul@346 | 46 | |
paul@346 | 47 | def __getitem__(self, index): |
paul@346 | 48 | # Note usage. |
paul@346 | 49 | IndexError |
paul@346 | 50 | |
paul@63 | 51 | def __getslice__(self, start, end=None): pass |
paul@431 | 52 | |
paul@431 | 53 | def __iadd__(self, other): |
paul@431 | 54 | "Return a new string for the operation." |
paul@431 | 55 | return _binary_op(self, other, native._str_add) |
paul@431 | 56 | |
paul@431 | 57 | __add__ = __radd__ = __iadd__ |
paul@431 | 58 | |
paul@63 | 59 | def __mul__(self, other): pass |
paul@203 | 60 | def __rmul__(self, other): pass |
paul@63 | 61 | def __mod__(self, other): pass |
paul@202 | 62 | def __rmod__(self, other): pass |
paul@431 | 63 | |
paul@431 | 64 | def __lt__(self, other): |
paul@431 | 65 | "Return a new boolean for the comparison." |
paul@431 | 66 | return _binary_op(self, other, native._str_lt) |
paul@431 | 67 | |
paul@431 | 68 | def __gt__(self, other): |
paul@431 | 69 | "Return a new boolean for the comparison." |
paul@431 | 70 | return _binary_op(self, other, native._str_gt) |
paul@431 | 71 | |
paul@431 | 72 | def __le__(self, other): |
paul@431 | 73 | "Return a new boolean for the comparison." |
paul@432 | 74 | return _negate(self.__gt__(other)) |
paul@431 | 75 | |
paul@431 | 76 | def __ge__(self, other): |
paul@431 | 77 | "Return a new boolean for the comparison." |
paul@432 | 78 | return _negate(self.__lt__(other)) |
paul@431 | 79 | |
paul@431 | 80 | def __eq__(self, other): |
paul@431 | 81 | "Return a new boolean for the comparison." |
paul@431 | 82 | return _binary_op(self, other, native._str_eq) |
paul@431 | 83 | |
paul@431 | 84 | def __ne__(self, other): |
paul@431 | 85 | "Return a new boolean for the comparison." |
paul@432 | 86 | return _negate(self.__eq__(other)) |
paul@431 | 87 | |
paul@63 | 88 | def __len__(self): pass |
paul@63 | 89 | def __str__(self): pass |
paul@431 | 90 | |
paul@431 | 91 | def __bool__(self): |
paul@432 | 92 | return _negate(native._str_eq(self, "")) |
paul@431 | 93 | |
paul@63 | 94 | def join(self, l): pass |
paul@367 | 95 | def split(self, s): pass |
paul@367 | 96 | def startswith(self, s): pass |
paul@367 | 97 | def endswith(self, s): pass |
paul@63 | 98 | |
paul@530 | 99 | def __iter__(self): |
paul@530 | 100 | |
paul@530 | 101 | "Return an iterator." |
paul@530 | 102 | |
paul@530 | 103 | return listiterator(self) |
paul@530 | 104 | |
paul@63 | 105 | class bool(object): |
paul@431 | 106 | def __bool__(self): |
paul@431 | 107 | "Identity operation." |
paul@431 | 108 | return self |
paul@61 | 109 | def __str__(self): pass |
paul@61 | 110 | |
paul@63 | 111 | class buffer(object): |
paul@61 | 112 | def __init__(self, size): pass |
paul@61 | 113 | def append(self, s): pass |
paul@61 | 114 | def __str__(self): pass |
paul@61 | 115 | |
paul@63 | 116 | class complex(object): |
paul@63 | 117 | def __init__(self, real, imag=None): pass |
paul@63 | 118 | |
paul@63 | 119 | class dict(object): |
paul@61 | 120 | def __init__(self, *args): pass |
paul@61 | 121 | def __setitem__(self, key, value): pass |
paul@725 | 122 | def __delitem__(self, key, value): pass |
paul@346 | 123 | |
paul@346 | 124 | def __getitem__(self, key): |
paul@346 | 125 | # Note usage. |
paul@346 | 126 | KeyError |
paul@61 | 127 | |
paul@725 | 128 | def has_key(self): pass |
paul@697 | 129 | def keys(self): pass |
paul@697 | 130 | def values(self): pass |
paul@697 | 131 | def items(self): pass |
paul@697 | 132 | def get(self, key): pass |
paul@697 | 133 | |
paul@63 | 134 | class file(object): |
paul@367 | 135 | def read(self, n=None): pass |
paul@61 | 136 | def write(self, s): pass |
paul@367 | 137 | def close(self): pass |
paul@61 | 138 | |
paul@63 | 139 | class float(object): |
paul@61 | 140 | def __init__(self, number_or_string=None): pass |
paul@61 | 141 | def __iadd__(self, other): pass |
paul@61 | 142 | def __isub__(self, other): pass |
paul@61 | 143 | def __add__(self, other): pass |
paul@61 | 144 | def __radd__(self, other): pass |
paul@61 | 145 | def __sub__(self, other): pass |
paul@61 | 146 | def __rsub__(self, other): pass |
paul@61 | 147 | def __mul__(self, other): pass |
paul@61 | 148 | def __rmul__(self, other): pass |
paul@61 | 149 | def __div__(self, other): pass |
paul@61 | 150 | def __rdiv__(self, other): pass |
paul@61 | 151 | def __floordiv__(self, other): pass |
paul@61 | 152 | def __rfloordiv__(self, other): pass |
paul@61 | 153 | def __mod__(self, other): pass |
paul@202 | 154 | def __rmod__(self, other): pass |
paul@61 | 155 | def __pow__(self, other): pass |
paul@61 | 156 | def __rpow__(self, other): pass |
paul@61 | 157 | def __lt__(self, other): pass |
paul@61 | 158 | def __gt__(self, other): pass |
paul@61 | 159 | def __le__(self, other): pass |
paul@61 | 160 | def __ge__(self, other): pass |
paul@61 | 161 | def __eq__(self, other): pass |
paul@61 | 162 | def __ne__(self, other): pass |
paul@61 | 163 | def __neg__(self): pass |
paul@61 | 164 | def __pos__(self): pass |
paul@61 | 165 | def __str__(self): pass |
paul@61 | 166 | def __bool__(self): pass |
paul@61 | 167 | |
paul@63 | 168 | class frozenset(object): |
paul@63 | 169 | def __init__(self, iterable): pass |
paul@63 | 170 | |
paul@137 | 171 | class function(object): |
paul@137 | 172 | pass |
paul@137 | 173 | |
paul@63 | 174 | class int(object): |
paul@61 | 175 | def __init__(self, number_or_string=None): pass |
paul@431 | 176 | |
paul@431 | 177 | def __iadd__(self, other): |
paul@431 | 178 | "Return a new int for the operation." |
paul@431 | 179 | return _binary_op(self, other, native._int_add) |
paul@431 | 180 | |
paul@431 | 181 | def __isub__(self, other): |
paul@431 | 182 | "Return a new int for the operation." |
paul@431 | 183 | return _binary_op(self, other, native._int_sub) |
paul@431 | 184 | |
paul@431 | 185 | def __imul__(self, other): |
paul@431 | 186 | "Return a new int for the operation." |
paul@431 | 187 | return _binary_op(self, other, native._int_mul) |
paul@431 | 188 | |
paul@431 | 189 | def __idiv__(self, other): |
paul@431 | 190 | "Return a new int for the operation." |
paul@431 | 191 | return _binary_op(self, other, native._int_div) |
paul@431 | 192 | |
paul@431 | 193 | def __imod__(self, other): |
paul@431 | 194 | "Return a new int for the operation." |
paul@431 | 195 | return _binary_op(self, other, native._int_mod) |
paul@431 | 196 | |
paul@431 | 197 | def __ipow__(self, other): |
paul@431 | 198 | "Return a new int for the operation." |
paul@431 | 199 | return _binary_op(self, other, native._int_pow) |
paul@431 | 200 | |
paul@431 | 201 | def __iand__(self, other): |
paul@431 | 202 | "Return a new int for the operation." |
paul@431 | 203 | return _binary_op(self, other, native._int_and) |
paul@431 | 204 | |
paul@431 | 205 | def __ior__(self, other): |
paul@431 | 206 | "Return a new int for the operation." |
paul@431 | 207 | return _binary_op(self, other, native._int_or) |
paul@431 | 208 | |
paul@431 | 209 | def __ixor__(self, other): |
paul@431 | 210 | "Return a new int for the operation." |
paul@431 | 211 | return _binary_op(self, other, native._int_xor) |
paul@431 | 212 | |
paul@431 | 213 | __add__ = __radd__ = __iadd__ |
paul@431 | 214 | __sub__ = __isub__ |
paul@431 | 215 | |
paul@431 | 216 | def __rsub__(self, other): |
paul@431 | 217 | "Return a new int for the operation." |
paul@431 | 218 | return _binary_op(self, other, native._int_rsub) |
paul@431 | 219 | |
paul@431 | 220 | __mul__ = __rmul__ = __imul__ |
paul@431 | 221 | __div__ = __idiv__ |
paul@431 | 222 | |
paul@431 | 223 | def __rdiv__(self, other): |
paul@431 | 224 | "Return a new int for the operation." |
paul@431 | 225 | return _binary_op(self, other, native._int_rdiv) |
paul@431 | 226 | |
paul@61 | 227 | def __floordiv__(self, other): pass |
paul@61 | 228 | def __rfloordiv__(self, other): pass |
paul@431 | 229 | |
paul@431 | 230 | __mod__ = __imod__ |
paul@431 | 231 | |
paul@431 | 232 | def __rmod__(self, other): |
paul@431 | 233 | "Return a new int for the operation." |
paul@431 | 234 | return _binary_op(self, other, native._int_rmod) |
paul@431 | 235 | |
paul@431 | 236 | __pow__ = __ipow__ |
paul@431 | 237 | |
paul@431 | 238 | def __rpow__(self, other): |
paul@431 | 239 | "Return a new int for the operation." |
paul@431 | 240 | return _binary_op(self, other, native._int_rpow) |
paul@431 | 241 | |
paul@431 | 242 | __and__ = __rand__ = __iand__ |
paul@431 | 243 | __or__ = __ror__ = __ior__ |
paul@431 | 244 | __xor__ = __rxor__ = __ixor__ |
paul@431 | 245 | |
paul@431 | 246 | def __lt__(self, other): |
paul@431 | 247 | "Return a new boolean for the comparison." |
paul@431 | 248 | return _binary_op(self, other, native._int_lt) |
paul@431 | 249 | |
paul@431 | 250 | def __gt__(self, other): |
paul@431 | 251 | "Return a new boolean for the comparison." |
paul@431 | 252 | return _binary_op(self, other, native._int_gt) |
paul@431 | 253 | |
paul@431 | 254 | def __le__(self, other): |
paul@431 | 255 | "Return a new boolean for the comparison." |
paul@432 | 256 | return _negate(self.__gt__(other)) |
paul@431 | 257 | |
paul@431 | 258 | def __ge__(self, other): |
paul@431 | 259 | "Return a new boolean for the comparison." |
paul@432 | 260 | return _negate(self.__lt__(other)) |
paul@431 | 261 | |
paul@431 | 262 | def __eq__(self, other): |
paul@431 | 263 | "Return a new boolean for the comparison." |
paul@431 | 264 | return _binary_op(self, other, native._int_eq) |
paul@431 | 265 | |
paul@431 | 266 | def __ne__(self, other): |
paul@431 | 267 | "Return a new boolean for the comparison." |
paul@432 | 268 | return _negate(self.__eq__(other)) |
paul@431 | 269 | |
paul@652 | 270 | def __invert__(self): pass |
paul@61 | 271 | def __neg__(self): pass |
paul@61 | 272 | def __pos__(self): pass |
paul@61 | 273 | def __str__(self): pass |
paul@260 | 274 | def __lshift__(self): pass |
paul@260 | 275 | def __rlshift__(self): pass |
paul@260 | 276 | def __rshift__(self): pass |
paul@260 | 277 | def __rrshift__(self): pass |
paul@61 | 278 | |
paul@431 | 279 | def __bool__(self): |
paul@431 | 280 | "Return whether this int is non-zero." |
paul@432 | 281 | return _negate(native._int_eq(self, 0)) |
paul@431 | 282 | |
paul@63 | 283 | class list(object): |
paul@332 | 284 | |
paul@332 | 285 | "Implementation of list." |
paul@332 | 286 | |
paul@332 | 287 | def __init__(self, args=None): |
paul@332 | 288 | |
paul@332 | 289 | "Initialise the list." |
paul@332 | 290 | |
paul@332 | 291 | self.__new__() |
paul@332 | 292 | |
paul@332 | 293 | if args is not None: |
paul@367 | 294 | self.extend(args) |
paul@332 | 295 | |
paul@332 | 296 | def __new__(self): |
paul@336 | 297 | # Reserve space for a fragment reference. |
paul@336 | 298 | self._elements = None |
paul@332 | 299 | |
paul@346 | 300 | def __getitem__(self, index): |
paul@390 | 301 | |
paul@390 | 302 | "Return the item or slice specified by 'index'." |
paul@390 | 303 | |
paul@390 | 304 | return _getitem(self, index) |
paul@346 | 305 | |
paul@367 | 306 | def __contains__(self, value): pass |
paul@61 | 307 | def __setitem__(self, index, value): pass |
paul@725 | 308 | def __delitem__(self, index): pass |
paul@390 | 309 | |
paul@390 | 310 | def __getslice__(self, start, end=None): |
paul@390 | 311 | |
paul@390 | 312 | "Return a slice starting from 'start', with the optional 'end'." |
paul@390 | 313 | |
paul@390 | 314 | return _getslice(self, start, end) |
paul@390 | 315 | |
paul@61 | 316 | def __setslice__(self, start, end, slice): pass |
paul@725 | 317 | def __delslice__(self, start, end): pass |
paul@61 | 318 | def append(self, value): pass |
paul@367 | 319 | |
paul@367 | 320 | def extend(self, iterable): |
paul@367 | 321 | |
paul@367 | 322 | "Extend the list with the contents of 'iterable'." |
paul@367 | 323 | |
paul@367 | 324 | for i in iterable: |
paul@367 | 325 | self.append(i) |
paul@367 | 326 | |
paul@410 | 327 | def pop(self): pass |
paul@595 | 328 | def reverse(self): pass |
paul@367 | 329 | def sort(self, cmp=None, key=None, reverse=0): pass |
paul@61 | 330 | def __len__(self): pass |
paul@61 | 331 | def __add__(self, other): pass |
paul@61 | 332 | def __iadd__(self, other): pass |
paul@61 | 333 | def __str__(self): pass |
paul@450 | 334 | |
paul@450 | 335 | def __bool__(self): |
paul@450 | 336 | |
paul@450 | 337 | "Lists are true if non-empty." |
paul@450 | 338 | |
paul@450 | 339 | return self.__len__() != 0 |
paul@61 | 340 | |
paul@259 | 341 | def __iter__(self): |
paul@259 | 342 | |
paul@259 | 343 | "Return an iterator." |
paul@259 | 344 | |
paul@259 | 345 | return listiterator(self) |
paul@259 | 346 | |
paul@390 | 347 | # Special implementation methods. |
paul@390 | 348 | |
paul@390 | 349 | def __get_single_item__(self, index): pass |
paul@390 | 350 | |
paul@259 | 351 | class listiterator(object): |
paul@262 | 352 | |
paul@262 | 353 | "Implementation of listiterator." |
paul@262 | 354 | |
paul@262 | 355 | def __init__(self, l): |
paul@262 | 356 | |
paul@262 | 357 | "Initialise with the given list 'l'." |
paul@262 | 358 | |
paul@262 | 359 | self.l = l |
paul@262 | 360 | self.i = 0 |
paul@262 | 361 | |
paul@262 | 362 | def next(self): |
paul@262 | 363 | |
paul@262 | 364 | "Return the next item." |
paul@262 | 365 | |
paul@262 | 366 | try: |
paul@262 | 367 | value = self.l[self.i] |
paul@262 | 368 | self.i += 1 |
paul@262 | 369 | return value |
paul@262 | 370 | except IndexError: |
paul@338 | 371 | raise StopIteration() |
paul@259 | 372 | |
paul@63 | 373 | class long(object): |
paul@61 | 374 | def __init__(self, number_or_string=None): pass |
paul@61 | 375 | def __iadd__(self, other): pass |
paul@61 | 376 | def __isub__(self, other): pass |
paul@61 | 377 | def __add__(self, other): pass |
paul@61 | 378 | def __radd__(self, other): pass |
paul@61 | 379 | def __sub__(self, other): pass |
paul@61 | 380 | def __rsub__(self, other): pass |
paul@61 | 381 | def __mul__(self, other): pass |
paul@61 | 382 | def __rmul__(self, other): pass |
paul@61 | 383 | def __div__(self, other): pass |
paul@61 | 384 | def __rdiv__(self, other): pass |
paul@61 | 385 | def __floordiv__(self, other): pass |
paul@61 | 386 | def __rfloordiv__(self, other): pass |
paul@61 | 387 | def __and__(self, other): pass |
paul@61 | 388 | def __rand__(self, other): pass |
paul@61 | 389 | def __or__(self, other): pass |
paul@61 | 390 | def __ror__(self, other): pass |
paul@61 | 391 | def __xor__(self, other): pass |
paul@61 | 392 | def __rxor__(self, other): pass |
paul@61 | 393 | def __lt__(self, other): pass |
paul@61 | 394 | def __gt__(self, other): pass |
paul@61 | 395 | def __le__(self, other): pass |
paul@61 | 396 | def __ge__(self, other): pass |
paul@61 | 397 | def __eq__(self, other): pass |
paul@61 | 398 | def __ne__(self, other): pass |
paul@61 | 399 | def __neg__(self): pass |
paul@61 | 400 | def __pos__(self): pass |
paul@61 | 401 | def __str__(self): pass |
paul@61 | 402 | def __bool__(self): pass |
paul@61 | 403 | |
paul@681 | 404 | class property(object): |
paul@681 | 405 | def __init__(fget=None, fset=None, fdel=None, doc=None): pass |
paul@681 | 406 | |
paul@63 | 407 | class set(object): |
paul@63 | 408 | def __init__(self, iterable): pass |
paul@604 | 409 | def add(self, item): pass |
paul@604 | 410 | def clear(self): pass |
paul@604 | 411 | def copy(self): pass |
paul@604 | 412 | def difference(self, other): pass |
paul@604 | 413 | def difference_update(self, other): pass |
paul@604 | 414 | def discard(self, item): pass |
paul@604 | 415 | def intersection(self, other): pass |
paul@604 | 416 | def intersection_update(self, other): pass |
paul@604 | 417 | def issubset(self, other): pass |
paul@604 | 418 | def issuperset(self, other): pass |
paul@604 | 419 | def pop(self): pass |
paul@604 | 420 | def remove(self, item): pass |
paul@604 | 421 | def symmetric_difference(self, other): pass |
paul@604 | 422 | def symmetric_difference_update(self, other): pass |
paul@604 | 423 | def union(self, other): pass |
paul@604 | 424 | def update(self, other): pass |
paul@61 | 425 | |
paul@342 | 426 | # See below for slice. |
paul@61 | 427 | |
paul@63 | 428 | class str(basestring): |
paul@63 | 429 | pass |
paul@61 | 430 | |
paul@63 | 431 | class type(object): |
paul@63 | 432 | pass |
paul@63 | 433 | |
paul@63 | 434 | class tuple(object): |
paul@408 | 435 | |
paul@408 | 436 | #"Implementation of tuple." |
paul@408 | 437 | |
paul@61 | 438 | def __init__(self, args): pass |
paul@346 | 439 | |
paul@346 | 440 | def __getitem__(self, index): |
paul@390 | 441 | |
paul@390 | 442 | "Return the item or slice specified by 'index'." |
paul@390 | 443 | |
paul@390 | 444 | return _getitem(self, index) |
paul@346 | 445 | |
paul@390 | 446 | def __getslice__(self, start, end=None): |
paul@390 | 447 | |
paul@390 | 448 | "Return a slice starting from 'start', with the optional 'end'." |
paul@390 | 449 | |
paul@408 | 450 | return _tuple(_getslice(self, start, end)) |
paul@390 | 451 | |
paul@61 | 452 | def __len__(self): pass |
paul@61 | 453 | def __add__(self, other): pass |
paul@61 | 454 | def __str__(self): pass |
paul@450 | 455 | |
paul@450 | 456 | def __bool__(self): |
paul@450 | 457 | |
paul@450 | 458 | "Tuples are true if non-empty." |
paul@450 | 459 | |
paul@450 | 460 | return self.__len__() != 0 |
paul@61 | 461 | |
paul@338 | 462 | def __iter__(self): |
paul@338 | 463 | |
paul@338 | 464 | "Return an iterator." |
paul@338 | 465 | |
paul@338 | 466 | return listiterator(self) |
paul@338 | 467 | |
paul@390 | 468 | # Special implementation methods. |
paul@390 | 469 | |
paul@390 | 470 | def __get_single_item__(self, index): pass |
paul@390 | 471 | |
paul@63 | 472 | class unicode(basestring): |
paul@63 | 473 | pass |
paul@63 | 474 | |
paul@63 | 475 | class xrange(object): |
paul@239 | 476 | |
paul@239 | 477 | "Implementation of xrange." |
paul@239 | 478 | |
paul@373 | 479 | NO_END = object() |
paul@373 | 480 | |
paul@373 | 481 | def __init__(self, start_or_end, end=NO_END, step=1): |
paul@239 | 482 | |
paul@239 | 483 | "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." |
paul@239 | 484 | |
paul@373 | 485 | if end is xrange.NO_END: |
paul@239 | 486 | self.start = 0 |
paul@239 | 487 | self.end = start_or_end |
paul@239 | 488 | else: |
paul@239 | 489 | self.start = start_or_end |
paul@239 | 490 | self.end = end |
paul@239 | 491 | |
paul@239 | 492 | self.step = step |
paul@239 | 493 | self.current = self.start |
paul@373 | 494 | self.limited = self.end is not xrange.NO_END |
paul@239 | 495 | |
paul@239 | 496 | def __iter__(self): |
paul@239 | 497 | |
paul@239 | 498 | "Return an iterator, currently self." |
paul@239 | 499 | |
paul@239 | 500 | return self |
paul@239 | 501 | |
paul@239 | 502 | def next(self): |
paul@239 | 503 | |
paul@239 | 504 | "Return the next item or raise a StopIteration exception." |
paul@239 | 505 | |
paul@342 | 506 | if self.limited: |
paul@342 | 507 | if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end: |
paul@342 | 508 | raise StopIteration() |
paul@239 | 509 | |
paul@239 | 510 | current = self.current |
paul@239 | 511 | self.current += self.step |
paul@239 | 512 | return current |
paul@61 | 513 | |
paul@342 | 514 | class slice(xrange): |
paul@342 | 515 | |
paul@342 | 516 | "Implementation of slice." |
paul@342 | 517 | |
paul@373 | 518 | def __init__(self, start_or_end=None, end=xrange.NO_END, step=1): |
paul@342 | 519 | |
paul@342 | 520 | "Initialise the slice with the given 'start_or_end', 'end' and 'step'." |
paul@342 | 521 | |
paul@342 | 522 | xrange.__init__(self, start_or_end, end, step) |
paul@342 | 523 | |
paul@63 | 524 | # Exceptions and warnings. |
paul@63 | 525 | |
paul@63 | 526 | class BaseException(object): |
paul@240 | 527 | |
paul@240 | 528 | "Implementation of BaseException." |
paul@240 | 529 | |
paul@235 | 530 | def __init__(self, *args): |
paul@405 | 531 | self._pc = None # remember where the exception occurred |
paul@235 | 532 | self.args = args |
paul@61 | 533 | |
paul@63 | 534 | class Exception(BaseException): pass |
paul@63 | 535 | class Warning(object): pass |
paul@61 | 536 | |
paul@63 | 537 | class ArithmeticError(Exception): pass |
paul@63 | 538 | class AssertionError(Exception): pass |
paul@63 | 539 | class AttributeError(Exception): pass |
paul@63 | 540 | class DeprecationWarning(Exception): pass |
paul@63 | 541 | class EOFError(Exception): pass |
paul@63 | 542 | class EnvironmentError(Exception): pass |
paul@63 | 543 | class FloatingPointError(Exception): pass |
paul@63 | 544 | class FutureWarning(Warning): pass |
paul@63 | 545 | class GeneratorExit(Exception): pass |
paul@63 | 546 | class ImportError(Exception): pass |
paul@63 | 547 | class ImportWarning(Warning): pass |
paul@63 | 548 | class IndentationError(Exception): pass |
paul@63 | 549 | class IndexError(Exception): pass |
paul@216 | 550 | class IOError(Exception): pass |
paul@63 | 551 | class KeyError(Exception): pass |
paul@63 | 552 | class KeyboardInterrupt(Exception): pass |
paul@63 | 553 | class LookupError(Exception): pass |
paul@63 | 554 | class MemoryError(Exception): pass |
paul@63 | 555 | class NameError(Exception): pass |
paul@63 | 556 | class NotImplementedError(Exception): pass |
paul@63 | 557 | class OSError(Exception): pass |
paul@63 | 558 | class OverflowError(Exception): pass |
paul@63 | 559 | class PendingDeprecationWarning(Warning): pass |
paul@63 | 560 | class ReferenceError(Exception): pass |
paul@63 | 561 | class RuntimeError(Exception): pass |
paul@63 | 562 | class RuntimeWarning(Warning): pass |
paul@63 | 563 | class StandardError(Exception): pass |
paul@240 | 564 | class StopIteration(Exception): "Implementation of StopIteration." |
paul@63 | 565 | class SyntaxError(Exception): pass |
paul@63 | 566 | class SyntaxWarning(Warning): pass |
paul@63 | 567 | class SystemError(Exception): pass |
paul@63 | 568 | class SystemExit(Exception): pass |
paul@63 | 569 | class TabError(Exception): pass |
paul@63 | 570 | class TypeError(Exception): pass |
paul@63 | 571 | class UnboundLocalError(Exception): pass |
paul@63 | 572 | class UnicodeDecodeError(Exception): pass |
paul@63 | 573 | class UnicodeEncodeError(Exception): pass |
paul@63 | 574 | class UnicodeError(Exception): pass |
paul@63 | 575 | class UnicodeTranslateError(Exception): pass |
paul@63 | 576 | class UnicodeWarning(Warning): pass |
paul@63 | 577 | class UserWarning(Warning): pass |
paul@63 | 578 | class ValueError(Exception): pass |
paul@63 | 579 | class ZeroDivisionError(Exception): pass |
paul@61 | 580 | |
paul@63 | 581 | # Various types. |
paul@63 | 582 | |
paul@203 | 583 | #class ellipsis: pass |
paul@486 | 584 | |
paul@486 | 585 | class NoneType: |
paul@486 | 586 | def __bool__(self): |
paul@486 | 587 | "None is always false." |
paul@486 | 588 | return False |
paul@486 | 589 | |
paul@63 | 590 | class NotImplementedType: pass |
paul@61 | 591 | |
paul@63 | 592 | # General functions. |
paul@63 | 593 | # NOTE: Some of these are actually provided by classes in CPython. |
paul@63 | 594 | # NOTE: We may refuse to support some of these in practice, such as... |
paul@360 | 595 | # NOTE: __import__, super, reload. |
paul@63 | 596 | |
paul@63 | 597 | def abs(number): pass |
paul@63 | 598 | def all(iterable): pass |
paul@63 | 599 | def any(iterable): pass |
paul@63 | 600 | def callable(obj): pass |
paul@63 | 601 | def chr(i): pass |
paul@63 | 602 | def classmethod(function): pass |
paul@63 | 603 | def cmp(x, y): pass |
paul@63 | 604 | def compile(source, filename, mode, flags=None, dont_inherit=None): pass |
paul@63 | 605 | def delattr(obj, name): pass |
paul@63 | 606 | def dir(obj=None): pass |
paul@63 | 607 | def divmod(x, y): pass |
paul@63 | 608 | def enumerate(iterable): pass |
paul@63 | 609 | def eval(source, globals=None, locals=None): pass |
paul@63 | 610 | def execfile(filename, globals=None, locals=None): pass |
paul@63 | 611 | def filter(function, sequence): pass |
paul@412 | 612 | |
paul@656 | 613 | _getattr_default=object() # a unique placeholder for a missing value |
paul@414 | 614 | def getattr(obj, name, default=_getattr_default): |
paul@412 | 615 | |
paul@656 | 616 | "Implementation of getattr (activating the _accessor type)." |
paul@412 | 617 | |
paul@412 | 618 | _accessor # avoid isinstance but ensure that this class is included |
paul@412 | 619 | |
paul@412 | 620 | try: |
paul@412 | 621 | return _getattr(obj, name) |
paul@412 | 622 | except AttributeError: |
paul@414 | 623 | if default is not _getattr_default: |
paul@412 | 624 | return default |
paul@412 | 625 | else: |
paul@412 | 626 | raise |
paul@412 | 627 | |
paul@63 | 628 | def globals(): pass |
paul@63 | 629 | def hasattr(obj, name): pass |
paul@63 | 630 | def hash(obj): pass |
paul@213 | 631 | def help(*args): pass |
paul@63 | 632 | def hex(number): pass |
paul@63 | 633 | def id(obj): pass |
paul@63 | 634 | def input(prompt=None): pass |
paul@377 | 635 | |
paul@377 | 636 | def isinstance(obj, cls_or_tuple): |
paul@377 | 637 | |
paul@377 | 638 | """ |
paul@377 | 639 | Return whether 'obj' is an instance of 'cls_or_tuple', where the latter is |
paul@377 | 640 | either a class or a tuple of classes. |
paul@377 | 641 | """ |
paul@377 | 642 | |
paul@390 | 643 | # NOTE: CPython insists on tuples, but any sequence might be considered |
paul@390 | 644 | # NOTE: acceptable. |
paul@390 | 645 | |
paul@395 | 646 | if _isinstance(cls_or_tuple, tuple): |
paul@377 | 647 | for cls in cls_or_tuple: |
paul@395 | 648 | if obj.__class__ is cls or _isinstance(obj, cls): |
paul@377 | 649 | return True |
paul@377 | 650 | return False |
paul@377 | 651 | else: |
paul@395 | 652 | return obj.__class__ is cls_or_tuple or _isinstance(obj, cls_or_tuple) |
paul@377 | 653 | |
paul@63 | 654 | def issubclass(obj, cls_or_tuple): pass |
paul@334 | 655 | |
paul@334 | 656 | def iter(collection): |
paul@334 | 657 | |
paul@334 | 658 | "Implementation of iter without callable plus sentinel support." |
paul@334 | 659 | |
paul@334 | 660 | return collection.__iter__() |
paul@246 | 661 | |
paul@246 | 662 | def len(obj): |
paul@246 | 663 | |
paul@246 | 664 | "Implementation of len." |
paul@246 | 665 | |
paul@246 | 666 | return obj.__len__() |
paul@246 | 667 | |
paul@63 | 668 | def locals(): pass |
paul@342 | 669 | |
paul@63 | 670 | def map(function, *args): pass |
paul@342 | 671 | |
paul@342 | 672 | def max(*args): |
paul@342 | 673 | |
paul@342 | 674 | "Implementation of max." |
paul@342 | 675 | |
paul@342 | 676 | highest = args[0] |
paul@342 | 677 | for arg in args[1:]: |
paul@342 | 678 | if arg > highest: |
paul@342 | 679 | highest = arg |
paul@342 | 680 | return highest |
paul@342 | 681 | |
paul@342 | 682 | def min(*args): |
paul@342 | 683 | |
paul@342 | 684 | "Implementation of min." |
paul@342 | 685 | |
paul@342 | 686 | lowest = args[0] |
paul@342 | 687 | for arg in args[1:]: |
paul@342 | 688 | if arg > lowest: |
paul@342 | 689 | lowest = arg |
paul@342 | 690 | return lowest |
paul@342 | 691 | |
paul@63 | 692 | def oct(number): pass |
paul@63 | 693 | def open(name, mode=None, buffering=None): pass |
paul@63 | 694 | def ord(c): pass |
paul@63 | 695 | def pow(x, y, z=None): pass |
paul@246 | 696 | |
paul@246 | 697 | def range(start_or_end, end=None, step=1): |
paul@246 | 698 | |
paul@246 | 699 | "Implementation of range." |
paul@246 | 700 | |
paul@358 | 701 | return list(xrange(start_or_end, end, step)) |
paul@246 | 702 | |
paul@63 | 703 | def raw_input(prompt=None): pass |
paul@63 | 704 | def reduce(function, sequence, initial=None): pass |
paul@63 | 705 | def reload(module): pass |
paul@63 | 706 | def repr(obj): pass |
paul@63 | 707 | def reversed(sequence): pass |
paul@63 | 708 | def round(number, ndigits=None): pass |
paul@63 | 709 | def setattr(obj, name, value): pass |
paul@63 | 710 | def sorted(iterable, cmp=None, key=None, reverse=False): pass |
paul@63 | 711 | def staticmethod(function): pass |
paul@63 | 712 | def sum(sequence, start=0): pass |
paul@63 | 713 | def super(*args): pass |
paul@63 | 714 | def unichr(i): pass |
paul@63 | 715 | def vars(obj=None): pass |
paul@63 | 716 | def zip(*args): pass |
paul@63 | 717 | |
paul@390 | 718 | # Utility functions. |
paul@390 | 719 | |
paul@431 | 720 | def _binary_op(self, other, op): |
paul@431 | 721 | |
paul@431 | 722 | "Test the type of 'other' and perform 'op'." |
paul@431 | 723 | |
paul@431 | 724 | if self.__class__ is other.__class__: |
paul@431 | 725 | return op(self, other) |
paul@431 | 726 | else: |
paul@431 | 727 | return NotImplemented |
paul@431 | 728 | |
paul@432 | 729 | def _negate(result): |
paul@432 | 730 | |
paul@432 | 731 | "Negate any valid logical value." |
paul@432 | 732 | |
paul@432 | 733 | if result is NotImplemented: |
paul@432 | 734 | return result |
paul@432 | 735 | else: |
paul@432 | 736 | return not result |
paul@432 | 737 | |
paul@390 | 738 | def _get_absolute_index(index, length): |
paul@390 | 739 | |
paul@390 | 740 | """ |
paul@390 | 741 | Return the absolute index for 'index' given a collection having the |
paul@390 | 742 | specified 'length'. |
paul@390 | 743 | """ |
paul@390 | 744 | |
paul@390 | 745 | if index < 0: |
paul@390 | 746 | return length + index |
paul@390 | 747 | else: |
paul@390 | 748 | return index |
paul@390 | 749 | |
paul@390 | 750 | def _normalise_index(index, length): |
paul@390 | 751 | |
paul@390 | 752 | "Normalise 'index' for a collection having the specified 'length'." |
paul@390 | 753 | |
paul@390 | 754 | return _min(length, _max(0, _get_absolute_index(index, length))) |
paul@390 | 755 | |
paul@390 | 756 | def _max(x, y): |
paul@390 | 757 | |
paul@390 | 758 | "Return the maximum of 'x' and 'y'." |
paul@390 | 759 | |
paul@390 | 760 | if x >= y: |
paul@390 | 761 | return x |
paul@390 | 762 | else: |
paul@390 | 763 | return y |
paul@390 | 764 | |
paul@390 | 765 | def _min(x, y): |
paul@390 | 766 | |
paul@390 | 767 | "Return the minimum of 'x' and 'y'." |
paul@390 | 768 | |
paul@390 | 769 | if x <= y: |
paul@390 | 770 | return x |
paul@390 | 771 | else: |
paul@390 | 772 | return y |
paul@390 | 773 | |
paul@390 | 774 | def _getitem(seq, index): |
paul@390 | 775 | |
paul@390 | 776 | "Return the item or slice specified by 'index'." |
paul@390 | 777 | |
paul@390 | 778 | if isinstance(index, int): |
paul@391 | 779 | index = _normalise_index(index, len(seq)) |
paul@390 | 780 | return seq.__get_single_item__(index) |
paul@390 | 781 | elif isinstance(index, slice): |
paul@390 | 782 | return seq.__getslice__(index.start, index.end) |
paul@390 | 783 | else: |
paul@390 | 784 | raise TypeError |
paul@390 | 785 | |
paul@390 | 786 | def _getslice(seq, start, end=None): |
paul@390 | 787 | |
paul@390 | 788 | "Return a slice starting from 'start', with the optional 'end'." |
paul@390 | 789 | |
paul@390 | 790 | length = len(seq) |
paul@390 | 791 | |
paul@390 | 792 | if start is None: |
paul@390 | 793 | start = 0 |
paul@390 | 794 | else: |
paul@390 | 795 | start = _normalise_index(start, length) |
paul@390 | 796 | |
paul@390 | 797 | if end is None: |
paul@390 | 798 | end = length |
paul@390 | 799 | else: |
paul@390 | 800 | end = _normalise_index(end, length) |
paul@390 | 801 | |
paul@390 | 802 | result = [] |
paul@390 | 803 | while start < end: |
paul@390 | 804 | result.append(seq.__get_single_item__(start)) |
paul@390 | 805 | start += 1 |
paul@390 | 806 | return result |
paul@390 | 807 | |
paul@412 | 808 | # Special implementation classes. |
paul@412 | 809 | |
paul@412 | 810 | class _accessor(str): |
paul@412 | 811 | |
paul@412 | 812 | "A string which can be used to access attributes." |
paul@412 | 813 | |
paul@412 | 814 | def __new__(self): |
paul@412 | 815 | # Reserve space for an object table index. |
paul@412 | 816 | self._index = None |
paul@412 | 817 | |
paul@390 | 818 | # Special implementation functions. |
paul@390 | 819 | |
paul@412 | 820 | def _getattr(obj, name): pass |
paul@390 | 821 | def _isinstance(obj, cls): pass |
paul@393 | 822 | def _print(dest, *args): pass |
paul@393 | 823 | def _printnl(dest, *args): pass |
paul@408 | 824 | def _tuple(l): pass |
paul@393 | 825 | |
paul@167 | 826 | # Reference some names to ensure their existence. This should be everything |
paul@167 | 827 | # mentioned in a get_builtin or load_builtin call. Instances from this module |
paul@167 | 828 | # should be predefined constants. |
paul@167 | 829 | |
paul@167 | 830 | function |
paul@167 | 831 | AttributeError |
paul@399 | 832 | #IndexError |
paul@470 | 833 | NoneType |
paul@432 | 834 | NotImplementedType |
paul@345 | 835 | #StopIteration |
paul@167 | 836 | TypeError |
paul@203 | 837 | |
paul@463 | 838 | bool |
paul@399 | 839 | #ellipsis |
paul@346 | 840 | #list |
paul@203 | 841 | tuple |
paul@401 | 842 | type |
paul@203 | 843 | #xrange |
paul@167 | 844 | |
paul@61 | 845 | # vim: tabstop=4 expandtab shiftwidth=4 |