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