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