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