Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * New exceptions.c written in Iceland by Richard Jones and Georg Brandl. |
| 3 | * |
| 4 | * Thanks go to Tim Peters and Michael Hudson for debugging. |
| 5 | */ |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | #define PY_SSIZE_T_CLEAN |
| 8 | #include <Python.h> |
| 9 | #include "structmember.h" |
| 10 | #include "osdefs.h" |
| 11 | |
| 12 | #define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | |
| 14 | /* NOTE: If the exception class hierarchy changes, don't forget to update |
| 15 | * Lib/test/exception_hierarchy.txt |
| 16 | */ |
| 17 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18 | /* |
| 19 | * BaseException |
| 20 | */ |
| 21 | static PyObject * |
| 22 | BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 23 | { |
| 24 | PyBaseExceptionObject *self; |
| 25 | |
| 26 | self = (PyBaseExceptionObject *)type->tp_alloc(type, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 27 | if (!self) |
| 28 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 29 | /* the dict is created on the fly in PyObject_GenericSetAttr */ |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 30 | self->dict = NULL; |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 31 | self->traceback = self->cause = self->context = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 32 | |
| 33 | self->args = PyTuple_New(0); |
| 34 | if (!self->args) { |
| 35 | Py_DECREF(self); |
| 36 | return NULL; |
| 37 | } |
| 38 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 39 | return (PyObject *)self; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) |
| 44 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 45 | if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 46 | return -1; |
| 47 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 48 | Py_DECREF(self->args); |
| 49 | self->args = args; |
| 50 | Py_INCREF(self->args); |
| 51 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 55 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 56 | BaseException_clear(PyBaseExceptionObject *self) |
| 57 | { |
| 58 | Py_CLEAR(self->dict); |
| 59 | Py_CLEAR(self->args); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 60 | Py_CLEAR(self->traceback); |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 61 | Py_CLEAR(self->cause); |
| 62 | Py_CLEAR(self->context); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | BaseException_dealloc(PyBaseExceptionObject *self) |
| 68 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 69 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 70 | BaseException_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 71 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 74 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 75 | BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg) |
| 76 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 77 | Py_VISIT(self->dict); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 78 | Py_VISIT(self->args); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 79 | Py_VISIT(self->traceback); |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 80 | Py_VISIT(self->cause); |
| 81 | Py_VISIT(self->context); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static PyObject * |
| 86 | BaseException_str(PyBaseExceptionObject *self) |
| 87 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 88 | switch (PyTuple_GET_SIZE(self->args)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 89 | case 0: |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 90 | return PyUnicode_FromString(""); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 91 | case 1: |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 92 | return PyObject_Str(PyTuple_GET_ITEM(self->args, 0)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 93 | default: |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 94 | return PyObject_Str(self->args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 95 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static PyObject * |
| 99 | BaseException_repr(PyBaseExceptionObject *self) |
| 100 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 101 | char *name; |
| 102 | char *dot; |
| 103 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 104 | name = (char *)Py_TYPE(self)->tp_name; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 105 | dot = strrchr(name, '.'); |
| 106 | if (dot != NULL) name = dot+1; |
| 107 | |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 108 | return PyUnicode_FromFormat("%s%R", name, self->args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* Pickling support */ |
| 112 | static PyObject * |
| 113 | BaseException_reduce(PyBaseExceptionObject *self) |
| 114 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 115 | if (self->args && self->dict) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 116 | return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 117 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 118 | return PyTuple_Pack(2, Py_TYPE(self), self->args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 121 | /* |
| 122 | * Needed for backward compatibility, since exceptions used to store |
| 123 | * all their attributes in the __dict__. Code is taken from cPickle's |
| 124 | * load_build function. |
| 125 | */ |
| 126 | static PyObject * |
| 127 | BaseException_setstate(PyObject *self, PyObject *state) |
| 128 | { |
| 129 | PyObject *d_key, *d_value; |
| 130 | Py_ssize_t i = 0; |
| 131 | |
| 132 | if (state != Py_None) { |
| 133 | if (!PyDict_Check(state)) { |
| 134 | PyErr_SetString(PyExc_TypeError, "state is not a dictionary"); |
| 135 | return NULL; |
| 136 | } |
| 137 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 138 | if (PyObject_SetAttr(self, d_key, d_value) < 0) |
| 139 | return NULL; |
| 140 | } |
| 141 | } |
| 142 | Py_RETURN_NONE; |
| 143 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 144 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 145 | static PyObject * |
| 146 | BaseException_with_traceback(PyObject *self, PyObject *tb) { |
| 147 | if (PyException_SetTraceback(self, tb)) |
| 148 | return NULL; |
| 149 | |
| 150 | Py_INCREF(self); |
| 151 | return self; |
| 152 | } |
| 153 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 154 | |
| 155 | static PyMethodDef BaseException_methods[] = { |
| 156 | {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS }, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 157 | {"__setstate__", (PyCFunction)BaseException_setstate, METH_O }, |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 158 | {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O }, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 159 | {NULL, NULL, 0, NULL}, |
| 160 | }; |
| 161 | |
| 162 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 163 | static PyObject * |
| 164 | BaseException_get_dict(PyBaseExceptionObject *self) |
| 165 | { |
| 166 | if (self->dict == NULL) { |
| 167 | self->dict = PyDict_New(); |
| 168 | if (!self->dict) |
| 169 | return NULL; |
| 170 | } |
| 171 | Py_INCREF(self->dict); |
| 172 | return self->dict; |
| 173 | } |
| 174 | |
| 175 | static int |
| 176 | BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val) |
| 177 | { |
| 178 | if (val == NULL) { |
| 179 | PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted"); |
| 180 | return -1; |
| 181 | } |
| 182 | if (!PyDict_Check(val)) { |
| 183 | PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary"); |
| 184 | return -1; |
| 185 | } |
| 186 | Py_CLEAR(self->dict); |
| 187 | Py_INCREF(val); |
| 188 | self->dict = val; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static PyObject * |
| 193 | BaseException_get_args(PyBaseExceptionObject *self) |
| 194 | { |
| 195 | if (self->args == NULL) { |
| 196 | Py_INCREF(Py_None); |
| 197 | return Py_None; |
| 198 | } |
| 199 | Py_INCREF(self->args); |
| 200 | return self->args; |
| 201 | } |
| 202 | |
| 203 | static int |
| 204 | BaseException_set_args(PyBaseExceptionObject *self, PyObject *val) |
| 205 | { |
| 206 | PyObject *seq; |
| 207 | if (val == NULL) { |
| 208 | PyErr_SetString(PyExc_TypeError, "args may not be deleted"); |
| 209 | return -1; |
| 210 | } |
| 211 | seq = PySequence_Tuple(val); |
| 212 | if (!seq) return -1; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 213 | Py_CLEAR(self->args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 214 | self->args = seq; |
| 215 | return 0; |
| 216 | } |
| 217 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 218 | static PyObject * |
| 219 | BaseException_get_tb(PyBaseExceptionObject *self) |
| 220 | { |
| 221 | if (self->traceback == NULL) { |
| 222 | Py_INCREF(Py_None); |
| 223 | return Py_None; |
| 224 | } |
| 225 | Py_INCREF(self->traceback); |
| 226 | return self->traceback; |
| 227 | } |
| 228 | |
| 229 | static int |
| 230 | BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb) |
| 231 | { |
| 232 | if (tb == NULL) { |
| 233 | PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted"); |
| 234 | return -1; |
| 235 | } |
| 236 | else if (!(tb == Py_None || PyTraceBack_Check(tb))) { |
| 237 | PyErr_SetString(PyExc_TypeError, |
| 238 | "__traceback__ must be a traceback or None"); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | Py_XINCREF(tb); |
| 243 | Py_XDECREF(self->traceback); |
| 244 | self->traceback = tb; |
| 245 | return 0; |
| 246 | } |
| 247 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 248 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 249 | static PyGetSetDef BaseException_getset[] = { |
| 250 | {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict}, |
| 251 | {"args", (getter)BaseException_get_args, (setter)BaseException_set_args}, |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 252 | {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 253 | {NULL}, |
| 254 | }; |
| 255 | |
| 256 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 257 | PyObject * |
| 258 | PyException_GetTraceback(PyObject *self) { |
| 259 | PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self; |
| 260 | Py_XINCREF(base_self->traceback); |
| 261 | return base_self->traceback; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | int |
| 266 | PyException_SetTraceback(PyObject *self, PyObject *tb) { |
| 267 | return BaseException_set_tb((PyBaseExceptionObject *)self, tb); |
| 268 | } |
| 269 | |
| 270 | PyObject * |
| 271 | PyException_GetCause(PyObject *self) { |
| 272 | PyObject *cause = ((PyBaseExceptionObject *)self)->cause; |
| 273 | Py_XINCREF(cause); |
| 274 | return cause; |
| 275 | } |
| 276 | |
| 277 | /* Steals a reference to cause */ |
| 278 | void |
| 279 | PyException_SetCause(PyObject *self, PyObject *cause) { |
| 280 | PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause; |
| 281 | ((PyBaseExceptionObject *)self)->cause = cause; |
| 282 | Py_XDECREF(old_cause); |
| 283 | } |
| 284 | |
| 285 | PyObject * |
| 286 | PyException_GetContext(PyObject *self) { |
| 287 | PyObject *context = ((PyBaseExceptionObject *)self)->context; |
| 288 | Py_XINCREF(context); |
| 289 | return context; |
| 290 | } |
| 291 | |
| 292 | /* Steals a reference to context */ |
| 293 | void |
| 294 | PyException_SetContext(PyObject *self, PyObject *context) { |
| 295 | PyObject *old_context = ((PyBaseExceptionObject *)self)->context; |
| 296 | ((PyBaseExceptionObject *)self)->context = context; |
| 297 | Py_XDECREF(old_context); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | static PyMemberDef BaseException_members[] = { |
| 302 | {"__context__", T_OBJECT, offsetof(PyBaseExceptionObject, context), 0, |
| 303 | PyDoc_STR("exception context")}, |
| 304 | {"__cause__", T_OBJECT, offsetof(PyBaseExceptionObject, cause), 0, |
| 305 | PyDoc_STR("exception cause")}, |
| 306 | {NULL} /* Sentinel */ |
| 307 | }; |
| 308 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 309 | static PyTypeObject _PyExc_BaseException = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 310 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 311 | "BaseException", /*tp_name*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 312 | sizeof(PyBaseExceptionObject), /*tp_basicsize*/ |
| 313 | 0, /*tp_itemsize*/ |
| 314 | (destructor)BaseException_dealloc, /*tp_dealloc*/ |
| 315 | 0, /*tp_print*/ |
| 316 | 0, /*tp_getattr*/ |
| 317 | 0, /*tp_setattr*/ |
| 318 | 0, /* tp_compare; */ |
| 319 | (reprfunc)BaseException_repr, /*tp_repr*/ |
| 320 | 0, /*tp_as_number*/ |
Brett Cannon | ba7bf49 | 2007-02-27 00:15:55 +0000 | [diff] [blame] | 321 | 0, /*tp_as_sequence*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 322 | 0, /*tp_as_mapping*/ |
| 323 | 0, /*tp_hash */ |
| 324 | 0, /*tp_call*/ |
| 325 | (reprfunc)BaseException_str, /*tp_str*/ |
| 326 | PyObject_GenericGetAttr, /*tp_getattro*/ |
| 327 | PyObject_GenericSetAttr, /*tp_setattro*/ |
| 328 | 0, /*tp_as_buffer*/ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 329 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 330 | Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 331 | PyDoc_STR("Common base class for all exceptions"), /* tp_doc */ |
| 332 | (traverseproc)BaseException_traverse, /* tp_traverse */ |
| 333 | (inquiry)BaseException_clear, /* tp_clear */ |
| 334 | 0, /* tp_richcompare */ |
| 335 | 0, /* tp_weaklistoffset */ |
| 336 | 0, /* tp_iter */ |
| 337 | 0, /* tp_iternext */ |
| 338 | BaseException_methods, /* tp_methods */ |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 339 | BaseException_members, /* tp_members */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 340 | BaseException_getset, /* tp_getset */ |
| 341 | 0, /* tp_base */ |
| 342 | 0, /* tp_dict */ |
| 343 | 0, /* tp_descr_get */ |
| 344 | 0, /* tp_descr_set */ |
| 345 | offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */ |
| 346 | (initproc)BaseException_init, /* tp_init */ |
| 347 | 0, /* tp_alloc */ |
| 348 | BaseException_new, /* tp_new */ |
| 349 | }; |
| 350 | /* the CPython API expects exceptions to be (PyObject *) - both a hold-over |
| 351 | from the previous implmentation and also allowing Python objects to be used |
| 352 | in the API */ |
| 353 | PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException; |
| 354 | |
| 355 | /* note these macros omit the last semicolon so the macro invocation may |
| 356 | * include it and not look strange. |
| 357 | */ |
| 358 | #define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \ |
| 359 | static PyTypeObject _PyExc_ ## EXCNAME = { \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 360 | PyVarObject_HEAD_INIT(NULL, 0) \ |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 361 | # EXCNAME, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 362 | sizeof(PyBaseExceptionObject), \ |
| 363 | 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \ |
| 364 | 0, 0, 0, 0, 0, 0, 0, \ |
| 365 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ |
| 366 | PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \ |
| 367 | (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ |
| 368 | 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \ |
| 369 | (initproc)BaseException_init, 0, BaseException_new,\ |
| 370 | }; \ |
| 371 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME |
| 372 | |
| 373 | #define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \ |
| 374 | static PyTypeObject _PyExc_ ## EXCNAME = { \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 375 | PyVarObject_HEAD_INIT(NULL, 0) \ |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 376 | # EXCNAME, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 377 | sizeof(Py ## EXCSTORE ## Object), \ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 378 | 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 379 | 0, 0, 0, 0, 0, \ |
| 380 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 381 | PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ |
| 382 | (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 383 | 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 384 | (initproc)EXCSTORE ## _init, 0, BaseException_new,\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 385 | }; \ |
| 386 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME |
| 387 | |
| 388 | #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \ |
| 389 | static PyTypeObject _PyExc_ ## EXCNAME = { \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 390 | PyVarObject_HEAD_INIT(NULL, 0) \ |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 391 | # EXCNAME, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 392 | sizeof(Py ## EXCSTORE ## Object), 0, \ |
| 393 | (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 394 | (reprfunc)EXCSTR, 0, 0, 0, \ |
| 395 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ |
| 396 | PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ |
| 397 | (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \ |
| 398 | EXCMEMBERS, 0, &_ ## EXCBASE, \ |
| 399 | 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 400 | (initproc)EXCSTORE ## _init, 0, BaseException_new,\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 401 | }; \ |
| 402 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME |
| 403 | |
| 404 | |
| 405 | /* |
| 406 | * Exception extends BaseException |
| 407 | */ |
| 408 | SimpleExtendsException(PyExc_BaseException, Exception, |
| 409 | "Common base class for all non-exit exceptions."); |
| 410 | |
| 411 | |
| 412 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 413 | * TypeError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 414 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 415 | SimpleExtendsException(PyExc_Exception, TypeError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 416 | "Inappropriate argument type."); |
| 417 | |
| 418 | |
| 419 | /* |
| 420 | * StopIteration extends Exception |
| 421 | */ |
| 422 | SimpleExtendsException(PyExc_Exception, StopIteration, |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 423 | "Signal the end from iterator.__next__()."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 424 | |
| 425 | |
| 426 | /* |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 427 | * GeneratorExit extends BaseException |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 428 | */ |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 429 | SimpleExtendsException(PyExc_BaseException, GeneratorExit, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 430 | "Request that a generator exit."); |
| 431 | |
| 432 | |
| 433 | /* |
| 434 | * SystemExit extends BaseException |
| 435 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 436 | |
| 437 | static int |
| 438 | SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds) |
| 439 | { |
| 440 | Py_ssize_t size = PyTuple_GET_SIZE(args); |
| 441 | |
| 442 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 443 | return -1; |
| 444 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 445 | if (size == 0) |
| 446 | return 0; |
| 447 | Py_CLEAR(self->code); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 448 | if (size == 1) |
| 449 | self->code = PyTuple_GET_ITEM(args, 0); |
| 450 | else if (size > 1) |
| 451 | self->code = args; |
| 452 | Py_INCREF(self->code); |
| 453 | return 0; |
| 454 | } |
| 455 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 456 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 457 | SystemExit_clear(PySystemExitObject *self) |
| 458 | { |
| 459 | Py_CLEAR(self->code); |
| 460 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 461 | } |
| 462 | |
| 463 | static void |
| 464 | SystemExit_dealloc(PySystemExitObject *self) |
| 465 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 466 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 467 | SystemExit_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 468 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 471 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 472 | SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg) |
| 473 | { |
| 474 | Py_VISIT(self->code); |
| 475 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 476 | } |
| 477 | |
| 478 | static PyMemberDef SystemExit_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0, |
| 480 | PyDoc_STR("exception code")}, |
| 481 | {NULL} /* Sentinel */ |
| 482 | }; |
| 483 | |
| 484 | ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit, |
| 485 | SystemExit_dealloc, 0, SystemExit_members, 0, |
| 486 | "Request to exit from the interpreter."); |
| 487 | |
| 488 | /* |
| 489 | * KeyboardInterrupt extends BaseException |
| 490 | */ |
| 491 | SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, |
| 492 | "Program interrupted by user."); |
| 493 | |
| 494 | |
| 495 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 496 | * ImportError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 497 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 498 | SimpleExtendsException(PyExc_Exception, ImportError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 499 | "Import can't find module, or can't find name in module."); |
| 500 | |
| 501 | |
| 502 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 503 | * EnvironmentError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 504 | */ |
| 505 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 506 | /* Where a function has a single filename, such as open() or some |
| 507 | * of the os module functions, PyErr_SetFromErrnoWithFilename() is |
| 508 | * called, giving a third argument which is the filename. But, so |
| 509 | * that old code using in-place unpacking doesn't break, e.g.: |
| 510 | * |
| 511 | * except IOError, (errno, strerror): |
| 512 | * |
| 513 | * we hack args so that it only contains two items. This also |
| 514 | * means we need our own __str__() which prints out the filename |
| 515 | * when it was supplied. |
| 516 | */ |
| 517 | static int |
| 518 | EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args, |
| 519 | PyObject *kwds) |
| 520 | { |
| 521 | PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL; |
| 522 | PyObject *subslice = NULL; |
| 523 | |
| 524 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 525 | return -1; |
| 526 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 527 | if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 528 | return 0; |
| 529 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 530 | |
| 531 | if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 532 | &myerrno, &strerror, &filename)) { |
| 533 | return -1; |
| 534 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 535 | Py_CLEAR(self->myerrno); /* replacing */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 536 | self->myerrno = myerrno; |
| 537 | Py_INCREF(self->myerrno); |
| 538 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 539 | Py_CLEAR(self->strerror); /* replacing */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 540 | self->strerror = strerror; |
| 541 | Py_INCREF(self->strerror); |
| 542 | |
| 543 | /* self->filename will remain Py_None otherwise */ |
| 544 | if (filename != NULL) { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 545 | Py_CLEAR(self->filename); /* replacing */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 546 | self->filename = filename; |
| 547 | Py_INCREF(self->filename); |
| 548 | |
| 549 | subslice = PyTuple_GetSlice(args, 0, 2); |
| 550 | if (!subslice) |
| 551 | return -1; |
| 552 | |
| 553 | Py_DECREF(self->args); /* replacing args */ |
| 554 | self->args = subslice; |
| 555 | } |
| 556 | return 0; |
| 557 | } |
| 558 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 559 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 560 | EnvironmentError_clear(PyEnvironmentErrorObject *self) |
| 561 | { |
| 562 | Py_CLEAR(self->myerrno); |
| 563 | Py_CLEAR(self->strerror); |
| 564 | Py_CLEAR(self->filename); |
| 565 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 566 | } |
| 567 | |
| 568 | static void |
| 569 | EnvironmentError_dealloc(PyEnvironmentErrorObject *self) |
| 570 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 571 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 572 | EnvironmentError_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 573 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 576 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 577 | EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit, |
| 578 | void *arg) |
| 579 | { |
| 580 | Py_VISIT(self->myerrno); |
| 581 | Py_VISIT(self->strerror); |
| 582 | Py_VISIT(self->filename); |
| 583 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 584 | } |
| 585 | |
| 586 | static PyObject * |
| 587 | EnvironmentError_str(PyEnvironmentErrorObject *self) |
| 588 | { |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 589 | if (self->filename) |
| 590 | return PyUnicode_FromFormat("[Errno %S] %S: %R", |
| 591 | self->myerrno ? self->myerrno: Py_None, |
| 592 | self->strerror ? self->strerror: Py_None, |
| 593 | self->filename); |
| 594 | else if (self->myerrno && self->strerror) |
| 595 | return PyUnicode_FromFormat("[Errno %S] %S", |
| 596 | self->myerrno ? self->myerrno: Py_None, |
| 597 | self->strerror ? self->strerror: Py_None); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 598 | else |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 599 | return BaseException_str((PyBaseExceptionObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static PyMemberDef EnvironmentError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 603 | {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0, |
| 604 | PyDoc_STR("exception errno")}, |
| 605 | {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0, |
| 606 | PyDoc_STR("exception strerror")}, |
| 607 | {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0, |
| 608 | PyDoc_STR("exception filename")}, |
| 609 | {NULL} /* Sentinel */ |
| 610 | }; |
| 611 | |
| 612 | |
| 613 | static PyObject * |
| 614 | EnvironmentError_reduce(PyEnvironmentErrorObject *self) |
| 615 | { |
| 616 | PyObject *args = self->args; |
| 617 | PyObject *res = NULL, *tmp; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 618 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 619 | /* self->args is only the first two real arguments if there was a |
| 620 | * file name given to EnvironmentError. */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 621 | if (PyTuple_GET_SIZE(args) == 2 && self->filename) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 622 | args = PyTuple_New(3); |
| 623 | if (!args) return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 624 | |
| 625 | tmp = PyTuple_GET_ITEM(self->args, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 626 | Py_INCREF(tmp); |
| 627 | PyTuple_SET_ITEM(args, 0, tmp); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 628 | |
| 629 | tmp = PyTuple_GET_ITEM(self->args, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 630 | Py_INCREF(tmp); |
| 631 | PyTuple_SET_ITEM(args, 1, tmp); |
| 632 | |
| 633 | Py_INCREF(self->filename); |
| 634 | PyTuple_SET_ITEM(args, 2, self->filename); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 635 | } else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 636 | Py_INCREF(args); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 637 | |
| 638 | if (self->dict) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 639 | res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 640 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 641 | res = PyTuple_Pack(2, Py_TYPE(self), args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 642 | Py_DECREF(args); |
| 643 | return res; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | static PyMethodDef EnvironmentError_methods[] = { |
| 648 | {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS}, |
| 649 | {NULL} |
| 650 | }; |
| 651 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 652 | ComplexExtendsException(PyExc_Exception, EnvironmentError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 653 | EnvironmentError, EnvironmentError_dealloc, |
| 654 | EnvironmentError_methods, EnvironmentError_members, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 655 | EnvironmentError_str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 656 | "Base class for I/O related errors."); |
| 657 | |
| 658 | |
| 659 | /* |
| 660 | * IOError extends EnvironmentError |
| 661 | */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 662 | MiddlingExtendsException(PyExc_EnvironmentError, IOError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 663 | EnvironmentError, "I/O operation failed."); |
| 664 | |
| 665 | |
| 666 | /* |
| 667 | * OSError extends EnvironmentError |
| 668 | */ |
| 669 | MiddlingExtendsException(PyExc_EnvironmentError, OSError, |
| 670 | EnvironmentError, "OS system call failed."); |
| 671 | |
| 672 | |
| 673 | /* |
| 674 | * WindowsError extends OSError |
| 675 | */ |
| 676 | #ifdef MS_WINDOWS |
| 677 | #include "errmap.h" |
| 678 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 679 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 680 | WindowsError_clear(PyWindowsErrorObject *self) |
| 681 | { |
| 682 | Py_CLEAR(self->myerrno); |
| 683 | Py_CLEAR(self->strerror); |
| 684 | Py_CLEAR(self->filename); |
| 685 | Py_CLEAR(self->winerror); |
| 686 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 687 | } |
| 688 | |
| 689 | static void |
| 690 | WindowsError_dealloc(PyWindowsErrorObject *self) |
| 691 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 692 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 693 | WindowsError_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 694 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 697 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 698 | WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg) |
| 699 | { |
| 700 | Py_VISIT(self->myerrno); |
| 701 | Py_VISIT(self->strerror); |
| 702 | Py_VISIT(self->filename); |
| 703 | Py_VISIT(self->winerror); |
| 704 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 705 | } |
| 706 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 707 | static int |
| 708 | WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds) |
| 709 | { |
| 710 | PyObject *o_errcode = NULL; |
| 711 | long errcode; |
| 712 | long posix_errno; |
| 713 | |
| 714 | if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds) |
| 715 | == -1) |
| 716 | return -1; |
| 717 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 718 | if (self->myerrno == NULL) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 719 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 720 | |
| 721 | /* Set errno to the POSIX errno, and winerror to the Win32 |
| 722 | error code. */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 723 | errcode = PyLong_AsLong(self->myerrno); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 724 | if (errcode == -1 && PyErr_Occurred()) |
| 725 | return -1; |
| 726 | posix_errno = winerror_to_errno(errcode); |
| 727 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 728 | Py_CLEAR(self->winerror); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 729 | self->winerror = self->myerrno; |
| 730 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 731 | o_errcode = PyLong_FromLong(posix_errno); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 732 | if (!o_errcode) |
| 733 | return -1; |
| 734 | |
| 735 | self->myerrno = o_errcode; |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | static PyObject * |
| 742 | WindowsError_str(PyWindowsErrorObject *self) |
| 743 | { |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 744 | if (self->filename) |
| 745 | return PyUnicode_FromFormat("[Error %S] %S: %R", |
| 746 | self->winerror ? self->winerror: Py_None, |
| 747 | self->strerror ? self->strerror: Py_None, |
| 748 | self->filename); |
| 749 | else if (self->winerror && self->strerror) |
| 750 | return PyUnicode_FromFormat("[Error %S] %S", |
| 751 | self->winerror ? self->winerror: Py_None, |
| 752 | self->strerror ? self->strerror: Py_None); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 753 | else |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 754 | return EnvironmentError_str((PyEnvironmentErrorObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static PyMemberDef WindowsError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 758 | {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0, |
| 759 | PyDoc_STR("POSIX exception code")}, |
| 760 | {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0, |
| 761 | PyDoc_STR("exception strerror")}, |
| 762 | {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0, |
| 763 | PyDoc_STR("exception filename")}, |
| 764 | {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0, |
| 765 | PyDoc_STR("Win32 exception code")}, |
| 766 | {NULL} /* Sentinel */ |
| 767 | }; |
| 768 | |
| 769 | ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError, |
| 770 | WindowsError_dealloc, 0, WindowsError_members, |
| 771 | WindowsError_str, "MS-Windows OS system call failed."); |
| 772 | |
| 773 | #endif /* MS_WINDOWS */ |
| 774 | |
| 775 | |
| 776 | /* |
| 777 | * VMSError extends OSError (I think) |
| 778 | */ |
| 779 | #ifdef __VMS |
| 780 | MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError, |
| 781 | "OpenVMS OS system call failed."); |
| 782 | #endif |
| 783 | |
| 784 | |
| 785 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 786 | * EOFError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 787 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 788 | SimpleExtendsException(PyExc_Exception, EOFError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 789 | "Read beyond end of file."); |
| 790 | |
| 791 | |
| 792 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 793 | * RuntimeError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 794 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 795 | SimpleExtendsException(PyExc_Exception, RuntimeError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 796 | "Unspecified run-time error."); |
| 797 | |
| 798 | |
| 799 | /* |
| 800 | * NotImplementedError extends RuntimeError |
| 801 | */ |
| 802 | SimpleExtendsException(PyExc_RuntimeError, NotImplementedError, |
| 803 | "Method or function hasn't been implemented yet."); |
| 804 | |
| 805 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 806 | * NameError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 807 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 808 | SimpleExtendsException(PyExc_Exception, NameError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 809 | "Name not found globally."); |
| 810 | |
| 811 | /* |
| 812 | * UnboundLocalError extends NameError |
| 813 | */ |
| 814 | SimpleExtendsException(PyExc_NameError, UnboundLocalError, |
| 815 | "Local name referenced but not bound to a value."); |
| 816 | |
| 817 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 818 | * AttributeError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 819 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 820 | SimpleExtendsException(PyExc_Exception, AttributeError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 821 | "Attribute not found."); |
| 822 | |
| 823 | |
| 824 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 825 | * SyntaxError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 826 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 827 | |
| 828 | static int |
| 829 | SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds) |
| 830 | { |
| 831 | PyObject *info = NULL; |
| 832 | Py_ssize_t lenargs = PyTuple_GET_SIZE(args); |
| 833 | |
| 834 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 835 | return -1; |
| 836 | |
| 837 | if (lenargs >= 1) { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 838 | Py_CLEAR(self->msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 839 | self->msg = PyTuple_GET_ITEM(args, 0); |
| 840 | Py_INCREF(self->msg); |
| 841 | } |
| 842 | if (lenargs == 2) { |
| 843 | info = PyTuple_GET_ITEM(args, 1); |
| 844 | info = PySequence_Tuple(info); |
| 845 | if (!info) return -1; |
| 846 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 847 | if (PyTuple_GET_SIZE(info) != 4) { |
| 848 | /* not a very good error message, but it's what Python 2.4 gives */ |
| 849 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 850 | Py_DECREF(info); |
| 851 | return -1; |
| 852 | } |
| 853 | |
| 854 | Py_CLEAR(self->filename); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 855 | self->filename = PyTuple_GET_ITEM(info, 0); |
| 856 | Py_INCREF(self->filename); |
| 857 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 858 | Py_CLEAR(self->lineno); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 859 | self->lineno = PyTuple_GET_ITEM(info, 1); |
| 860 | Py_INCREF(self->lineno); |
| 861 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 862 | Py_CLEAR(self->offset); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 863 | self->offset = PyTuple_GET_ITEM(info, 2); |
| 864 | Py_INCREF(self->offset); |
| 865 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 866 | Py_CLEAR(self->text); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 867 | self->text = PyTuple_GET_ITEM(info, 3); |
| 868 | Py_INCREF(self->text); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 869 | |
| 870 | Py_DECREF(info); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 871 | } |
| 872 | return 0; |
| 873 | } |
| 874 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 875 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 876 | SyntaxError_clear(PySyntaxErrorObject *self) |
| 877 | { |
| 878 | Py_CLEAR(self->msg); |
| 879 | Py_CLEAR(self->filename); |
| 880 | Py_CLEAR(self->lineno); |
| 881 | Py_CLEAR(self->offset); |
| 882 | Py_CLEAR(self->text); |
| 883 | Py_CLEAR(self->print_file_and_line); |
| 884 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 885 | } |
| 886 | |
| 887 | static void |
| 888 | SyntaxError_dealloc(PySyntaxErrorObject *self) |
| 889 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 890 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 891 | SyntaxError_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 892 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 895 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 896 | SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg) |
| 897 | { |
| 898 | Py_VISIT(self->msg); |
| 899 | Py_VISIT(self->filename); |
| 900 | Py_VISIT(self->lineno); |
| 901 | Py_VISIT(self->offset); |
| 902 | Py_VISIT(self->text); |
| 903 | Py_VISIT(self->print_file_and_line); |
| 904 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 905 | } |
| 906 | |
| 907 | /* This is called "my_basename" instead of just "basename" to avoid name |
| 908 | conflicts with glibc; basename is already prototyped if _GNU_SOURCE is |
| 909 | defined, and Python does define that. */ |
| 910 | static char * |
| 911 | my_basename(char *name) |
| 912 | { |
| 913 | char *cp = name; |
| 914 | char *result = name; |
| 915 | |
| 916 | if (name == NULL) |
| 917 | return "???"; |
| 918 | while (*cp != '\0') { |
| 919 | if (*cp == SEP) |
| 920 | result = cp + 1; |
| 921 | ++cp; |
| 922 | } |
| 923 | return result; |
| 924 | } |
| 925 | |
| 926 | |
| 927 | static PyObject * |
| 928 | SyntaxError_str(PySyntaxErrorObject *self) |
| 929 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 930 | int have_lineno = 0; |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 931 | char *filename = 0; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 932 | /* Below, we always ignore overflow errors, just printing -1. |
| 933 | Still, we cannot allow an OverflowError to be raised, so |
| 934 | we need to call PyLong_AsLongAndOverflow. */ |
| 935 | int overflow; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 936 | |
| 937 | /* XXX -- do all the additional formatting with filename and |
| 938 | lineno here */ |
| 939 | |
Neal Norwitz | ed2b739 | 2007-08-26 04:51:10 +0000 | [diff] [blame] | 940 | if (self->filename && PyUnicode_Check(self->filename)) { |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 941 | filename = PyUnicode_AsString(self->filename); |
| 942 | } |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 943 | have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 944 | |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 945 | if (!filename && !have_lineno) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 946 | return PyObject_Str(self->msg ? self->msg : Py_None); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 947 | |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 948 | if (filename && have_lineno) |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 949 | return PyUnicode_FromFormat("%S (%s, line %ld)", |
| 950 | self->msg ? self->msg : Py_None, |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 951 | my_basename(filename), |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 952 | PyLong_AsLongAndOverflow(self->lineno, &overflow)); |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 953 | else if (filename) |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 954 | return PyUnicode_FromFormat("%S (%s)", |
| 955 | self->msg ? self->msg : Py_None, |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 956 | my_basename(filename)); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 957 | else /* only have_lineno */ |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 958 | return PyUnicode_FromFormat("%S (line %ld)", |
| 959 | self->msg ? self->msg : Py_None, |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 960 | PyLong_AsLongAndOverflow(self->lineno, &overflow)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static PyMemberDef SyntaxError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 964 | {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0, |
| 965 | PyDoc_STR("exception msg")}, |
| 966 | {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0, |
| 967 | PyDoc_STR("exception filename")}, |
| 968 | {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0, |
| 969 | PyDoc_STR("exception lineno")}, |
| 970 | {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0, |
| 971 | PyDoc_STR("exception offset")}, |
| 972 | {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0, |
| 973 | PyDoc_STR("exception text")}, |
| 974 | {"print_file_and_line", T_OBJECT, |
| 975 | offsetof(PySyntaxErrorObject, print_file_and_line), 0, |
| 976 | PyDoc_STR("exception print_file_and_line")}, |
| 977 | {NULL} /* Sentinel */ |
| 978 | }; |
| 979 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 980 | ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 981 | SyntaxError_dealloc, 0, SyntaxError_members, |
| 982 | SyntaxError_str, "Invalid syntax."); |
| 983 | |
| 984 | |
| 985 | /* |
| 986 | * IndentationError extends SyntaxError |
| 987 | */ |
| 988 | MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError, |
| 989 | "Improper indentation."); |
| 990 | |
| 991 | |
| 992 | /* |
| 993 | * TabError extends IndentationError |
| 994 | */ |
| 995 | MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError, |
| 996 | "Improper mixture of spaces and tabs."); |
| 997 | |
| 998 | |
| 999 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1000 | * LookupError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1001 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1002 | SimpleExtendsException(PyExc_Exception, LookupError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1003 | "Base class for lookup errors."); |
| 1004 | |
| 1005 | |
| 1006 | /* |
| 1007 | * IndexError extends LookupError |
| 1008 | */ |
| 1009 | SimpleExtendsException(PyExc_LookupError, IndexError, |
| 1010 | "Sequence index out of range."); |
| 1011 | |
| 1012 | |
| 1013 | /* |
| 1014 | * KeyError extends LookupError |
| 1015 | */ |
| 1016 | static PyObject * |
| 1017 | KeyError_str(PyBaseExceptionObject *self) |
| 1018 | { |
| 1019 | /* If args is a tuple of exactly one item, apply repr to args[0]. |
| 1020 | This is done so that e.g. the exception raised by {}[''] prints |
| 1021 | KeyError: '' |
| 1022 | rather than the confusing |
| 1023 | KeyError |
| 1024 | alone. The downside is that if KeyError is raised with an explanatory |
| 1025 | string, that string will be displayed in quotes. Too bad. |
| 1026 | If args is anything else, use the default BaseException__str__(). |
| 1027 | */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1028 | if (PyTuple_GET_SIZE(self->args) == 1) { |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 1029 | return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1030 | } |
| 1031 | return BaseException_str(self); |
| 1032 | } |
| 1033 | |
| 1034 | ComplexExtendsException(PyExc_LookupError, KeyError, BaseException, |
| 1035 | 0, 0, 0, KeyError_str, "Mapping key not found."); |
| 1036 | |
| 1037 | |
| 1038 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1039 | * ValueError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1040 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1041 | SimpleExtendsException(PyExc_Exception, ValueError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1042 | "Inappropriate argument value (of correct type)."); |
| 1043 | |
| 1044 | /* |
| 1045 | * UnicodeError extends ValueError |
| 1046 | */ |
| 1047 | |
| 1048 | SimpleExtendsException(PyExc_ValueError, UnicodeError, |
| 1049 | "Unicode related error."); |
| 1050 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1051 | static PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1052 | get_string(PyObject *attr, const char *name) |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 1053 | { |
| 1054 | if (!attr) { |
| 1055 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1059 | if (!PyString_Check(attr)) { |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 1060 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name); |
| 1061 | return NULL; |
| 1062 | } |
| 1063 | Py_INCREF(attr); |
| 1064 | return attr; |
| 1065 | } |
| 1066 | |
| 1067 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1068 | get_unicode(PyObject *attr, const char *name) |
| 1069 | { |
| 1070 | if (!attr) { |
| 1071 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 1072 | return NULL; |
| 1073 | } |
| 1074 | |
| 1075 | if (!PyUnicode_Check(attr)) { |
| 1076 | PyErr_Format(PyExc_TypeError, |
| 1077 | "%.200s attribute must be unicode", name); |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | Py_INCREF(attr); |
| 1081 | return attr; |
| 1082 | } |
| 1083 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1084 | static int |
| 1085 | set_unicodefromstring(PyObject **attr, const char *value) |
| 1086 | { |
| 1087 | PyObject *obj = PyUnicode_FromString(value); |
| 1088 | if (!obj) |
| 1089 | return -1; |
| 1090 | Py_CLEAR(*attr); |
| 1091 | *attr = obj; |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1095 | PyObject * |
| 1096 | PyUnicodeEncodeError_GetEncoding(PyObject *exc) |
| 1097 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1098 | return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | PyObject * |
| 1102 | PyUnicodeDecodeError_GetEncoding(PyObject *exc) |
| 1103 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1104 | return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | PyObject * |
| 1108 | PyUnicodeEncodeError_GetObject(PyObject *exc) |
| 1109 | { |
| 1110 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1111 | } |
| 1112 | |
| 1113 | PyObject * |
| 1114 | PyUnicodeDecodeError_GetObject(PyObject *exc) |
| 1115 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1116 | return get_string(((PyUnicodeErrorObject *)exc)->object, "object"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | PyObject * |
| 1120 | PyUnicodeTranslateError_GetObject(PyObject *exc) |
| 1121 | { |
| 1122 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1123 | } |
| 1124 | |
| 1125 | int |
| 1126 | PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1127 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1128 | Py_ssize_t size; |
| 1129 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1130 | "object"); |
| 1131 | if (!obj) |
| 1132 | return -1; |
| 1133 | *start = ((PyUnicodeErrorObject *)exc)->start; |
| 1134 | size = PyUnicode_GET_SIZE(obj); |
| 1135 | if (*start<0) |
| 1136 | *start = 0; /*XXX check for values <0*/ |
| 1137 | if (*start>=size) |
| 1138 | *start = size-1; |
| 1139 | Py_DECREF(obj); |
| 1140 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | |
| 1144 | int |
| 1145 | PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1146 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1147 | Py_ssize_t size; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1148 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object"); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1149 | if (!obj) |
| 1150 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1151 | size = PyString_GET_SIZE(obj); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1152 | *start = ((PyUnicodeErrorObject *)exc)->start; |
| 1153 | if (*start<0) |
| 1154 | *start = 0; |
| 1155 | if (*start>=size) |
| 1156 | *start = size-1; |
| 1157 | Py_DECREF(obj); |
| 1158 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | int |
| 1163 | PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1164 | { |
| 1165 | return PyUnicodeEncodeError_GetStart(exc, start); |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | int |
| 1170 | PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1171 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1172 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1173 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | int |
| 1178 | PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1179 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1180 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1181 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | int |
| 1186 | PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1187 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1188 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1189 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | |
| 1193 | int |
| 1194 | PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1195 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1196 | Py_ssize_t size; |
| 1197 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1198 | "object"); |
| 1199 | if (!obj) |
| 1200 | return -1; |
| 1201 | *end = ((PyUnicodeErrorObject *)exc)->end; |
| 1202 | size = PyUnicode_GET_SIZE(obj); |
| 1203 | if (*end<1) |
| 1204 | *end = 1; |
| 1205 | if (*end>size) |
| 1206 | *end = size; |
| 1207 | Py_DECREF(obj); |
| 1208 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | |
| 1212 | int |
| 1213 | PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1214 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1215 | Py_ssize_t size; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1216 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object"); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1217 | if (!obj) |
| 1218 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1219 | size = PyString_GET_SIZE(obj); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1220 | *end = ((PyUnicodeErrorObject *)exc)->end; |
| 1221 | if (*end<1) |
| 1222 | *end = 1; |
| 1223 | if (*end>size) |
| 1224 | *end = size; |
| 1225 | Py_DECREF(obj); |
| 1226 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | int |
| 1231 | PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start) |
| 1232 | { |
| 1233 | return PyUnicodeEncodeError_GetEnd(exc, start); |
| 1234 | } |
| 1235 | |
| 1236 | |
| 1237 | int |
| 1238 | PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1239 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1240 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1241 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | int |
| 1246 | PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1247 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1248 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1249 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | |
| 1253 | int |
| 1254 | PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1255 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1256 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1257 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | PyObject * |
| 1261 | PyUnicodeEncodeError_GetReason(PyObject *exc) |
| 1262 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1263 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | PyObject * |
| 1268 | PyUnicodeDecodeError_GetReason(PyObject *exc) |
| 1269 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1270 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | PyObject * |
| 1275 | PyUnicodeTranslateError_GetReason(PyObject *exc) |
| 1276 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1277 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | |
| 1281 | int |
| 1282 | PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason) |
| 1283 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1284 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1285 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | int |
| 1290 | PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason) |
| 1291 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1292 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1293 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | int |
| 1298 | PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason) |
| 1299 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1300 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1301 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1305 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1306 | UnicodeError_clear(PyUnicodeErrorObject *self) |
| 1307 | { |
| 1308 | Py_CLEAR(self->encoding); |
| 1309 | Py_CLEAR(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1310 | Py_CLEAR(self->reason); |
| 1311 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 1312 | } |
| 1313 | |
| 1314 | static void |
| 1315 | UnicodeError_dealloc(PyUnicodeErrorObject *self) |
| 1316 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1317 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1318 | UnicodeError_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1319 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1322 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1323 | UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg) |
| 1324 | { |
| 1325 | Py_VISIT(self->encoding); |
| 1326 | Py_VISIT(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1327 | Py_VISIT(self->reason); |
| 1328 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 1329 | } |
| 1330 | |
| 1331 | static PyMemberDef UnicodeError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1332 | {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0, |
| 1333 | PyDoc_STR("exception encoding")}, |
| 1334 | {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0, |
| 1335 | PyDoc_STR("exception object")}, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1336 | {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1337 | PyDoc_STR("exception start")}, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1338 | {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1339 | PyDoc_STR("exception end")}, |
| 1340 | {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0, |
| 1341 | PyDoc_STR("exception reason")}, |
| 1342 | {NULL} /* Sentinel */ |
| 1343 | }; |
| 1344 | |
| 1345 | |
| 1346 | /* |
| 1347 | * UnicodeEncodeError extends UnicodeError |
| 1348 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1349 | |
| 1350 | static int |
| 1351 | UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1352 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1353 | PyUnicodeErrorObject *err; |
| 1354 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1355 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1356 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1357 | |
| 1358 | err = (PyUnicodeErrorObject *)self; |
| 1359 | |
| 1360 | Py_CLEAR(err->encoding); |
| 1361 | Py_CLEAR(err->object); |
| 1362 | Py_CLEAR(err->reason); |
| 1363 | |
| 1364 | if (!PyArg_ParseTuple(args, "O!O!nnO!", |
| 1365 | &PyUnicode_Type, &err->encoding, |
| 1366 | &PyUnicode_Type, &err->object, |
| 1367 | &err->start, |
| 1368 | &err->end, |
| 1369 | &PyUnicode_Type, &err->reason)) { |
| 1370 | err->encoding = err->object = err->reason = NULL; |
| 1371 | return -1; |
| 1372 | } |
| 1373 | |
| 1374 | Py_INCREF(err->encoding); |
| 1375 | Py_INCREF(err->object); |
| 1376 | Py_INCREF(err->reason); |
| 1377 | |
| 1378 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | static PyObject * |
| 1382 | UnicodeEncodeError_str(PyObject *self) |
| 1383 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1384 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1385 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1386 | if (uself->end==uself->start+1) { |
| 1387 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1388 | const char *fmt; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1389 | if (badchar <= 0xff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1390 | fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1391 | else if (badchar <= 0xffff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1392 | fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1393 | else |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1394 | fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U"; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1395 | return PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1396 | fmt, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1397 | ((PyUnicodeErrorObject *)self)->encoding, |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1398 | badchar, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1399 | uself->start, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1400 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1401 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1402 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1403 | return PyUnicode_FromFormat( |
| 1404 | "'%U' codec can't encode characters in position %zd-%zd: %U", |
| 1405 | ((PyUnicodeErrorObject *)self)->encoding, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1406 | uself->start, |
| 1407 | uself->end-1, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1408 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1409 | ); |
| 1410 | } |
| 1411 | |
| 1412 | static PyTypeObject _PyExc_UnicodeEncodeError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1413 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1414 | "UnicodeEncodeError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1415 | sizeof(PyUnicodeErrorObject), 0, |
| 1416 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1417 | (reprfunc)UnicodeEncodeError_str, 0, 0, 0, |
| 1418 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1419 | PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse, |
| 1420 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1421 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1422 | (initproc)UnicodeEncodeError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1423 | }; |
| 1424 | PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError; |
| 1425 | |
| 1426 | PyObject * |
| 1427 | PyUnicodeEncodeError_Create( |
| 1428 | const char *encoding, const Py_UNICODE *object, Py_ssize_t length, |
| 1429 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1430 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1431 | return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1432 | encoding, object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | /* |
| 1437 | * UnicodeDecodeError extends UnicodeError |
| 1438 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1439 | |
| 1440 | static int |
| 1441 | UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1442 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1443 | PyUnicodeErrorObject *ude; |
| 1444 | const char *data; |
| 1445 | Py_ssize_t size; |
| 1446 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1447 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1448 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1449 | |
| 1450 | ude = (PyUnicodeErrorObject *)self; |
| 1451 | |
| 1452 | Py_CLEAR(ude->encoding); |
| 1453 | Py_CLEAR(ude->object); |
| 1454 | Py_CLEAR(ude->reason); |
| 1455 | |
| 1456 | if (!PyArg_ParseTuple(args, "O!OnnO!", |
| 1457 | &PyUnicode_Type, &ude->encoding, |
| 1458 | &ude->object, |
| 1459 | &ude->start, |
| 1460 | &ude->end, |
| 1461 | &PyUnicode_Type, &ude->reason)) { |
| 1462 | ude->encoding = ude->object = ude->reason = NULL; |
| 1463 | return -1; |
| 1464 | } |
| 1465 | |
| 1466 | if (!PyString_Check(ude->object)) { |
| 1467 | if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) { |
| 1468 | ude->encoding = ude->object = ude->reason = NULL; |
| 1469 | return -1; |
| 1470 | } |
| 1471 | ude->object = PyString_FromStringAndSize(data, size); |
| 1472 | } |
| 1473 | else { |
| 1474 | Py_INCREF(ude->object); |
| 1475 | } |
| 1476 | |
| 1477 | Py_INCREF(ude->encoding); |
| 1478 | Py_INCREF(ude->reason); |
| 1479 | |
| 1480 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | static PyObject * |
| 1484 | UnicodeDecodeError_str(PyObject *self) |
| 1485 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1486 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1488 | if (uself->end==uself->start+1) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1489 | int byte = (int)(PyString_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1490 | return PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1491 | "'%U' codec can't decode byte 0x%02x in position %zd: %U", |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1492 | ((PyUnicodeErrorObject *)self)->encoding, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1493 | byte, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1494 | uself->start, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1495 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1496 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1497 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1498 | return PyUnicode_FromFormat( |
| 1499 | "'%U' codec can't decode bytes in position %zd-%zd: %U", |
| 1500 | ((PyUnicodeErrorObject *)self)->encoding, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1501 | uself->start, |
| 1502 | uself->end-1, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1503 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1504 | ); |
| 1505 | } |
| 1506 | |
| 1507 | static PyTypeObject _PyExc_UnicodeDecodeError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1508 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1509 | "UnicodeDecodeError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1510 | sizeof(PyUnicodeErrorObject), 0, |
| 1511 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1512 | (reprfunc)UnicodeDecodeError_str, 0, 0, 0, |
| 1513 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1514 | PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse, |
| 1515 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1516 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1517 | (initproc)UnicodeDecodeError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1518 | }; |
| 1519 | PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError; |
| 1520 | |
| 1521 | PyObject * |
| 1522 | PyUnicodeDecodeError_Create( |
| 1523 | const char *encoding, const char *object, Py_ssize_t length, |
| 1524 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1525 | { |
| 1526 | assert(length < INT_MAX); |
| 1527 | assert(start < INT_MAX); |
| 1528 | assert(end < INT_MAX); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1529 | return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1530 | encoding, object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | /* |
| 1535 | * UnicodeTranslateError extends UnicodeError |
| 1536 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1537 | |
| 1538 | static int |
| 1539 | UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args, |
| 1540 | PyObject *kwds) |
| 1541 | { |
| 1542 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1543 | return -1; |
| 1544 | |
| 1545 | Py_CLEAR(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1546 | Py_CLEAR(self->reason); |
| 1547 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1548 | if (!PyArg_ParseTuple(args, "O!nnO!", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1549 | &PyUnicode_Type, &self->object, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1550 | &self->start, |
| 1551 | &self->end, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1552 | &PyUnicode_Type, &self->reason)) { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1553 | self->object = self->reason = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1554 | return -1; |
| 1555 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1556 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1557 | Py_INCREF(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1558 | Py_INCREF(self->reason); |
| 1559 | |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | static PyObject * |
| 1565 | UnicodeTranslateError_str(PyObject *self) |
| 1566 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1567 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1569 | if (uself->end==uself->start+1) { |
| 1570 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1571 | const char *fmt; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1572 | if (badchar <= 0xff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1573 | fmt = "can't translate character '\\x%02x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1574 | else if (badchar <= 0xffff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1575 | fmt = "can't translate character '\\u%04x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1576 | else |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1577 | fmt = "can't translate character '\\U%08x' in position %zd: %U"; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1578 | return PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1579 | fmt, |
| 1580 | badchar, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1581 | uself->start, |
| 1582 | uself->reason |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1583 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1584 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1585 | return PyUnicode_FromFormat( |
| 1586 | "can't translate characters in position %zd-%zd: %U", |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1587 | uself->start, |
| 1588 | uself->end-1, |
| 1589 | uself->reason |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1590 | ); |
| 1591 | } |
| 1592 | |
| 1593 | static PyTypeObject _PyExc_UnicodeTranslateError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1594 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1595 | "UnicodeTranslateError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1596 | sizeof(PyUnicodeErrorObject), 0, |
| 1597 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1598 | (reprfunc)UnicodeTranslateError_str, 0, 0, 0, |
| 1599 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1600 | PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1601 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
| 1602 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1603 | (initproc)UnicodeTranslateError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1604 | }; |
| 1605 | PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError; |
| 1606 | |
| 1607 | PyObject * |
| 1608 | PyUnicodeTranslateError_Create( |
| 1609 | const Py_UNICODE *object, Py_ssize_t length, |
| 1610 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1611 | { |
| 1612 | return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1613 | object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1614 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1615 | |
| 1616 | |
| 1617 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1618 | * AssertionError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1619 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1620 | SimpleExtendsException(PyExc_Exception, AssertionError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1621 | "Assertion failed."); |
| 1622 | |
| 1623 | |
| 1624 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1625 | * ArithmeticError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1626 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1627 | SimpleExtendsException(PyExc_Exception, ArithmeticError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1628 | "Base class for arithmetic errors."); |
| 1629 | |
| 1630 | |
| 1631 | /* |
| 1632 | * FloatingPointError extends ArithmeticError |
| 1633 | */ |
| 1634 | SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError, |
| 1635 | "Floating point operation failed."); |
| 1636 | |
| 1637 | |
| 1638 | /* |
| 1639 | * OverflowError extends ArithmeticError |
| 1640 | */ |
| 1641 | SimpleExtendsException(PyExc_ArithmeticError, OverflowError, |
| 1642 | "Result too large to be represented."); |
| 1643 | |
| 1644 | |
| 1645 | /* |
| 1646 | * ZeroDivisionError extends ArithmeticError |
| 1647 | */ |
| 1648 | SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError, |
| 1649 | "Second argument to a division or modulo operation was zero."); |
| 1650 | |
| 1651 | |
| 1652 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1653 | * SystemError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1654 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1655 | SimpleExtendsException(PyExc_Exception, SystemError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1656 | "Internal error in the Python interpreter.\n" |
| 1657 | "\n" |
| 1658 | "Please report this to the Python maintainer, along with the traceback,\n" |
| 1659 | "the Python version, and the hardware/OS platform and version."); |
| 1660 | |
| 1661 | |
| 1662 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1663 | * ReferenceError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1664 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1665 | SimpleExtendsException(PyExc_Exception, ReferenceError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1666 | "Weak ref proxy used after referent went away."); |
| 1667 | |
| 1668 | |
| 1669 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1670 | * MemoryError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1671 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1672 | SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1673 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1674 | /* |
| 1675 | * BufferError extends Exception |
| 1676 | */ |
| 1677 | SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error."); |
| 1678 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1679 | |
| 1680 | /* Warning category docstrings */ |
| 1681 | |
| 1682 | /* |
| 1683 | * Warning extends Exception |
| 1684 | */ |
| 1685 | SimpleExtendsException(PyExc_Exception, Warning, |
| 1686 | "Base class for warning categories."); |
| 1687 | |
| 1688 | |
| 1689 | /* |
| 1690 | * UserWarning extends Warning |
| 1691 | */ |
| 1692 | SimpleExtendsException(PyExc_Warning, UserWarning, |
| 1693 | "Base class for warnings generated by user code."); |
| 1694 | |
| 1695 | |
| 1696 | /* |
| 1697 | * DeprecationWarning extends Warning |
| 1698 | */ |
| 1699 | SimpleExtendsException(PyExc_Warning, DeprecationWarning, |
| 1700 | "Base class for warnings about deprecated features."); |
| 1701 | |
| 1702 | |
| 1703 | /* |
| 1704 | * PendingDeprecationWarning extends Warning |
| 1705 | */ |
| 1706 | SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning, |
| 1707 | "Base class for warnings about features which will be deprecated\n" |
| 1708 | "in the future."); |
| 1709 | |
| 1710 | |
| 1711 | /* |
| 1712 | * SyntaxWarning extends Warning |
| 1713 | */ |
| 1714 | SimpleExtendsException(PyExc_Warning, SyntaxWarning, |
| 1715 | "Base class for warnings about dubious syntax."); |
| 1716 | |
| 1717 | |
| 1718 | /* |
| 1719 | * RuntimeWarning extends Warning |
| 1720 | */ |
| 1721 | SimpleExtendsException(PyExc_Warning, RuntimeWarning, |
| 1722 | "Base class for warnings about dubious runtime behavior."); |
| 1723 | |
| 1724 | |
| 1725 | /* |
| 1726 | * FutureWarning extends Warning |
| 1727 | */ |
| 1728 | SimpleExtendsException(PyExc_Warning, FutureWarning, |
| 1729 | "Base class for warnings about constructs that will change semantically\n" |
| 1730 | "in the future."); |
| 1731 | |
| 1732 | |
| 1733 | /* |
| 1734 | * ImportWarning extends Warning |
| 1735 | */ |
| 1736 | SimpleExtendsException(PyExc_Warning, ImportWarning, |
| 1737 | "Base class for warnings about probable mistakes in module imports"); |
| 1738 | |
| 1739 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1740 | /* |
| 1741 | * UnicodeWarning extends Warning |
| 1742 | */ |
| 1743 | SimpleExtendsException(PyExc_Warning, UnicodeWarning, |
| 1744 | "Base class for warnings about Unicode related problems, mostly\n" |
| 1745 | "related to conversion problems."); |
| 1746 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1747 | /* |
| 1748 | * BytesWarning extends Warning |
| 1749 | */ |
| 1750 | SimpleExtendsException(PyExc_Warning, BytesWarning, |
| 1751 | "Base class for warnings about bytes and buffer related problems, mostly\n" |
| 1752 | "related to conversion from str or comparing to str."); |
| 1753 | |
| 1754 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1755 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1756 | /* Pre-computed MemoryError instance. Best to create this as early as |
| 1757 | * possible and not wait until a MemoryError is actually raised! |
| 1758 | */ |
| 1759 | PyObject *PyExc_MemoryErrorInst=NULL; |
| 1760 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 1761 | /* Pre-computed RuntimeError instance for when recursion depth is reached. |
| 1762 | Meant to be used when normalizing the exception for exceeding the recursion |
| 1763 | depth will cause its own infinite recursion. |
| 1764 | */ |
| 1765 | PyObject *PyExc_RecursionErrorInst = NULL; |
| 1766 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1767 | #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \ |
| 1768 | Py_FatalError("exceptions bootstrapping error."); |
| 1769 | |
| 1770 | #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1771 | if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \ |
| 1772 | Py_FatalError("Module dictionary insertion problem."); |
| 1773 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1774 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 1775 | /* crt variable checking in VisualStudio .NET 2005 */ |
| 1776 | #include <crtdbg.h> |
| 1777 | |
| 1778 | static int prevCrtReportMode; |
| 1779 | static _invalid_parameter_handler prevCrtHandler; |
| 1780 | |
| 1781 | /* Invalid parameter handler. Sets a ValueError exception */ |
| 1782 | static void |
| 1783 | InvalidParameterHandler( |
| 1784 | const wchar_t * expression, |
| 1785 | const wchar_t * function, |
| 1786 | const wchar_t * file, |
| 1787 | unsigned int line, |
| 1788 | uintptr_t pReserved) |
| 1789 | { |
| 1790 | /* Do nothing, allow execution to continue. Usually this |
| 1791 | * means that the CRT will set errno to EINVAL |
| 1792 | */ |
| 1793 | } |
| 1794 | #endif |
| 1795 | |
| 1796 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1797 | PyMODINIT_FUNC |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1798 | _PyExc_Init(void) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1799 | { |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1800 | PyObject *bltinmod, *bdict; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1801 | |
| 1802 | PRE_INIT(BaseException) |
| 1803 | PRE_INIT(Exception) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1804 | PRE_INIT(TypeError) |
| 1805 | PRE_INIT(StopIteration) |
| 1806 | PRE_INIT(GeneratorExit) |
| 1807 | PRE_INIT(SystemExit) |
| 1808 | PRE_INIT(KeyboardInterrupt) |
| 1809 | PRE_INIT(ImportError) |
| 1810 | PRE_INIT(EnvironmentError) |
| 1811 | PRE_INIT(IOError) |
| 1812 | PRE_INIT(OSError) |
| 1813 | #ifdef MS_WINDOWS |
| 1814 | PRE_INIT(WindowsError) |
| 1815 | #endif |
| 1816 | #ifdef __VMS |
| 1817 | PRE_INIT(VMSError) |
| 1818 | #endif |
| 1819 | PRE_INIT(EOFError) |
| 1820 | PRE_INIT(RuntimeError) |
| 1821 | PRE_INIT(NotImplementedError) |
| 1822 | PRE_INIT(NameError) |
| 1823 | PRE_INIT(UnboundLocalError) |
| 1824 | PRE_INIT(AttributeError) |
| 1825 | PRE_INIT(SyntaxError) |
| 1826 | PRE_INIT(IndentationError) |
| 1827 | PRE_INIT(TabError) |
| 1828 | PRE_INIT(LookupError) |
| 1829 | PRE_INIT(IndexError) |
| 1830 | PRE_INIT(KeyError) |
| 1831 | PRE_INIT(ValueError) |
| 1832 | PRE_INIT(UnicodeError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1833 | PRE_INIT(UnicodeEncodeError) |
| 1834 | PRE_INIT(UnicodeDecodeError) |
| 1835 | PRE_INIT(UnicodeTranslateError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1836 | PRE_INIT(AssertionError) |
| 1837 | PRE_INIT(ArithmeticError) |
| 1838 | PRE_INIT(FloatingPointError) |
| 1839 | PRE_INIT(OverflowError) |
| 1840 | PRE_INIT(ZeroDivisionError) |
| 1841 | PRE_INIT(SystemError) |
| 1842 | PRE_INIT(ReferenceError) |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 1843 | PRE_INIT(BufferError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1844 | PRE_INIT(MemoryError) |
| 1845 | PRE_INIT(Warning) |
| 1846 | PRE_INIT(UserWarning) |
| 1847 | PRE_INIT(DeprecationWarning) |
| 1848 | PRE_INIT(PendingDeprecationWarning) |
| 1849 | PRE_INIT(SyntaxWarning) |
| 1850 | PRE_INIT(RuntimeWarning) |
| 1851 | PRE_INIT(FutureWarning) |
| 1852 | PRE_INIT(ImportWarning) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1853 | PRE_INIT(UnicodeWarning) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1854 | PRE_INIT(BytesWarning) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1855 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 1856 | bltinmod = PyImport_ImportModule("builtins"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1857 | if (bltinmod == NULL) |
| 1858 | Py_FatalError("exceptions bootstrapping error."); |
| 1859 | bdict = PyModule_GetDict(bltinmod); |
| 1860 | if (bdict == NULL) |
| 1861 | Py_FatalError("exceptions bootstrapping error."); |
| 1862 | |
| 1863 | POST_INIT(BaseException) |
| 1864 | POST_INIT(Exception) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1865 | POST_INIT(TypeError) |
| 1866 | POST_INIT(StopIteration) |
| 1867 | POST_INIT(GeneratorExit) |
| 1868 | POST_INIT(SystemExit) |
| 1869 | POST_INIT(KeyboardInterrupt) |
| 1870 | POST_INIT(ImportError) |
| 1871 | POST_INIT(EnvironmentError) |
| 1872 | POST_INIT(IOError) |
| 1873 | POST_INIT(OSError) |
| 1874 | #ifdef MS_WINDOWS |
| 1875 | POST_INIT(WindowsError) |
| 1876 | #endif |
| 1877 | #ifdef __VMS |
| 1878 | POST_INIT(VMSError) |
| 1879 | #endif |
| 1880 | POST_INIT(EOFError) |
| 1881 | POST_INIT(RuntimeError) |
| 1882 | POST_INIT(NotImplementedError) |
| 1883 | POST_INIT(NameError) |
| 1884 | POST_INIT(UnboundLocalError) |
| 1885 | POST_INIT(AttributeError) |
| 1886 | POST_INIT(SyntaxError) |
| 1887 | POST_INIT(IndentationError) |
| 1888 | POST_INIT(TabError) |
| 1889 | POST_INIT(LookupError) |
| 1890 | POST_INIT(IndexError) |
| 1891 | POST_INIT(KeyError) |
| 1892 | POST_INIT(ValueError) |
| 1893 | POST_INIT(UnicodeError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1894 | POST_INIT(UnicodeEncodeError) |
| 1895 | POST_INIT(UnicodeDecodeError) |
| 1896 | POST_INIT(UnicodeTranslateError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1897 | POST_INIT(AssertionError) |
| 1898 | POST_INIT(ArithmeticError) |
| 1899 | POST_INIT(FloatingPointError) |
| 1900 | POST_INIT(OverflowError) |
| 1901 | POST_INIT(ZeroDivisionError) |
| 1902 | POST_INIT(SystemError) |
| 1903 | POST_INIT(ReferenceError) |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 1904 | POST_INIT(BufferError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1905 | POST_INIT(MemoryError) |
| 1906 | POST_INIT(Warning) |
| 1907 | POST_INIT(UserWarning) |
| 1908 | POST_INIT(DeprecationWarning) |
| 1909 | POST_INIT(PendingDeprecationWarning) |
| 1910 | POST_INIT(SyntaxWarning) |
| 1911 | POST_INIT(RuntimeWarning) |
| 1912 | POST_INIT(FutureWarning) |
| 1913 | POST_INIT(ImportWarning) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1914 | POST_INIT(UnicodeWarning) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1915 | POST_INIT(BytesWarning) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1916 | |
| 1917 | PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL); |
| 1918 | if (!PyExc_MemoryErrorInst) |
| 1919 | Py_FatalError("Cannot pre-allocate MemoryError instance\n"); |
| 1920 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 1921 | PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL); |
| 1922 | if (!PyExc_RecursionErrorInst) |
| 1923 | Py_FatalError("Cannot pre-allocate RuntimeError instance for " |
| 1924 | "recursion errors"); |
| 1925 | else { |
| 1926 | PyBaseExceptionObject *err_inst = |
| 1927 | (PyBaseExceptionObject *)PyExc_RecursionErrorInst; |
| 1928 | PyObject *args_tuple; |
| 1929 | PyObject *exc_message; |
Neal Norwitz | bed6784 | 2007-10-27 04:00:45 +0000 | [diff] [blame] | 1930 | exc_message = PyUnicode_FromString("maximum recursion depth exceeded"); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 1931 | if (!exc_message) |
| 1932 | Py_FatalError("cannot allocate argument for RuntimeError " |
| 1933 | "pre-allocation"); |
| 1934 | args_tuple = PyTuple_Pack(1, exc_message); |
| 1935 | if (!args_tuple) |
| 1936 | Py_FatalError("cannot allocate tuple for RuntimeError " |
| 1937 | "pre-allocation"); |
| 1938 | Py_DECREF(exc_message); |
| 1939 | if (BaseException_init(err_inst, args_tuple, NULL)) |
| 1940 | Py_FatalError("init of pre-allocated RuntimeError failed"); |
| 1941 | Py_DECREF(args_tuple); |
| 1942 | } |
| 1943 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1944 | Py_DECREF(bltinmod); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1945 | |
| 1946 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 1947 | /* Set CRT argument error handler */ |
| 1948 | prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler); |
| 1949 | /* turn off assertions in debug mode */ |
| 1950 | prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0); |
| 1951 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | void |
| 1955 | _PyExc_Fini(void) |
| 1956 | { |
| 1957 | Py_XDECREF(PyExc_MemoryErrorInst); |
| 1958 | PyExc_MemoryErrorInst = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1959 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 1960 | /* reset CRT error handling */ |
| 1961 | _set_invalid_parameter_handler(prevCrtHandler); |
| 1962 | _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode); |
| 1963 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1964 | } |