Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1 | /* This module provides the suite of standard class-based exceptions for |
| 2 | * Python's builtin module. This is a complete C implementation of what, |
| 3 | * in Python 1.5.2, was contained in the exceptions.py module. The problem |
| 4 | * there was that if exceptions.py could not be imported for some reason, |
| 5 | * the entire interpreter would abort. |
| 6 | * |
| 7 | * By moving the exceptions into C and statically linking, we can guarantee |
| 8 | * that the standard exceptions will always be available. |
| 9 | * |
| 10 | * history: |
| 11 | * 98-08-19 fl created (for pyexe) |
| 12 | * 00-02-08 fl updated for 1.5.2 |
| 13 | * 26-May-2000 baw vetted for Python 1.6 |
| 14 | * |
| 15 | * written by Fredrik Lundh |
| 16 | * modifications, additions, cleanups, and proofreading by Barry Warsaw |
| 17 | * |
| 18 | * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. |
| 19 | */ |
| 20 | |
| 21 | #include "Python.h" |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 22 | #include "osdefs.h" |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 23 | |
Tim Peters | bf26e07 | 2000-07-12 04:02:10 +0000 | [diff] [blame] | 24 | /* Caution: MS Visual C++ 6 errors if a single string literal exceeds |
| 25 | * 2Kb. So the module docstring has been broken roughly in half, using |
| 26 | * compile-time literal concatenation. |
| 27 | */ |
Skip Montanaro | 995895f | 2002-03-28 20:57:51 +0000 | [diff] [blame] | 28 | |
| 29 | /* NOTE: If the exception class hierarchy changes, don't forget to update |
| 30 | * Doc/lib/libexcs.tex! |
| 31 | */ |
| 32 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 33 | static char |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 34 | module__doc__[] = |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 35 | "Python's standard exception class hierarchy.\n\ |
| 36 | \n\ |
| 37 | Before Python 1.5, the standard exceptions were all simple string objects.\n\ |
| 38 | In Python 1.5, the standard exceptions were converted to classes organized\n\ |
| 39 | into a relatively flat hierarchy. String-based standard exceptions were\n\ |
| 40 | optional, or used as a fallback if some problem occurred while importing\n\ |
| 41 | the exception module. With Python 1.6, optional string-based standard\n\ |
| 42 | exceptions were removed (along with the -X command line flag).\n\ |
| 43 | \n\ |
| 44 | The class exceptions were implemented in such a way as to be almost\n\ |
| 45 | completely backward compatible. Some tricky uses of IOError could\n\ |
| 46 | potentially have broken, but by Python 1.6, all of these should have\n\ |
| 47 | been fixed. As of Python 1.6, the class-based standard exceptions are\n\ |
| 48 | now implemented in C, and are guaranteed to exist in the Python\n\ |
| 49 | interpreter.\n\ |
| 50 | \n\ |
| 51 | Here is a rundown of the class hierarchy. The classes found here are\n\ |
| 52 | inserted into both the exceptions module and the `built-in' module. It is\n\ |
| 53 | recommended that user defined class based exceptions be derived from the\n\ |
Tim Peters | bf26e07 | 2000-07-12 04:02:10 +0000 | [diff] [blame] | 54 | `Exception' class, although this is currently not enforced.\n" |
| 55 | /* keep string pieces "small" */ |
| 56 | "\n\ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 57 | Exception\n\ |
| 58 | |\n\ |
| 59 | +-- SystemExit\n\ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 60 | +-- StopIteration\n\ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 61 | +-- StandardError\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 62 | | |\n\ |
| 63 | | +-- KeyboardInterrupt\n\ |
| 64 | | +-- ImportError\n\ |
| 65 | | +-- EnvironmentError\n\ |
| 66 | | | |\n\ |
| 67 | | | +-- IOError\n\ |
| 68 | | | +-- OSError\n\ |
| 69 | | | |\n\ |
| 70 | | | +-- WindowsError\n\ |
| 71 | | |\n\ |
| 72 | | +-- EOFError\n\ |
| 73 | | +-- RuntimeError\n\ |
| 74 | | | |\n\ |
| 75 | | | +-- NotImplementedError\n\ |
| 76 | | |\n\ |
| 77 | | +-- NameError\n\ |
| 78 | | | |\n\ |
| 79 | | | +-- UnboundLocalError\n\ |
| 80 | | |\n\ |
| 81 | | +-- AttributeError\n\ |
| 82 | | +-- SyntaxError\n\ |
| 83 | | | |\n\ |
| 84 | | | +-- IndentationError\n\ |
| 85 | | | |\n\ |
| 86 | | | +-- TabError\n\ |
| 87 | | |\n\ |
| 88 | | +-- TypeError\n\ |
| 89 | | +-- AssertionError\n\ |
| 90 | | +-- LookupError\n\ |
| 91 | | | |\n\ |
| 92 | | | +-- IndexError\n\ |
| 93 | | | +-- KeyError\n\ |
| 94 | | |\n\ |
| 95 | | +-- ArithmeticError\n\ |
| 96 | | | |\n\ |
| 97 | | | +-- OverflowError\n\ |
| 98 | | | +-- ZeroDivisionError\n\ |
| 99 | | | +-- FloatingPointError\n\ |
| 100 | | |\n\ |
| 101 | | +-- ValueError\n\ |
| 102 | | | |\n\ |
| 103 | | | +-- UnicodeError\n\ |
| 104 | | |\n\ |
Fred Drake | bb9fa21 | 2001-10-05 21:50:08 +0000 | [diff] [blame] | 105 | | +-- ReferenceError\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 106 | | +-- SystemError\n\ |
| 107 | | +-- MemoryError\n\ |
| 108 | |\n\ |
| 109 | +---Warning\n\ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 110 | |\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 111 | +-- UserWarning\n\ |
| 112 | +-- DeprecationWarning\n\ |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 113 | +-- PendingDeprecationWarning\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 114 | +-- SyntaxWarning\n\ |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame] | 115 | +-- OverflowWarning\n\ |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 116 | +-- RuntimeWarning"; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | /* Helper function for populating a dictionary with method wrappers. */ |
| 120 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 121 | populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 122 | { |
| 123 | if (!methods) |
| 124 | return 0; |
| 125 | |
| 126 | while (methods->ml_name) { |
| 127 | /* get a wrapper for the built-in function */ |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 128 | PyObject *func = PyCFunction_New(methods, NULL); |
| 129 | PyObject *meth; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 130 | int status; |
| 131 | |
| 132 | if (!func) |
| 133 | return -1; |
| 134 | |
| 135 | /* turn the function into an unbound method */ |
| 136 | if (!(meth = PyMethod_New(func, NULL, klass))) { |
| 137 | Py_DECREF(func); |
| 138 | return -1; |
| 139 | } |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 140 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 141 | /* add method to dictionary */ |
| 142 | status = PyDict_SetItemString(dict, methods->ml_name, meth); |
| 143 | Py_DECREF(meth); |
| 144 | Py_DECREF(func); |
| 145 | |
| 146 | /* stop now if an error occurred, otherwise do the next method */ |
| 147 | if (status) |
| 148 | return status; |
| 149 | |
| 150 | methods++; |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 155 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 156 | |
| 157 | /* This function is used to create all subsequent exception classes. */ |
| 158 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 159 | make_class(PyObject **klass, PyObject *base, |
| 160 | char *name, PyMethodDef *methods, |
| 161 | char *docstr) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 162 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 163 | PyObject *dict = PyDict_New(); |
| 164 | PyObject *str = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 165 | int status = -1; |
| 166 | |
| 167 | if (!dict) |
| 168 | return -1; |
| 169 | |
| 170 | /* If an error occurs from here on, goto finally instead of explicitly |
| 171 | * returning NULL. |
| 172 | */ |
| 173 | |
| 174 | if (docstr) { |
| 175 | if (!(str = PyString_FromString(docstr))) |
| 176 | goto finally; |
| 177 | if (PyDict_SetItemString(dict, "__doc__", str)) |
| 178 | goto finally; |
| 179 | } |
| 180 | |
| 181 | if (!(*klass = PyErr_NewException(name, base, dict))) |
| 182 | goto finally; |
| 183 | |
| 184 | if (populate_methods(*klass, dict, methods)) { |
| 185 | Py_DECREF(*klass); |
| 186 | *klass = NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 187 | goto finally; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | status = 0; |
| 191 | |
| 192 | finally: |
| 193 | Py_XDECREF(dict); |
| 194 | Py_XDECREF(str); |
| 195 | return status; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */ |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 200 | static PyObject * |
| 201 | get_self(PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 202 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 203 | PyObject *self = PyTuple_GetItem(args, 0); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 204 | if (!self) { |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 205 | /* Watch out for being called to early in the bootstrapping process */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 206 | if (PyExc_TypeError) { |
| 207 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 208 | "unbound method must be called with instance as first argument"); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 209 | } |
| 210 | return NULL; |
| 211 | } |
| 212 | return self; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | /* Notes on bootstrapping the exception classes. |
| 218 | * |
| 219 | * First thing we create is the base class for all exceptions, called |
| 220 | * appropriately enough: Exception. Creation of this class makes no |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 221 | * assumptions about the existence of any other exception class -- except |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 222 | * for TypeError, which can conditionally exist. |
| 223 | * |
| 224 | * Next, StandardError is created (which is quite simple) followed by |
| 225 | * TypeError, because the instantiation of other exceptions can potentially |
| 226 | * throw a TypeError. Once these exceptions are created, all the others |
| 227 | * can be created in any order. See the static exctable below for the |
| 228 | * explicit bootstrap order. |
| 229 | * |
| 230 | * All classes after Exception can be created using PyErr_NewException(). |
| 231 | */ |
| 232 | |
| 233 | static char |
| 234 | Exception__doc__[] = "Common base class for all exceptions."; |
| 235 | |
| 236 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 237 | static PyObject * |
| 238 | Exception__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 239 | { |
| 240 | int status; |
| 241 | |
| 242 | if (!(self = get_self(args))) |
| 243 | return NULL; |
| 244 | |
| 245 | /* set args attribute */ |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 246 | /* XXX size is only a hint */ |
| 247 | args = PySequence_GetSlice(args, 1, PySequence_Size(args)); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 248 | if (!args) |
| 249 | return NULL; |
| 250 | status = PyObject_SetAttrString(self, "args", args); |
| 251 | Py_DECREF(args); |
| 252 | if (status < 0) |
| 253 | return NULL; |
| 254 | |
| 255 | Py_INCREF(Py_None); |
| 256 | return Py_None; |
| 257 | } |
| 258 | |
| 259 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 260 | static PyObject * |
| 261 | Exception__str__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 262 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 263 | PyObject *out; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 264 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 265 | if (!PyArg_ParseTuple(args, "O:__str__", &self)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 266 | return NULL; |
| 267 | |
| 268 | args = PyObject_GetAttrString(self, "args"); |
| 269 | if (!args) |
| 270 | return NULL; |
| 271 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 272 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 273 | case 0: |
| 274 | out = PyString_FromString(""); |
| 275 | break; |
| 276 | case 1: |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 277 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 278 | PyObject *tmp = PySequence_GetItem(args, 0); |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 279 | if (tmp) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 280 | out = PyObject_Str(tmp); |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 281 | Py_DECREF(tmp); |
| 282 | } |
| 283 | else |
| 284 | out = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 285 | break; |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 286 | } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 287 | default: |
| 288 | out = PyObject_Str(args); |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | Py_DECREF(args); |
| 293 | return out; |
| 294 | } |
| 295 | |
| 296 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 297 | static PyObject * |
| 298 | Exception__getitem__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 299 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 300 | PyObject *out; |
| 301 | PyObject *index; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 302 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 303 | if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 304 | return NULL; |
| 305 | |
| 306 | args = PyObject_GetAttrString(self, "args"); |
| 307 | if (!args) |
| 308 | return NULL; |
| 309 | |
| 310 | out = PyObject_GetItem(args, index); |
| 311 | Py_DECREF(args); |
| 312 | return out; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | static PyMethodDef |
| 317 | Exception_methods[] = { |
| 318 | /* methods for the Exception class */ |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 319 | { "__getitem__", Exception__getitem__, METH_VARARGS}, |
| 320 | { "__str__", Exception__str__, METH_VARARGS}, |
| 321 | { "__init__", Exception__init__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 322 | { NULL, NULL } |
| 323 | }; |
| 324 | |
| 325 | |
| 326 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 327 | make_Exception(char *modulename) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 328 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 329 | PyObject *dict = PyDict_New(); |
| 330 | PyObject *str = NULL; |
| 331 | PyObject *name = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 332 | int status = -1; |
| 333 | |
| 334 | if (!dict) |
| 335 | return -1; |
| 336 | |
| 337 | /* If an error occurs from here on, goto finally instead of explicitly |
| 338 | * returning NULL. |
| 339 | */ |
| 340 | |
| 341 | if (!(str = PyString_FromString(modulename))) |
| 342 | goto finally; |
| 343 | if (PyDict_SetItemString(dict, "__module__", str)) |
| 344 | goto finally; |
| 345 | Py_DECREF(str); |
| 346 | if (!(str = PyString_FromString(Exception__doc__))) |
| 347 | goto finally; |
| 348 | if (PyDict_SetItemString(dict, "__doc__", str)) |
| 349 | goto finally; |
| 350 | |
| 351 | if (!(name = PyString_FromString("Exception"))) |
| 352 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 353 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 354 | if (!(PyExc_Exception = PyClass_New(NULL, dict, name))) |
| 355 | goto finally; |
| 356 | |
| 357 | /* Now populate the dictionary with the method suite */ |
| 358 | if (populate_methods(PyExc_Exception, dict, Exception_methods)) |
| 359 | /* Don't need to reclaim PyExc_Exception here because that'll |
| 360 | * happen during interpreter shutdown. |
| 361 | */ |
| 362 | goto finally; |
| 363 | |
| 364 | status = 0; |
| 365 | |
| 366 | finally: |
| 367 | Py_XDECREF(dict); |
| 368 | Py_XDECREF(str); |
| 369 | Py_XDECREF(name); |
| 370 | return status; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | |
| 375 | static char |
| 376 | StandardError__doc__[] = "Base class for all standard Python exceptions."; |
| 377 | |
| 378 | static char |
| 379 | TypeError__doc__[] = "Inappropriate argument type."; |
| 380 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 381 | static char |
| 382 | StopIteration__doc__[] = "Signal the end from iterator.next()."; |
| 383 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 384 | |
| 385 | |
| 386 | static char |
| 387 | SystemExit__doc__[] = "Request to exit from the interpreter."; |
| 388 | |
| 389 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 390 | static PyObject * |
| 391 | SystemExit__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 392 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 393 | PyObject *code; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 394 | int status; |
| 395 | |
| 396 | if (!(self = get_self(args))) |
| 397 | return NULL; |
| 398 | |
| 399 | /* Set args attribute. */ |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 400 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 401 | return NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 402 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 403 | status = PyObject_SetAttrString(self, "args", args); |
| 404 | if (status < 0) { |
| 405 | Py_DECREF(args); |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | /* set code attribute */ |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 410 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 411 | case 0: |
| 412 | Py_INCREF(Py_None); |
| 413 | code = Py_None; |
| 414 | break; |
| 415 | case 1: |
| 416 | code = PySequence_GetItem(args, 0); |
| 417 | break; |
| 418 | default: |
| 419 | Py_INCREF(args); |
| 420 | code = args; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | status = PyObject_SetAttrString(self, "code", code); |
| 425 | Py_DECREF(code); |
| 426 | Py_DECREF(args); |
| 427 | if (status < 0) |
| 428 | return NULL; |
| 429 | |
| 430 | Py_INCREF(Py_None); |
| 431 | return Py_None; |
| 432 | } |
| 433 | |
| 434 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 435 | static PyMethodDef SystemExit_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 436 | { "__init__", SystemExit__init__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 437 | {NULL, NULL} |
| 438 | }; |
| 439 | |
| 440 | |
| 441 | |
| 442 | static char |
| 443 | KeyboardInterrupt__doc__[] = "Program interrupted by user."; |
| 444 | |
| 445 | static char |
| 446 | ImportError__doc__[] = |
| 447 | "Import can't find module, or can't find name in module."; |
| 448 | |
| 449 | |
| 450 | |
| 451 | static char |
| 452 | EnvironmentError__doc__[] = "Base class for I/O related errors."; |
| 453 | |
| 454 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 455 | static PyObject * |
| 456 | EnvironmentError__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 457 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 458 | PyObject *item0 = NULL; |
| 459 | PyObject *item1 = NULL; |
| 460 | PyObject *item2 = NULL; |
| 461 | PyObject *subslice = NULL; |
| 462 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 463 | |
| 464 | if (!(self = get_self(args))) |
| 465 | return NULL; |
| 466 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 467 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 468 | return NULL; |
| 469 | |
| 470 | if (PyObject_SetAttrString(self, "args", args) || |
| 471 | PyObject_SetAttrString(self, "errno", Py_None) || |
| 472 | PyObject_SetAttrString(self, "strerror", Py_None) || |
| 473 | PyObject_SetAttrString(self, "filename", Py_None)) |
| 474 | { |
| 475 | goto finally; |
| 476 | } |
| 477 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 478 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 479 | case 3: |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 480 | /* Where a function has a single filename, such as open() or some |
| 481 | * of the os module functions, PyErr_SetFromErrnoWithFilename() is |
| 482 | * called, giving a third argument which is the filename. But, so |
| 483 | * that old code using in-place unpacking doesn't break, e.g.: |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 484 | * |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 485 | * except IOError, (errno, strerror): |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 486 | * |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 487 | * we hack args so that it only contains two items. This also |
| 488 | * means we need our own __str__() which prints out the filename |
| 489 | * when it was supplied. |
| 490 | */ |
| 491 | item0 = PySequence_GetItem(args, 0); |
| 492 | item1 = PySequence_GetItem(args, 1); |
| 493 | item2 = PySequence_GetItem(args, 2); |
| 494 | if (!item0 || !item1 || !item2) |
| 495 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 496 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 497 | if (PyObject_SetAttrString(self, "errno", item0) || |
| 498 | PyObject_SetAttrString(self, "strerror", item1) || |
| 499 | PyObject_SetAttrString(self, "filename", item2)) |
| 500 | { |
| 501 | goto finally; |
| 502 | } |
| 503 | |
| 504 | subslice = PySequence_GetSlice(args, 0, 2); |
| 505 | if (!subslice || PyObject_SetAttrString(self, "args", subslice)) |
| 506 | goto finally; |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 507 | break; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 508 | |
| 509 | case 2: |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 510 | /* Used when PyErr_SetFromErrno() is called and no filename |
| 511 | * argument is given. |
| 512 | */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 513 | item0 = PySequence_GetItem(args, 0); |
| 514 | item1 = PySequence_GetItem(args, 1); |
| 515 | if (!item0 || !item1) |
| 516 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 517 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 518 | if (PyObject_SetAttrString(self, "errno", item0) || |
| 519 | PyObject_SetAttrString(self, "strerror", item1)) |
| 520 | { |
| 521 | goto finally; |
| 522 | } |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 523 | break; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | Py_INCREF(Py_None); |
| 527 | rtnval = Py_None; |
| 528 | |
| 529 | finally: |
| 530 | Py_DECREF(args); |
| 531 | Py_XDECREF(item0); |
| 532 | Py_XDECREF(item1); |
| 533 | Py_XDECREF(item2); |
| 534 | Py_XDECREF(subslice); |
| 535 | return rtnval; |
| 536 | } |
| 537 | |
| 538 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 539 | static PyObject * |
| 540 | EnvironmentError__str__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 541 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 542 | PyObject *originalself = self; |
| 543 | PyObject *filename; |
| 544 | PyObject *serrno; |
| 545 | PyObject *strerror; |
| 546 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 547 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 548 | if (!PyArg_ParseTuple(args, "O:__str__", &self)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 549 | return NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 550 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 551 | filename = PyObject_GetAttrString(self, "filename"); |
| 552 | serrno = PyObject_GetAttrString(self, "errno"); |
| 553 | strerror = PyObject_GetAttrString(self, "strerror"); |
| 554 | if (!filename || !serrno || !strerror) |
| 555 | goto finally; |
| 556 | |
| 557 | if (filename != Py_None) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 558 | PyObject *fmt = PyString_FromString("[Errno %s] %s: %s"); |
| 559 | PyObject *repr = PyObject_Repr(filename); |
| 560 | PyObject *tuple = PyTuple_New(3); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 561 | |
| 562 | if (!fmt || !repr || !tuple) { |
| 563 | Py_XDECREF(fmt); |
| 564 | Py_XDECREF(repr); |
| 565 | Py_XDECREF(tuple); |
| 566 | goto finally; |
| 567 | } |
| 568 | |
| 569 | PyTuple_SET_ITEM(tuple, 0, serrno); |
| 570 | PyTuple_SET_ITEM(tuple, 1, strerror); |
| 571 | PyTuple_SET_ITEM(tuple, 2, repr); |
| 572 | |
| 573 | rtnval = PyString_Format(fmt, tuple); |
| 574 | |
| 575 | Py_DECREF(fmt); |
| 576 | Py_DECREF(tuple); |
| 577 | /* already freed because tuple owned only reference */ |
| 578 | serrno = NULL; |
| 579 | strerror = NULL; |
| 580 | } |
| 581 | else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 582 | PyObject *fmt = PyString_FromString("[Errno %s] %s"); |
| 583 | PyObject *tuple = PyTuple_New(2); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 584 | |
| 585 | if (!fmt || !tuple) { |
| 586 | Py_XDECREF(fmt); |
| 587 | Py_XDECREF(tuple); |
| 588 | goto finally; |
| 589 | } |
| 590 | |
| 591 | PyTuple_SET_ITEM(tuple, 0, serrno); |
| 592 | PyTuple_SET_ITEM(tuple, 1, strerror); |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 593 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 594 | rtnval = PyString_Format(fmt, tuple); |
| 595 | |
| 596 | Py_DECREF(fmt); |
| 597 | Py_DECREF(tuple); |
| 598 | /* already freed because tuple owned only reference */ |
| 599 | serrno = NULL; |
| 600 | strerror = NULL; |
| 601 | } |
| 602 | else |
| 603 | /* The original Python code said: |
| 604 | * |
| 605 | * return StandardError.__str__(self) |
| 606 | * |
| 607 | * but there is no StandardError__str__() function; we happen to |
| 608 | * know that's just a pass through to Exception__str__(). |
| 609 | */ |
| 610 | rtnval = Exception__str__(originalself, args); |
| 611 | |
| 612 | finally: |
| 613 | Py_XDECREF(filename); |
| 614 | Py_XDECREF(serrno); |
| 615 | Py_XDECREF(strerror); |
| 616 | return rtnval; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | static |
| 621 | PyMethodDef EnvironmentError_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 622 | {"__init__", EnvironmentError__init__, METH_VARARGS}, |
| 623 | {"__str__", EnvironmentError__str__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 624 | {NULL, NULL} |
| 625 | }; |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | static char |
| 631 | IOError__doc__[] = "I/O operation failed."; |
| 632 | |
| 633 | static char |
| 634 | OSError__doc__[] = "OS system call failed."; |
| 635 | |
| 636 | #ifdef MS_WINDOWS |
| 637 | static char |
| 638 | WindowsError__doc__[] = "MS-Windows OS system call failed."; |
| 639 | #endif /* MS_WINDOWS */ |
| 640 | |
| 641 | static char |
| 642 | EOFError__doc__[] = "Read beyond end of file."; |
| 643 | |
| 644 | static char |
| 645 | RuntimeError__doc__[] = "Unspecified run-time error."; |
| 646 | |
| 647 | static char |
| 648 | NotImplementedError__doc__[] = |
| 649 | "Method or function hasn't been implemented yet."; |
| 650 | |
| 651 | static char |
| 652 | NameError__doc__[] = "Name not found globally."; |
| 653 | |
| 654 | static char |
| 655 | UnboundLocalError__doc__[] = |
| 656 | "Local name referenced but not bound to a value."; |
| 657 | |
| 658 | static char |
| 659 | AttributeError__doc__[] = "Attribute not found."; |
| 660 | |
| 661 | |
| 662 | |
| 663 | static char |
| 664 | SyntaxError__doc__[] = "Invalid syntax."; |
| 665 | |
| 666 | |
| 667 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 668 | SyntaxError__classinit__(PyObject *klass) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 669 | { |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 670 | int retval = 0; |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 671 | PyObject *emptystring = PyString_FromString(""); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 672 | |
| 673 | /* Additional class-creation time initializations */ |
| 674 | if (!emptystring || |
| 675 | PyObject_SetAttrString(klass, "msg", emptystring) || |
| 676 | PyObject_SetAttrString(klass, "filename", Py_None) || |
| 677 | PyObject_SetAttrString(klass, "lineno", Py_None) || |
| 678 | PyObject_SetAttrString(klass, "offset", Py_None) || |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 679 | PyObject_SetAttrString(klass, "text", Py_None) || |
| 680 | PyObject_SetAttrString(klass, "print_file_and_line", Py_None)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 681 | { |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 682 | retval = -1; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 683 | } |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 684 | Py_XDECREF(emptystring); |
| 685 | return retval; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 689 | static PyObject * |
| 690 | SyntaxError__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 691 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 692 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 693 | int lenargs; |
| 694 | |
| 695 | if (!(self = get_self(args))) |
| 696 | return NULL; |
| 697 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 698 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 699 | return NULL; |
| 700 | |
| 701 | if (PyObject_SetAttrString(self, "args", args)) |
| 702 | goto finally; |
| 703 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 704 | lenargs = PySequence_Size(args); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 705 | if (lenargs >= 1) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 706 | PyObject *item0 = PySequence_GetItem(args, 0); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 707 | int status; |
| 708 | |
| 709 | if (!item0) |
| 710 | goto finally; |
| 711 | status = PyObject_SetAttrString(self, "msg", item0); |
| 712 | Py_DECREF(item0); |
| 713 | if (status) |
| 714 | goto finally; |
| 715 | } |
| 716 | if (lenargs == 2) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 717 | PyObject *info = PySequence_GetItem(args, 1); |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 718 | PyObject *filename = NULL, *lineno = NULL; |
| 719 | PyObject *offset = NULL, *text = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 720 | int status = 1; |
| 721 | |
| 722 | if (!info) |
| 723 | goto finally; |
| 724 | |
| 725 | filename = PySequence_GetItem(info, 0); |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 726 | if (filename != NULL) { |
| 727 | lineno = PySequence_GetItem(info, 1); |
| 728 | if (lineno != NULL) { |
| 729 | offset = PySequence_GetItem(info, 2); |
| 730 | if (offset != NULL) { |
| 731 | text = PySequence_GetItem(info, 3); |
| 732 | if (text != NULL) { |
| 733 | status = |
| 734 | PyObject_SetAttrString(self, "filename", filename) |
| 735 | || PyObject_SetAttrString(self, "lineno", lineno) |
| 736 | || PyObject_SetAttrString(self, "offset", offset) |
| 737 | || PyObject_SetAttrString(self, "text", text); |
| 738 | Py_DECREF(text); |
| 739 | } |
| 740 | Py_DECREF(offset); |
| 741 | } |
| 742 | Py_DECREF(lineno); |
| 743 | } |
| 744 | Py_DECREF(filename); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 745 | } |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 746 | Py_DECREF(info); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 747 | |
| 748 | if (status) |
| 749 | goto finally; |
| 750 | } |
| 751 | Py_INCREF(Py_None); |
| 752 | rtnval = Py_None; |
| 753 | |
| 754 | finally: |
| 755 | Py_DECREF(args); |
| 756 | return rtnval; |
| 757 | } |
| 758 | |
| 759 | |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 760 | /* This is called "my_basename" instead of just "basename" to avoid name |
| 761 | conflicts with glibc; basename is already prototyped if _GNU_SOURCE is |
| 762 | defined, and Python does define that. */ |
| 763 | static char * |
| 764 | my_basename(char *name) |
| 765 | { |
| 766 | char *cp = name; |
| 767 | char *result = name; |
| 768 | |
| 769 | if (name == NULL) |
| 770 | return "???"; |
| 771 | while (*cp != '\0') { |
| 772 | if (*cp == SEP) |
| 773 | result = cp + 1; |
| 774 | ++cp; |
| 775 | } |
| 776 | return result; |
| 777 | } |
| 778 | |
| 779 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 780 | static PyObject * |
| 781 | SyntaxError__str__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 782 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 783 | PyObject *msg; |
| 784 | PyObject *str; |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 785 | PyObject *filename, *lineno, *result; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 786 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 787 | if (!PyArg_ParseTuple(args, "O:__str__", &self)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 788 | return NULL; |
| 789 | |
| 790 | if (!(msg = PyObject_GetAttrString(self, "msg"))) |
| 791 | return NULL; |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 792 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 793 | str = PyObject_Str(msg); |
| 794 | Py_DECREF(msg); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 795 | result = str; |
| 796 | |
| 797 | /* XXX -- do all the additional formatting with filename and |
| 798 | lineno here */ |
| 799 | |
| 800 | if (PyString_Check(str)) { |
| 801 | int have_filename = 0; |
| 802 | int have_lineno = 0; |
| 803 | char *buffer = NULL; |
| 804 | |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 805 | if ((filename = PyObject_GetAttrString(self, "filename")) != NULL) |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 806 | have_filename = PyString_Check(filename); |
| 807 | else |
| 808 | PyErr_Clear(); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 809 | |
| 810 | if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL) |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 811 | have_lineno = PyInt_Check(lineno); |
| 812 | else |
| 813 | PyErr_Clear(); |
| 814 | |
| 815 | if (have_filename || have_lineno) { |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 816 | int bufsize = PyString_GET_SIZE(str) + 64; |
| 817 | if (have_filename) |
| 818 | bufsize += PyString_GET_SIZE(filename); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 819 | |
Jeremy Hylton | 05bd787 | 2001-11-28 20:24:33 +0000 | [diff] [blame] | 820 | buffer = PyMem_MALLOC(bufsize); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 821 | if (buffer != NULL) { |
| 822 | if (have_filename && have_lineno) |
Jeremy Hylton | 05bd787 | 2001-11-28 20:24:33 +0000 | [diff] [blame] | 823 | PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)", |
| 824 | PyString_AS_STRING(str), |
| 825 | my_basename(PyString_AS_STRING(filename)), |
| 826 | PyInt_AsLong(lineno)); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 827 | else if (have_filename) |
Jeremy Hylton | 05bd787 | 2001-11-28 20:24:33 +0000 | [diff] [blame] | 828 | PyOS_snprintf(buffer, bufsize, "%s (%s)", |
| 829 | PyString_AS_STRING(str), |
| 830 | my_basename(PyString_AS_STRING(filename))); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 831 | else if (have_lineno) |
Jeremy Hylton | 05bd787 | 2001-11-28 20:24:33 +0000 | [diff] [blame] | 832 | PyOS_snprintf(buffer, bufsize, "%s (line %ld)", |
| 833 | PyString_AS_STRING(str), |
| 834 | PyInt_AsLong(lineno)); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 835 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 836 | result = PyString_FromString(buffer); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 837 | PyMem_FREE(buffer); |
| 838 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 839 | if (result == NULL) |
| 840 | result = str; |
| 841 | else |
| 842 | Py_DECREF(str); |
| 843 | } |
| 844 | } |
| 845 | Py_XDECREF(filename); |
| 846 | Py_XDECREF(lineno); |
| 847 | } |
| 848 | return result; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 852 | static PyMethodDef SyntaxError_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 853 | {"__init__", SyntaxError__init__, METH_VARARGS}, |
| 854 | {"__str__", SyntaxError__str__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 855 | {NULL, NULL} |
| 856 | }; |
| 857 | |
| 858 | |
| 859 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 860 | /* Exception doc strings */ |
| 861 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 862 | static char |
| 863 | AssertionError__doc__[] = "Assertion failed."; |
| 864 | |
| 865 | static char |
| 866 | LookupError__doc__[] = "Base class for lookup errors."; |
| 867 | |
| 868 | static char |
| 869 | IndexError__doc__[] = "Sequence index out of range."; |
| 870 | |
| 871 | static char |
| 872 | KeyError__doc__[] = "Mapping key not found."; |
| 873 | |
| 874 | static char |
| 875 | ArithmeticError__doc__[] = "Base class for arithmetic errors."; |
| 876 | |
| 877 | static char |
| 878 | OverflowError__doc__[] = "Result too large to be represented."; |
| 879 | |
| 880 | static char |
| 881 | ZeroDivisionError__doc__[] = |
| 882 | "Second argument to a division or modulo operation was zero."; |
| 883 | |
| 884 | static char |
| 885 | FloatingPointError__doc__[] = "Floating point operation failed."; |
| 886 | |
| 887 | static char |
| 888 | ValueError__doc__[] = "Inappropriate argument value (of correct type)."; |
| 889 | |
| 890 | static char |
| 891 | UnicodeError__doc__[] = "Unicode related error."; |
| 892 | |
| 893 | static char |
| 894 | SystemError__doc__[] = "Internal error in the Python interpreter.\n\ |
| 895 | \n\ |
| 896 | Please report this to the Python maintainer, along with the traceback,\n\ |
| 897 | the Python version, and the hardware/OS platform and version."; |
| 898 | |
| 899 | static char |
Fred Drake | bb9fa21 | 2001-10-05 21:50:08 +0000 | [diff] [blame] | 900 | ReferenceError__doc__[] = "Weak ref proxy used after referent went away."; |
| 901 | |
| 902 | static char |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 903 | MemoryError__doc__[] = "Out of memory."; |
| 904 | |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 905 | static char |
| 906 | IndentationError__doc__[] = "Improper indentation."; |
| 907 | |
| 908 | static char |
| 909 | TabError__doc__[] = "Improper mixture of spaces and tabs."; |
| 910 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 911 | /* Warning category docstrings */ |
| 912 | |
| 913 | static char |
| 914 | Warning__doc__[] = "Base class for warning categories."; |
| 915 | |
| 916 | static char |
| 917 | UserWarning__doc__[] = "Base class for warnings generated by user code."; |
| 918 | |
| 919 | static char |
| 920 | DeprecationWarning__doc__[] = |
| 921 | "Base class for warnings about deprecated features."; |
| 922 | |
| 923 | static char |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 924 | PendingDeprecationWarning__doc__[] = |
| 925 | "Base class for warnings about features which will be deprecated " |
| 926 | "in the future."; |
| 927 | |
| 928 | static char |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 929 | SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax."; |
| 930 | |
| 931 | static char |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame] | 932 | OverflowWarning__doc__[] = "Base class for warnings about numeric overflow."; |
| 933 | |
| 934 | static char |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 935 | RuntimeWarning__doc__[] = |
| 936 | "Base class for warnings about dubious runtime behavior."; |
| 937 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 938 | |
| 939 | |
| 940 | /* module global functions */ |
| 941 | static PyMethodDef functions[] = { |
| 942 | /* Sentinel */ |
| 943 | {NULL, NULL} |
| 944 | }; |
| 945 | |
| 946 | |
| 947 | |
| 948 | /* Global C API defined exceptions */ |
| 949 | |
| 950 | PyObject *PyExc_Exception; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 951 | PyObject *PyExc_StopIteration; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 952 | PyObject *PyExc_StandardError; |
| 953 | PyObject *PyExc_ArithmeticError; |
| 954 | PyObject *PyExc_LookupError; |
| 955 | |
| 956 | PyObject *PyExc_AssertionError; |
| 957 | PyObject *PyExc_AttributeError; |
| 958 | PyObject *PyExc_EOFError; |
| 959 | PyObject *PyExc_FloatingPointError; |
| 960 | PyObject *PyExc_EnvironmentError; |
| 961 | PyObject *PyExc_IOError; |
| 962 | PyObject *PyExc_OSError; |
| 963 | PyObject *PyExc_ImportError; |
| 964 | PyObject *PyExc_IndexError; |
| 965 | PyObject *PyExc_KeyError; |
| 966 | PyObject *PyExc_KeyboardInterrupt; |
| 967 | PyObject *PyExc_MemoryError; |
| 968 | PyObject *PyExc_NameError; |
| 969 | PyObject *PyExc_OverflowError; |
| 970 | PyObject *PyExc_RuntimeError; |
| 971 | PyObject *PyExc_NotImplementedError; |
| 972 | PyObject *PyExc_SyntaxError; |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 973 | PyObject *PyExc_IndentationError; |
| 974 | PyObject *PyExc_TabError; |
Fred Drake | bb9fa21 | 2001-10-05 21:50:08 +0000 | [diff] [blame] | 975 | PyObject *PyExc_ReferenceError; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 976 | PyObject *PyExc_SystemError; |
| 977 | PyObject *PyExc_SystemExit; |
| 978 | PyObject *PyExc_UnboundLocalError; |
| 979 | PyObject *PyExc_UnicodeError; |
| 980 | PyObject *PyExc_TypeError; |
| 981 | PyObject *PyExc_ValueError; |
| 982 | PyObject *PyExc_ZeroDivisionError; |
| 983 | #ifdef MS_WINDOWS |
| 984 | PyObject *PyExc_WindowsError; |
| 985 | #endif |
| 986 | |
| 987 | /* Pre-computed MemoryError instance. Best to create this as early as |
| 988 | * possibly and not wait until a MemoryError is actually raised! |
| 989 | */ |
| 990 | PyObject *PyExc_MemoryErrorInst; |
| 991 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 992 | /* Predefined warning categories */ |
| 993 | PyObject *PyExc_Warning; |
| 994 | PyObject *PyExc_UserWarning; |
| 995 | PyObject *PyExc_DeprecationWarning; |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 996 | PyObject *PyExc_PendingDeprecationWarning; |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 997 | PyObject *PyExc_SyntaxWarning; |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame] | 998 | PyObject *PyExc_OverflowWarning; |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 999 | PyObject *PyExc_RuntimeWarning; |
| 1000 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1001 | |
| 1002 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1003 | /* mapping between exception names and their PyObject ** */ |
| 1004 | static struct { |
| 1005 | char *name; |
| 1006 | PyObject **exc; |
| 1007 | PyObject **base; /* NULL == PyExc_StandardError */ |
| 1008 | char *docstr; |
| 1009 | PyMethodDef *methods; |
| 1010 | int (*classinit)(PyObject *); |
| 1011 | } exctable[] = { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1012 | /* |
| 1013 | * The first three classes MUST appear in exactly this order |
| 1014 | */ |
| 1015 | {"Exception", &PyExc_Exception}, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1016 | {"StopIteration", &PyExc_StopIteration, &PyExc_Exception, |
| 1017 | StopIteration__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1018 | {"StandardError", &PyExc_StandardError, &PyExc_Exception, |
| 1019 | StandardError__doc__}, |
| 1020 | {"TypeError", &PyExc_TypeError, 0, TypeError__doc__}, |
| 1021 | /* |
| 1022 | * The rest appear in depth-first order of the hierarchy |
| 1023 | */ |
| 1024 | {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__, |
| 1025 | SystemExit_methods}, |
| 1026 | {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__}, |
| 1027 | {"ImportError", &PyExc_ImportError, 0, ImportError__doc__}, |
| 1028 | {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__, |
| 1029 | EnvironmentError_methods}, |
| 1030 | {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__}, |
| 1031 | {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__}, |
| 1032 | #ifdef MS_WINDOWS |
Mark Hammond | 557a044 | 2000-08-15 00:37:32 +0000 | [diff] [blame] | 1033 | {"WindowsError", &PyExc_WindowsError, &PyExc_OSError, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1034 | WindowsError__doc__}, |
| 1035 | #endif /* MS_WINDOWS */ |
| 1036 | {"EOFError", &PyExc_EOFError, 0, EOFError__doc__}, |
| 1037 | {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__}, |
| 1038 | {"NotImplementedError", &PyExc_NotImplementedError, |
| 1039 | &PyExc_RuntimeError, NotImplementedError__doc__}, |
| 1040 | {"NameError", &PyExc_NameError, 0, NameError__doc__}, |
| 1041 | {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError, |
| 1042 | UnboundLocalError__doc__}, |
| 1043 | {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__}, |
| 1044 | {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__, |
| 1045 | SyntaxError_methods, SyntaxError__classinit__}, |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1046 | {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError, |
| 1047 | IndentationError__doc__}, |
| 1048 | {"TabError", &PyExc_TabError, &PyExc_IndentationError, |
| 1049 | TabError__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1050 | {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__}, |
| 1051 | {"LookupError", &PyExc_LookupError, 0, LookupError__doc__}, |
| 1052 | {"IndexError", &PyExc_IndexError, &PyExc_LookupError, |
| 1053 | IndexError__doc__}, |
| 1054 | {"KeyError", &PyExc_KeyError, &PyExc_LookupError, |
| 1055 | KeyError__doc__}, |
| 1056 | {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__}, |
| 1057 | {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError, |
| 1058 | OverflowError__doc__}, |
| 1059 | {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError, |
| 1060 | ZeroDivisionError__doc__}, |
| 1061 | {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError, |
| 1062 | FloatingPointError__doc__}, |
| 1063 | {"ValueError", &PyExc_ValueError, 0, ValueError__doc__}, |
| 1064 | {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__}, |
Fred Drake | bb9fa21 | 2001-10-05 21:50:08 +0000 | [diff] [blame] | 1065 | {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1066 | {"SystemError", &PyExc_SystemError, 0, SystemError__doc__}, |
| 1067 | {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1068 | /* Warning categories */ |
| 1069 | {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__}, |
| 1070 | {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__}, |
| 1071 | {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning, |
| 1072 | DeprecationWarning__doc__}, |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 1073 | {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning, |
| 1074 | PendingDeprecationWarning__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1075 | {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__}, |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame] | 1076 | {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning, |
| 1077 | OverflowWarning__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1078 | {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning, |
| 1079 | RuntimeWarning__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1080 | /* Sentinel */ |
| 1081 | {NULL} |
| 1082 | }; |
| 1083 | |
| 1084 | |
| 1085 | |
Tim Peters | 5687ffe | 2001-02-28 16:44:18 +0000 | [diff] [blame] | 1086 | DL_EXPORT(void) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1087 | _PyExc_Init(void) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1088 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1089 | char *modulename = "exceptions"; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1090 | int modnamesz = strlen(modulename); |
| 1091 | int i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1092 | PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1093 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1094 | me = Py_InitModule(modulename, functions); |
| 1095 | if (me == NULL) |
| 1096 | goto err; |
| 1097 | mydict = PyModule_GetDict(me); |
| 1098 | if (mydict == NULL) |
| 1099 | goto err; |
| 1100 | bltinmod = PyImport_ImportModule("__builtin__"); |
| 1101 | if (bltinmod == NULL) |
| 1102 | goto err; |
| 1103 | bdict = PyModule_GetDict(bltinmod); |
| 1104 | if (bdict == NULL) |
| 1105 | goto err; |
| 1106 | doc = PyString_FromString(module__doc__); |
| 1107 | if (doc == NULL) |
| 1108 | goto err; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1109 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1110 | i = PyDict_SetItemString(mydict, "__doc__", doc); |
Barry Warsaw | 8fcaa92 | 2000-07-01 04:45:52 +0000 | [diff] [blame] | 1111 | Py_DECREF(doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1112 | if (i < 0) { |
| 1113 | err: |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1114 | Py_FatalError("exceptions bootstrapping error."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1115 | return; |
| 1116 | } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1117 | |
| 1118 | /* This is the base class of all exceptions, so make it first. */ |
| 1119 | if (make_Exception(modulename) || |
| 1120 | PyDict_SetItemString(mydict, "Exception", PyExc_Exception) || |
| 1121 | PyDict_SetItemString(bdict, "Exception", PyExc_Exception)) |
| 1122 | { |
| 1123 | Py_FatalError("Base class `Exception' could not be created."); |
| 1124 | } |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 1125 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1126 | /* Now we can programmatically create all the remaining exceptions. |
| 1127 | * Remember to start the loop at 1 to skip Exceptions. |
| 1128 | */ |
| 1129 | for (i=1; exctable[i].name; i++) { |
| 1130 | int status; |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1131 | char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2); |
| 1132 | PyObject *base; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1133 | |
| 1134 | (void)strcpy(cname, modulename); |
| 1135 | (void)strcat(cname, "."); |
| 1136 | (void)strcat(cname, exctable[i].name); |
| 1137 | |
| 1138 | if (exctable[i].base == 0) |
| 1139 | base = PyExc_StandardError; |
| 1140 | else |
| 1141 | base = *exctable[i].base; |
| 1142 | |
| 1143 | status = make_class(exctable[i].exc, base, cname, |
| 1144 | exctable[i].methods, |
| 1145 | exctable[i].docstr); |
| 1146 | |
| 1147 | PyMem_DEL(cname); |
| 1148 | |
| 1149 | if (status) |
| 1150 | Py_FatalError("Standard exception classes could not be created."); |
| 1151 | |
| 1152 | if (exctable[i].classinit) { |
| 1153 | status = (*exctable[i].classinit)(*exctable[i].exc); |
| 1154 | if (status) |
| 1155 | Py_FatalError("An exception class could not be initialized."); |
| 1156 | } |
| 1157 | |
| 1158 | /* Now insert the class into both this module and the __builtin__ |
| 1159 | * module. |
| 1160 | */ |
| 1161 | if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) || |
| 1162 | PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc)) |
| 1163 | { |
| 1164 | Py_FatalError("Module dictionary insertion problem."); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /* Now we need to pre-allocate a MemoryError instance */ |
| 1169 | args = Py_BuildValue("()"); |
| 1170 | if (!args || |
| 1171 | !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args))) |
| 1172 | { |
| 1173 | Py_FatalError("Cannot pre-allocate MemoryError instance\n"); |
| 1174 | } |
| 1175 | Py_DECREF(args); |
| 1176 | |
| 1177 | /* We're done with __builtin__ */ |
| 1178 | Py_DECREF(bltinmod); |
| 1179 | } |
| 1180 | |
| 1181 | |
Tim Peters | 5687ffe | 2001-02-28 16:44:18 +0000 | [diff] [blame] | 1182 | DL_EXPORT(void) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1183 | _PyExc_Fini(void) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1184 | { |
| 1185 | int i; |
| 1186 | |
| 1187 | Py_XDECREF(PyExc_MemoryErrorInst); |
| 1188 | PyExc_MemoryErrorInst = NULL; |
| 1189 | |
| 1190 | for (i=0; exctable[i].name; i++) { |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 1191 | /* clear the class's dictionary, freeing up circular references |
| 1192 | * between the class and its methods. |
| 1193 | */ |
| 1194 | PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__"); |
| 1195 | PyDict_Clear(cdict); |
| 1196 | Py_DECREF(cdict); |
| 1197 | |
| 1198 | /* Now decref the exception class */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1199 | Py_XDECREF(*exctable[i].exc); |
| 1200 | *exctable[i].exc = NULL; |
| 1201 | } |
| 1202 | } |