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