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