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