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