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