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