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