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