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