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