paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Core objects. |
paul@6 | 5 | |
paul@470 | 6 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@354 | 22 | from native import get_using |
paul@230 | 23 | |
paul@6 | 24 | class object: |
paul@107 | 25 | |
paul@107 | 26 | "The root class of all objects except functions." |
paul@107 | 27 | |
paul@6 | 28 | def __init__(self): |
paul@230 | 29 | |
paul@6 | 30 | "No-operation." |
paul@230 | 31 | |
paul@6 | 32 | pass |
paul@107 | 33 | |
paul@6 | 34 | def __bool__(self): |
paul@230 | 35 | |
paul@6 | 36 | "Objects are true by default." |
paul@230 | 37 | |
paul@6 | 38 | return True |
paul@6 | 39 | |
paul@230 | 40 | def __str__(self): |
paul@230 | 41 | |
paul@230 | 42 | "Return a string representation." |
paul@230 | 43 | |
paul@254 | 44 | return str(buffer(["<", self.__name__, " instance>"])) |
paul@230 | 45 | |
paul@230 | 46 | __repr__ = __str__ |
paul@230 | 47 | |
paul@269 | 48 | class module: |
paul@269 | 49 | |
paul@269 | 50 | "The class of module objects." |
paul@269 | 51 | |
paul@271 | 52 | def __init__(self): |
paul@271 | 53 | |
paul@271 | 54 | """ |
paul@271 | 55 | Reserve special attributes for module instances. |
paul@271 | 56 | """ |
paul@271 | 57 | |
paul@271 | 58 | self.__file__ = None |
paul@271 | 59 | self.__mname__ = None |
paul@271 | 60 | |
paul@269 | 61 | def __str__(self): |
paul@269 | 62 | |
paul@269 | 63 | "Return a string representation." |
paul@269 | 64 | |
paul@271 | 65 | return self.__mname__ |
paul@269 | 66 | |
paul@269 | 67 | __repr__ = __str__ |
paul@269 | 68 | |
paul@107 | 69 | class function: |
paul@107 | 70 | |
paul@107 | 71 | """ |
paul@107 | 72 | The class of all function objects. |
paul@107 | 73 | Note that as a special case, function does not inherit from object. |
paul@107 | 74 | """ |
paul@107 | 75 | |
paul@6 | 76 | def __init__(self): |
paul@6 | 77 | |
paul@107 | 78 | """ |
paul@107 | 79 | Reserve special attributes for function instances. |
paul@107 | 80 | """ |
paul@6 | 81 | |
paul@6 | 82 | self.__fn__ = None |
paul@6 | 83 | self.__args__ = None |
paul@251 | 84 | self.__fname__ = None |
paul@6 | 85 | |
paul@107 | 86 | def __bool__(self): |
paul@230 | 87 | |
paul@107 | 88 | "Functions are true by default." |
paul@230 | 89 | |
paul@107 | 90 | return True |
paul@107 | 91 | |
paul@230 | 92 | def __str__(self): |
paul@230 | 93 | |
paul@230 | 94 | "Return a string representation." |
paul@230 | 95 | |
paul@251 | 96 | return self.__fname__ |
paul@230 | 97 | |
paul@230 | 98 | __repr__ = __str__ |
paul@230 | 99 | |
paul@274 | 100 | class type: |
paul@107 | 101 | |
paul@274 | 102 | """ |
paul@274 | 103 | The class of all classes. Methods of this class do not treat contexts as |
paul@274 | 104 | instances, even though classes are meant to be instances of this class. |
paul@274 | 105 | Instead, contexts are either classes or instances. |
paul@274 | 106 | """ |
paul@107 | 107 | |
paul@274 | 108 | def __str__(self): |
paul@274 | 109 | |
paul@274 | 110 | "Return a string representation." |
paul@230 | 111 | |
paul@274 | 112 | return self.__name__ |
paul@274 | 113 | |
paul@274 | 114 | __repr__ = __str__ |
paul@6 | 115 | |
paul@470 | 116 | class Exception: |
paul@233 | 117 | |
paul@233 | 118 | "The root of all exception types." |
paul@233 | 119 | |
paul@233 | 120 | pass |
paul@233 | 121 | |
paul@470 | 122 | # Fundamental exceptions |
paul@470 | 123 | |
paul@470 | 124 | class MemoryError(Exception): |
paul@470 | 125 | |
paul@470 | 126 | "An error indicating failure to allocate or manage memory." |
paul@470 | 127 | |
paul@470 | 128 | pass |
paul@470 | 129 | |
paul@470 | 130 | class TypeError(Exception): |
paul@470 | 131 | |
paul@470 | 132 | "An error indicating unsuitable type usage." |
paul@470 | 133 | |
paul@470 | 134 | pass |
paul@470 | 135 | |
paul@470 | 136 | class UnboundMethodInvocation(Exception): |
paul@470 | 137 | |
paul@470 | 138 | "An error indicating an attempt to call an unbound method." |
paul@470 | 139 | |
paul@470 | 140 | pass |
paul@470 | 141 | |
paul@470 | 142 | class ArithmeticError(Exception): |
paul@470 | 143 | |
paul@470 | 144 | "A general arithmetic operation error." |
paul@470 | 145 | |
paul@470 | 146 | pass |
paul@470 | 147 | |
paul@470 | 148 | class FloatingPointError(Exception): |
paul@470 | 149 | |
paul@470 | 150 | "A floating point operation error." |
paul@470 | 151 | |
paul@470 | 152 | pass |
paul@470 | 153 | |
paul@470 | 154 | class OverflowError(ArithmeticError): |
paul@470 | 155 | |
paul@470 | 156 | """ |
paul@470 | 157 | Indicates that an arithmetic operation produced a result that could not be |
paul@470 | 158 | represented. |
paul@470 | 159 | """ |
paul@470 | 160 | |
paul@470 | 161 | pass |
paul@470 | 162 | |
paul@470 | 163 | class ZeroDivisionError(ArithmeticError): |
paul@470 | 164 | |
paul@470 | 165 | "An error occurring when an attempt was made to divide an operand by zero." |
paul@470 | 166 | |
paul@470 | 167 | pass |
paul@6 | 168 | |
paul@6 | 169 | # vim: tabstop=4 expandtab shiftwidth=4 |