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