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 def getattr(obj, name, default=object()): # object() is a placeholder 453 454 "Implementation of getattr." 455 456 _accessor # avoid isinstance but ensure that this class is included 457 458 try: 459 return _getattr(obj, name) 460 except AttributeError: 461 if default is not NotImplemented: 462 return default 463 else: 464 raise 465 466 def globals(): pass 467 def hasattr(obj, name): pass 468 def hash(obj): pass 469 def help(*args): pass 470 def hex(number): pass 471 def id(obj): pass 472 def input(prompt=None): pass 473 474 def isinstance(obj, cls_or_tuple): 475 476 """ 477 Return whether 'obj' is an instance of 'cls_or_tuple', where the latter is 478 either a class or a tuple of classes. 479 """ 480 481 # NOTE: CPython insists on tuples, but any sequence might be considered 482 # NOTE: acceptable. 483 484 if _isinstance(cls_or_tuple, tuple): 485 for cls in cls_or_tuple: 486 if obj.__class__ is cls or _isinstance(obj, cls): 487 return True 488 return False 489 else: 490 return obj.__class__ is cls_or_tuple or _isinstance(obj, cls_or_tuple) 491 492 def issubclass(obj, cls_or_tuple): pass 493 494 def iter(collection): 495 496 "Implementation of iter without callable plus sentinel support." 497 498 return collection.__iter__() 499 500 def len(obj): 501 502 "Implementation of len." 503 504 return obj.__len__() 505 506 def locals(): pass 507 508 def map(function, *args): pass 509 510 def max(*args): 511 512 "Implementation of max." 513 514 highest = args[0] 515 for arg in args[1:]: 516 if arg > highest: 517 highest = arg 518 return highest 519 520 def min(*args): 521 522 "Implementation of min." 523 524 lowest = args[0] 525 for arg in args[1:]: 526 if arg > lowest: 527 lowest = arg 528 return lowest 529 530 def oct(number): pass 531 def open(name, mode=None, buffering=None): pass 532 def ord(c): pass 533 def pow(x, y, z=None): pass 534 def property(fget=None, fset=None, fdel=None, doc=None): pass 535 536 def range(start_or_end, end=None, step=1): 537 538 "Implementation of range." 539 540 return list(xrange(start_or_end, end, step)) 541 542 def raw_input(prompt=None): pass 543 def reduce(function, sequence, initial=None): pass 544 def reload(module): pass 545 def repr(obj): pass 546 def reversed(sequence): pass 547 def round(number, ndigits=None): pass 548 def setattr(obj, name, value): pass 549 def sorted(iterable, cmp=None, key=None, reverse=False): pass 550 def staticmethod(function): pass 551 def sum(sequence, start=0): pass 552 def super(*args): pass 553 def unichr(i): pass 554 def vars(obj=None): pass 555 def zip(*args): pass 556 557 # Utility functions. 558 559 def _get_absolute_index(index, length): 560 561 """ 562 Return the absolute index for 'index' given a collection having the 563 specified 'length'. 564 """ 565 566 if index < 0: 567 return length + index 568 else: 569 return index 570 571 def _normalise_index(index, length): 572 573 "Normalise 'index' for a collection having the specified 'length'." 574 575 return _min(length, _max(0, _get_absolute_index(index, length))) 576 577 def _max(x, y): 578 579 "Return the maximum of 'x' and 'y'." 580 581 if x >= y: 582 return x 583 else: 584 return y 585 586 def _min(x, y): 587 588 "Return the minimum of 'x' and 'y'." 589 590 if x <= y: 591 return x 592 else: 593 return y 594 595 def _getitem(seq, index): 596 597 "Return the item or slice specified by 'index'." 598 599 if isinstance(index, int): 600 index = _normalise_index(index, len(seq)) 601 return seq.__get_single_item__(index) 602 elif isinstance(index, slice): 603 return seq.__getslice__(index.start, index.end) 604 else: 605 raise TypeError 606 607 def _getslice(seq, start, end=None): 608 609 "Return a slice starting from 'start', with the optional 'end'." 610 611 length = len(seq) 612 613 if start is None: 614 start = 0 615 else: 616 start = _normalise_index(start, length) 617 618 if end is None: 619 end = length 620 else: 621 end = _normalise_index(end, length) 622 623 result = [] 624 while start < end: 625 result.append(seq.__get_single_item__(start)) 626 start += 1 627 return result 628 629 # Special implementation classes. 630 631 class _accessor(str): 632 633 "A string which can be used to access attributes." 634 635 def __new__(self): 636 # Reserve space for an object table index. 637 self._index = None 638 639 # Special implementation functions. 640 641 def _getattr(obj, name): pass 642 def _isinstance(obj, cls): pass 643 def _print(dest, *args): pass 644 def _printnl(dest, *args): pass 645 def _tuple(l): pass 646 647 # Reference some names to ensure their existence. This should be everything 648 # mentioned in a get_builtin or load_builtin call. Instances from this module 649 # should be predefined constants. 650 651 function 652 AttributeError 653 #IndexError 654 #NoneType 655 #NotImplementedType 656 #StopIteration 657 TypeError 658 659 #bool 660 #ellipsis 661 #list 662 tuple 663 type 664 #xrange 665 666 # vim: tabstop=4 expandtab shiftwidth=4