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