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 | |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 836 | if (self->filename) { |
| 837 | if (PyString_Check(self->filename)) |
| 838 | filename = PyString_AsString(self->filename); |
| 839 | else if (PyUnicode_Check(self->filename)) |
| 840 | filename = PyUnicode_AsString(self->filename); |
| 841 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 842 | have_lineno = (self->lineno != NULL) && PyInt_CheckExact(self->lineno); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 843 | |
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 PyObject_Unicode(self->msg ? self->msg : Py_None); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 846 | |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 847 | if (filename && have_lineno) |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 848 | return PyUnicode_FromFormat("%S (%s, line %ld)", |
| 849 | self->msg ? self->msg : Py_None, |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 850 | my_basename(filename), |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 851 | PyInt_AsLong(self->lineno)); |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 852 | else if (filename) |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 853 | return PyUnicode_FromFormat("%S (%s)", |
| 854 | self->msg ? self->msg : Py_None, |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 855 | my_basename(filename)); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 856 | else /* only have_lineno */ |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 857 | return PyUnicode_FromFormat("%S (line %ld)", |
| 858 | self->msg ? self->msg : Py_None, |
| 859 | PyInt_AsLong(self->lineno)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static PyMemberDef SyntaxError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 863 | {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0, |
| 864 | PyDoc_STR("exception msg")}, |
| 865 | {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0, |
| 866 | PyDoc_STR("exception filename")}, |
| 867 | {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0, |
| 868 | PyDoc_STR("exception lineno")}, |
| 869 | {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0, |
| 870 | PyDoc_STR("exception offset")}, |
| 871 | {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0, |
| 872 | PyDoc_STR("exception text")}, |
| 873 | {"print_file_and_line", T_OBJECT, |
| 874 | offsetof(PySyntaxErrorObject, print_file_and_line), 0, |
| 875 | PyDoc_STR("exception print_file_and_line")}, |
| 876 | {NULL} /* Sentinel */ |
| 877 | }; |
| 878 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 879 | ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 880 | SyntaxError_dealloc, 0, SyntaxError_members, |
| 881 | SyntaxError_str, "Invalid syntax."); |
| 882 | |
| 883 | |
| 884 | /* |
| 885 | * IndentationError extends SyntaxError |
| 886 | */ |
| 887 | MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError, |
| 888 | "Improper indentation."); |
| 889 | |
| 890 | |
| 891 | /* |
| 892 | * TabError extends IndentationError |
| 893 | */ |
| 894 | MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError, |
| 895 | "Improper mixture of spaces and tabs."); |
| 896 | |
| 897 | |
| 898 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 899 | * LookupError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 900 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 901 | SimpleExtendsException(PyExc_Exception, LookupError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 902 | "Base class for lookup errors."); |
| 903 | |
| 904 | |
| 905 | /* |
| 906 | * IndexError extends LookupError |
| 907 | */ |
| 908 | SimpleExtendsException(PyExc_LookupError, IndexError, |
| 909 | "Sequence index out of range."); |
| 910 | |
| 911 | |
| 912 | /* |
| 913 | * KeyError extends LookupError |
| 914 | */ |
| 915 | static PyObject * |
| 916 | KeyError_str(PyBaseExceptionObject *self) |
| 917 | { |
| 918 | /* If args is a tuple of exactly one item, apply repr to args[0]. |
| 919 | This is done so that e.g. the exception raised by {}[''] prints |
| 920 | KeyError: '' |
| 921 | rather than the confusing |
| 922 | KeyError |
| 923 | alone. The downside is that if KeyError is raised with an explanatory |
| 924 | string, that string will be displayed in quotes. Too bad. |
| 925 | If args is anything else, use the default BaseException__str__(). |
| 926 | */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 927 | if (PyTuple_GET_SIZE(self->args) == 1) { |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 928 | return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 929 | } |
| 930 | return BaseException_str(self); |
| 931 | } |
| 932 | |
| 933 | ComplexExtendsException(PyExc_LookupError, KeyError, BaseException, |
| 934 | 0, 0, 0, KeyError_str, "Mapping key not found."); |
| 935 | |
| 936 | |
| 937 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 938 | * ValueError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 940 | SimpleExtendsException(PyExc_Exception, ValueError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 941 | "Inappropriate argument value (of correct type)."); |
| 942 | |
| 943 | /* |
| 944 | * UnicodeError extends ValueError |
| 945 | */ |
| 946 | |
| 947 | SimpleExtendsException(PyExc_ValueError, UnicodeError, |
| 948 | "Unicode related error."); |
| 949 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 950 | static PyObject * |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 951 | get_bytes(PyObject *attr, const char *name) |
| 952 | { |
| 953 | if (!attr) { |
| 954 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | if (!PyBytes_Check(attr)) { |
| 959 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name); |
| 960 | return NULL; |
| 961 | } |
| 962 | Py_INCREF(attr); |
| 963 | return attr; |
| 964 | } |
| 965 | |
| 966 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 967 | get_unicode(PyObject *attr, const char *name) |
| 968 | { |
| 969 | if (!attr) { |
| 970 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 971 | return NULL; |
| 972 | } |
| 973 | |
| 974 | if (!PyUnicode_Check(attr)) { |
| 975 | PyErr_Format(PyExc_TypeError, |
| 976 | "%.200s attribute must be unicode", name); |
| 977 | return NULL; |
| 978 | } |
| 979 | Py_INCREF(attr); |
| 980 | return attr; |
| 981 | } |
| 982 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 983 | static int |
| 984 | set_unicodefromstring(PyObject **attr, const char *value) |
| 985 | { |
| 986 | PyObject *obj = PyUnicode_FromString(value); |
| 987 | if (!obj) |
| 988 | return -1; |
| 989 | Py_CLEAR(*attr); |
| 990 | *attr = obj; |
| 991 | return 0; |
| 992 | } |
| 993 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 994 | PyObject * |
| 995 | PyUnicodeEncodeError_GetEncoding(PyObject *exc) |
| 996 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 997 | return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | PyObject * |
| 1001 | PyUnicodeDecodeError_GetEncoding(PyObject *exc) |
| 1002 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1003 | return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | PyObject * |
| 1007 | PyUnicodeEncodeError_GetObject(PyObject *exc) |
| 1008 | { |
| 1009 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1010 | } |
| 1011 | |
| 1012 | PyObject * |
| 1013 | PyUnicodeDecodeError_GetObject(PyObject *exc) |
| 1014 | { |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 1015 | return get_bytes(((PyUnicodeErrorObject *)exc)->object, "object"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | PyObject * |
| 1019 | PyUnicodeTranslateError_GetObject(PyObject *exc) |
| 1020 | { |
| 1021 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1022 | } |
| 1023 | |
| 1024 | int |
| 1025 | PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1026 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1027 | Py_ssize_t size; |
| 1028 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1029 | "object"); |
| 1030 | if (!obj) |
| 1031 | return -1; |
| 1032 | *start = ((PyUnicodeErrorObject *)exc)->start; |
| 1033 | size = PyUnicode_GET_SIZE(obj); |
| 1034 | if (*start<0) |
| 1035 | *start = 0; /*XXX check for values <0*/ |
| 1036 | if (*start>=size) |
| 1037 | *start = size-1; |
| 1038 | Py_DECREF(obj); |
| 1039 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | int |
| 1044 | PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1045 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1046 | Py_ssize_t size; |
| 1047 | PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1048 | if (!obj) |
| 1049 | return -1; |
| 1050 | size = PyBytes_GET_SIZE(obj); |
| 1051 | *start = ((PyUnicodeErrorObject *)exc)->start; |
| 1052 | if (*start<0) |
| 1053 | *start = 0; |
| 1054 | if (*start>=size) |
| 1055 | *start = size-1; |
| 1056 | Py_DECREF(obj); |
| 1057 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | int |
| 1062 | PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1063 | { |
| 1064 | return PyUnicodeEncodeError_GetStart(exc, start); |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | int |
| 1069 | PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1070 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1071 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1072 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | int |
| 1077 | PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1078 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1079 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1080 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | int |
| 1085 | PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1086 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1087 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1088 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | int |
| 1093 | PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1094 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1095 | Py_ssize_t size; |
| 1096 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1097 | "object"); |
| 1098 | if (!obj) |
| 1099 | return -1; |
| 1100 | *end = ((PyUnicodeErrorObject *)exc)->end; |
| 1101 | size = PyUnicode_GET_SIZE(obj); |
| 1102 | if (*end<1) |
| 1103 | *end = 1; |
| 1104 | if (*end>size) |
| 1105 | *end = size; |
| 1106 | Py_DECREF(obj); |
| 1107 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | int |
| 1112 | PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1113 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1114 | Py_ssize_t size; |
| 1115 | PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1116 | if (!obj) |
| 1117 | return -1; |
| 1118 | size = PyBytes_GET_SIZE(obj); |
| 1119 | *end = ((PyUnicodeErrorObject *)exc)->end; |
| 1120 | if (*end<1) |
| 1121 | *end = 1; |
| 1122 | if (*end>size) |
| 1123 | *end = size; |
| 1124 | Py_DECREF(obj); |
| 1125 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | int |
| 1130 | PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start) |
| 1131 | { |
| 1132 | return PyUnicodeEncodeError_GetEnd(exc, start); |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | int |
| 1137 | PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1138 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1139 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1140 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | |
| 1144 | int |
| 1145 | PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1146 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1147 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1148 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | int |
| 1153 | PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1154 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1155 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1156 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | PyObject * |
| 1160 | PyUnicodeEncodeError_GetReason(PyObject *exc) |
| 1161 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1162 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | PyObject * |
| 1167 | PyUnicodeDecodeError_GetReason(PyObject *exc) |
| 1168 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1169 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | PyObject * |
| 1174 | PyUnicodeTranslateError_GetReason(PyObject *exc) |
| 1175 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1176 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | int |
| 1181 | PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason) |
| 1182 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1183 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1184 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | int |
| 1189 | PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason) |
| 1190 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1191 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1192 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | int |
| 1197 | PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason) |
| 1198 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1199 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1200 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1204 | static int |
| 1205 | UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds, |
| 1206 | PyTypeObject *objecttype) |
| 1207 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1208 | Py_CLEAR(self->encoding); |
| 1209 | Py_CLEAR(self->object); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1210 | Py_CLEAR(self->reason); |
| 1211 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1212 | if (!PyArg_ParseTuple(args, "O!O!nnO!", |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1213 | &PyUnicode_Type, &self->encoding, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1214 | objecttype, &self->object, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1215 | &self->start, |
| 1216 | &self->end, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1217 | &PyUnicode_Type, &self->reason)) { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1218 | self->encoding = self->object = self->reason = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1219 | return -1; |
| 1220 | } |
| 1221 | |
| 1222 | Py_INCREF(self->encoding); |
| 1223 | Py_INCREF(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1224 | Py_INCREF(self->reason); |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1229 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1230 | UnicodeError_clear(PyUnicodeErrorObject *self) |
| 1231 | { |
| 1232 | Py_CLEAR(self->encoding); |
| 1233 | Py_CLEAR(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1234 | Py_CLEAR(self->reason); |
| 1235 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 1236 | } |
| 1237 | |
| 1238 | static void |
| 1239 | UnicodeError_dealloc(PyUnicodeErrorObject *self) |
| 1240 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1241 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1242 | UnicodeError_clear(self); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1243 | Py_Type(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1246 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1247 | UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg) |
| 1248 | { |
| 1249 | Py_VISIT(self->encoding); |
| 1250 | Py_VISIT(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1251 | Py_VISIT(self->reason); |
| 1252 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 1253 | } |
| 1254 | |
| 1255 | static PyMemberDef UnicodeError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1256 | {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0, |
| 1257 | PyDoc_STR("exception encoding")}, |
| 1258 | {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0, |
| 1259 | PyDoc_STR("exception object")}, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1260 | {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1261 | PyDoc_STR("exception start")}, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1262 | {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1263 | PyDoc_STR("exception end")}, |
| 1264 | {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0, |
| 1265 | PyDoc_STR("exception reason")}, |
| 1266 | {NULL} /* Sentinel */ |
| 1267 | }; |
| 1268 | |
| 1269 | |
| 1270 | /* |
| 1271 | * UnicodeEncodeError extends UnicodeError |
| 1272 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1273 | |
| 1274 | static int |
| 1275 | UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1276 | { |
| 1277 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1278 | return -1; |
| 1279 | return UnicodeError_init((PyUnicodeErrorObject *)self, args, |
| 1280 | kwds, &PyUnicode_Type); |
| 1281 | } |
| 1282 | |
| 1283 | static PyObject * |
| 1284 | UnicodeEncodeError_str(PyObject *self) |
| 1285 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1286 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1287 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1288 | if (uself->end==uself->start+1) { |
| 1289 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1290 | const char *fmt; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1291 | if (badchar <= 0xff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1292 | fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1293 | else if (badchar <= 0xffff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1294 | fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1295 | else |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1296 | fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U"; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1297 | return PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1298 | fmt, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1299 | ((PyUnicodeErrorObject *)self)->encoding, |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1300 | badchar, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1301 | uself->start, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1302 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1303 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1304 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1305 | return PyUnicode_FromFormat( |
| 1306 | "'%U' codec can't encode characters in position %zd-%zd: %U", |
| 1307 | ((PyUnicodeErrorObject *)self)->encoding, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1308 | uself->start, |
| 1309 | uself->end-1, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1310 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1311 | ); |
| 1312 | } |
| 1313 | |
| 1314 | static PyTypeObject _PyExc_UnicodeEncodeError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1315 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1316 | "UnicodeEncodeError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1317 | sizeof(PyUnicodeErrorObject), 0, |
| 1318 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1319 | (reprfunc)UnicodeEncodeError_str, 0, 0, 0, |
| 1320 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1321 | PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse, |
| 1322 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1323 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1324 | (initproc)UnicodeEncodeError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1325 | }; |
| 1326 | PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError; |
| 1327 | |
| 1328 | PyObject * |
| 1329 | PyUnicodeEncodeError_Create( |
| 1330 | const char *encoding, const Py_UNICODE *object, Py_ssize_t length, |
| 1331 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1332 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1333 | return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1334 | encoding, object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | /* |
| 1339 | * UnicodeDecodeError extends UnicodeError |
| 1340 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1341 | |
| 1342 | static int |
| 1343 | UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1344 | { |
| 1345 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1346 | return -1; |
| 1347 | return UnicodeError_init((PyUnicodeErrorObject *)self, args, |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 1348 | kwds, &PyBytes_Type); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | static PyObject * |
| 1352 | UnicodeDecodeError_str(PyObject *self) |
| 1353 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1354 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1355 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1356 | if (uself->end==uself->start+1) { |
| 1357 | 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] | 1358 | return PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1359 | "'%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] | 1360 | ((PyUnicodeErrorObject *)self)->encoding, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1361 | byte, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1362 | uself->start, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1363 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1364 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1365 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1366 | return PyUnicode_FromFormat( |
| 1367 | "'%U' codec can't decode bytes in position %zd-%zd: %U", |
| 1368 | ((PyUnicodeErrorObject *)self)->encoding, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1369 | uself->start, |
| 1370 | uself->end-1, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1371 | ((PyUnicodeErrorObject *)self)->reason |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | ); |
| 1373 | } |
| 1374 | |
| 1375 | static PyTypeObject _PyExc_UnicodeDecodeError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1376 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1377 | "UnicodeDecodeError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1378 | sizeof(PyUnicodeErrorObject), 0, |
| 1379 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1380 | (reprfunc)UnicodeDecodeError_str, 0, 0, 0, |
| 1381 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1382 | PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse, |
| 1383 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1384 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1385 | (initproc)UnicodeDecodeError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1386 | }; |
| 1387 | PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError; |
| 1388 | |
| 1389 | PyObject * |
| 1390 | PyUnicodeDecodeError_Create( |
| 1391 | const char *encoding, const char *object, Py_ssize_t length, |
| 1392 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1393 | { |
| 1394 | assert(length < INT_MAX); |
| 1395 | assert(start < INT_MAX); |
| 1396 | assert(end < INT_MAX); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1397 | return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1398 | encoding, object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | /* |
| 1403 | * UnicodeTranslateError extends UnicodeError |
| 1404 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1405 | |
| 1406 | static int |
| 1407 | UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args, |
| 1408 | PyObject *kwds) |
| 1409 | { |
| 1410 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1411 | return -1; |
| 1412 | |
| 1413 | Py_CLEAR(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1414 | Py_CLEAR(self->reason); |
| 1415 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1416 | if (!PyArg_ParseTuple(args, "O!nnO!", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1417 | &PyUnicode_Type, &self->object, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1418 | &self->start, |
| 1419 | &self->end, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1420 | &PyUnicode_Type, &self->reason)) { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1421 | self->object = self->reason = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1422 | return -1; |
| 1423 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1424 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1425 | Py_INCREF(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1426 | Py_INCREF(self->reason); |
| 1427 | |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | |
| 1432 | static PyObject * |
| 1433 | UnicodeTranslateError_str(PyObject *self) |
| 1434 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1435 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1436 | |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1437 | if (uself->end==uself->start+1) { |
| 1438 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1439 | const char *fmt; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1440 | if (badchar <= 0xff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1441 | fmt = "can't translate character '\\x%02x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1442 | else if (badchar <= 0xffff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1443 | fmt = "can't translate character '\\u%04x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1444 | else |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1445 | fmt = "can't translate character '\\U%08x' in position %zd: %U"; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1446 | return PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1447 | fmt, |
| 1448 | badchar, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1449 | uself->start, |
| 1450 | uself->reason |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1451 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1452 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1453 | return PyUnicode_FromFormat( |
| 1454 | "can't translate characters in position %zd-%zd: %U", |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1455 | uself->start, |
| 1456 | uself->end-1, |
| 1457 | uself->reason |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1458 | ); |
| 1459 | } |
| 1460 | |
| 1461 | static PyTypeObject _PyExc_UnicodeTranslateError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1462 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1463 | "UnicodeTranslateError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1464 | sizeof(PyUnicodeErrorObject), 0, |
| 1465 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1466 | (reprfunc)UnicodeTranslateError_str, 0, 0, 0, |
| 1467 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1468 | PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1469 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
| 1470 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1471 | (initproc)UnicodeTranslateError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1472 | }; |
| 1473 | PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError; |
| 1474 | |
| 1475 | PyObject * |
| 1476 | PyUnicodeTranslateError_Create( |
| 1477 | const Py_UNICODE *object, Py_ssize_t length, |
| 1478 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1479 | { |
| 1480 | return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1481 | object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1482 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1483 | |
| 1484 | |
| 1485 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1486 | * AssertionError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1487 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1488 | SimpleExtendsException(PyExc_Exception, AssertionError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1489 | "Assertion failed."); |
| 1490 | |
| 1491 | |
| 1492 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1493 | * ArithmeticError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1494 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1495 | SimpleExtendsException(PyExc_Exception, ArithmeticError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1496 | "Base class for arithmetic errors."); |
| 1497 | |
| 1498 | |
| 1499 | /* |
| 1500 | * FloatingPointError extends ArithmeticError |
| 1501 | */ |
| 1502 | SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError, |
| 1503 | "Floating point operation failed."); |
| 1504 | |
| 1505 | |
| 1506 | /* |
| 1507 | * OverflowError extends ArithmeticError |
| 1508 | */ |
| 1509 | SimpleExtendsException(PyExc_ArithmeticError, OverflowError, |
| 1510 | "Result too large to be represented."); |
| 1511 | |
| 1512 | |
| 1513 | /* |
| 1514 | * ZeroDivisionError extends ArithmeticError |
| 1515 | */ |
| 1516 | SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError, |
| 1517 | "Second argument to a division or modulo operation was zero."); |
| 1518 | |
| 1519 | |
| 1520 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1521 | * SystemError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1522 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1523 | SimpleExtendsException(PyExc_Exception, SystemError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1524 | "Internal error in the Python interpreter.\n" |
| 1525 | "\n" |
| 1526 | "Please report this to the Python maintainer, along with the traceback,\n" |
| 1527 | "the Python version, and the hardware/OS platform and version."); |
| 1528 | |
| 1529 | |
| 1530 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1531 | * ReferenceError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1532 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1533 | SimpleExtendsException(PyExc_Exception, ReferenceError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1534 | "Weak ref proxy used after referent went away."); |
| 1535 | |
| 1536 | |
| 1537 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1538 | * MemoryError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1539 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1540 | SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1541 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1542 | /* |
| 1543 | * BufferError extends Exception |
| 1544 | */ |
| 1545 | SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error."); |
| 1546 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1547 | |
| 1548 | /* Warning category docstrings */ |
| 1549 | |
| 1550 | /* |
| 1551 | * Warning extends Exception |
| 1552 | */ |
| 1553 | SimpleExtendsException(PyExc_Exception, Warning, |
| 1554 | "Base class for warning categories."); |
| 1555 | |
| 1556 | |
| 1557 | /* |
| 1558 | * UserWarning extends Warning |
| 1559 | */ |
| 1560 | SimpleExtendsException(PyExc_Warning, UserWarning, |
| 1561 | "Base class for warnings generated by user code."); |
| 1562 | |
| 1563 | |
| 1564 | /* |
| 1565 | * DeprecationWarning extends Warning |
| 1566 | */ |
| 1567 | SimpleExtendsException(PyExc_Warning, DeprecationWarning, |
| 1568 | "Base class for warnings about deprecated features."); |
| 1569 | |
| 1570 | |
| 1571 | /* |
| 1572 | * PendingDeprecationWarning extends Warning |
| 1573 | */ |
| 1574 | SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning, |
| 1575 | "Base class for warnings about features which will be deprecated\n" |
| 1576 | "in the future."); |
| 1577 | |
| 1578 | |
| 1579 | /* |
| 1580 | * SyntaxWarning extends Warning |
| 1581 | */ |
| 1582 | SimpleExtendsException(PyExc_Warning, SyntaxWarning, |
| 1583 | "Base class for warnings about dubious syntax."); |
| 1584 | |
| 1585 | |
| 1586 | /* |
| 1587 | * RuntimeWarning extends Warning |
| 1588 | */ |
| 1589 | SimpleExtendsException(PyExc_Warning, RuntimeWarning, |
| 1590 | "Base class for warnings about dubious runtime behavior."); |
| 1591 | |
| 1592 | |
| 1593 | /* |
| 1594 | * FutureWarning extends Warning |
| 1595 | */ |
| 1596 | SimpleExtendsException(PyExc_Warning, FutureWarning, |
| 1597 | "Base class for warnings about constructs that will change semantically\n" |
| 1598 | "in the future."); |
| 1599 | |
| 1600 | |
| 1601 | /* |
| 1602 | * ImportWarning extends Warning |
| 1603 | */ |
| 1604 | SimpleExtendsException(PyExc_Warning, ImportWarning, |
| 1605 | "Base class for warnings about probable mistakes in module imports"); |
| 1606 | |
| 1607 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1608 | /* |
| 1609 | * UnicodeWarning extends Warning |
| 1610 | */ |
| 1611 | SimpleExtendsException(PyExc_Warning, UnicodeWarning, |
| 1612 | "Base class for warnings about Unicode related problems, mostly\n" |
| 1613 | "related to conversion problems."); |
| 1614 | |
| 1615 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1616 | /* Pre-computed MemoryError instance. Best to create this as early as |
| 1617 | * possible and not wait until a MemoryError is actually raised! |
| 1618 | */ |
| 1619 | PyObject *PyExc_MemoryErrorInst=NULL; |
| 1620 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1621 | #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \ |
| 1622 | Py_FatalError("exceptions bootstrapping error."); |
| 1623 | |
| 1624 | #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1625 | if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \ |
| 1626 | Py_FatalError("Module dictionary insertion problem."); |
| 1627 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1628 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 1629 | /* crt variable checking in VisualStudio .NET 2005 */ |
| 1630 | #include <crtdbg.h> |
| 1631 | |
| 1632 | static int prevCrtReportMode; |
| 1633 | static _invalid_parameter_handler prevCrtHandler; |
| 1634 | |
| 1635 | /* Invalid parameter handler. Sets a ValueError exception */ |
| 1636 | static void |
| 1637 | InvalidParameterHandler( |
| 1638 | const wchar_t * expression, |
| 1639 | const wchar_t * function, |
| 1640 | const wchar_t * file, |
| 1641 | unsigned int line, |
| 1642 | uintptr_t pReserved) |
| 1643 | { |
| 1644 | /* Do nothing, allow execution to continue. Usually this |
| 1645 | * means that the CRT will set errno to EINVAL |
| 1646 | */ |
| 1647 | } |
| 1648 | #endif |
| 1649 | |
| 1650 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1651 | PyMODINIT_FUNC |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1652 | _PyExc_Init(void) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1653 | { |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1654 | PyObject *bltinmod, *bdict; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1655 | |
| 1656 | PRE_INIT(BaseException) |
| 1657 | PRE_INIT(Exception) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1658 | PRE_INIT(TypeError) |
| 1659 | PRE_INIT(StopIteration) |
| 1660 | PRE_INIT(GeneratorExit) |
| 1661 | PRE_INIT(SystemExit) |
| 1662 | PRE_INIT(KeyboardInterrupt) |
| 1663 | PRE_INIT(ImportError) |
| 1664 | PRE_INIT(EnvironmentError) |
| 1665 | PRE_INIT(IOError) |
| 1666 | PRE_INIT(OSError) |
| 1667 | #ifdef MS_WINDOWS |
| 1668 | PRE_INIT(WindowsError) |
| 1669 | #endif |
| 1670 | #ifdef __VMS |
| 1671 | PRE_INIT(VMSError) |
| 1672 | #endif |
| 1673 | PRE_INIT(EOFError) |
| 1674 | PRE_INIT(RuntimeError) |
| 1675 | PRE_INIT(NotImplementedError) |
| 1676 | PRE_INIT(NameError) |
| 1677 | PRE_INIT(UnboundLocalError) |
| 1678 | PRE_INIT(AttributeError) |
| 1679 | PRE_INIT(SyntaxError) |
| 1680 | PRE_INIT(IndentationError) |
| 1681 | PRE_INIT(TabError) |
| 1682 | PRE_INIT(LookupError) |
| 1683 | PRE_INIT(IndexError) |
| 1684 | PRE_INIT(KeyError) |
| 1685 | PRE_INIT(ValueError) |
| 1686 | PRE_INIT(UnicodeError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1687 | PRE_INIT(UnicodeEncodeError) |
| 1688 | PRE_INIT(UnicodeDecodeError) |
| 1689 | PRE_INIT(UnicodeTranslateError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1690 | PRE_INIT(AssertionError) |
| 1691 | PRE_INIT(ArithmeticError) |
| 1692 | PRE_INIT(FloatingPointError) |
| 1693 | PRE_INIT(OverflowError) |
| 1694 | PRE_INIT(ZeroDivisionError) |
| 1695 | PRE_INIT(SystemError) |
| 1696 | PRE_INIT(ReferenceError) |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 1697 | PRE_INIT(BufferError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1698 | PRE_INIT(MemoryError) |
| 1699 | PRE_INIT(Warning) |
| 1700 | PRE_INIT(UserWarning) |
| 1701 | PRE_INIT(DeprecationWarning) |
| 1702 | PRE_INIT(PendingDeprecationWarning) |
| 1703 | PRE_INIT(SyntaxWarning) |
| 1704 | PRE_INIT(RuntimeWarning) |
| 1705 | PRE_INIT(FutureWarning) |
| 1706 | PRE_INIT(ImportWarning) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1707 | PRE_INIT(UnicodeWarning) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1708 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1709 | bltinmod = PyImport_ImportModule("__builtin__"); |
| 1710 | if (bltinmod == NULL) |
| 1711 | Py_FatalError("exceptions bootstrapping error."); |
| 1712 | bdict = PyModule_GetDict(bltinmod); |
| 1713 | if (bdict == NULL) |
| 1714 | Py_FatalError("exceptions bootstrapping error."); |
| 1715 | |
| 1716 | POST_INIT(BaseException) |
| 1717 | POST_INIT(Exception) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1718 | POST_INIT(TypeError) |
| 1719 | POST_INIT(StopIteration) |
| 1720 | POST_INIT(GeneratorExit) |
| 1721 | POST_INIT(SystemExit) |
| 1722 | POST_INIT(KeyboardInterrupt) |
| 1723 | POST_INIT(ImportError) |
| 1724 | POST_INIT(EnvironmentError) |
| 1725 | POST_INIT(IOError) |
| 1726 | POST_INIT(OSError) |
| 1727 | #ifdef MS_WINDOWS |
| 1728 | POST_INIT(WindowsError) |
| 1729 | #endif |
| 1730 | #ifdef __VMS |
| 1731 | POST_INIT(VMSError) |
| 1732 | #endif |
| 1733 | POST_INIT(EOFError) |
| 1734 | POST_INIT(RuntimeError) |
| 1735 | POST_INIT(NotImplementedError) |
| 1736 | POST_INIT(NameError) |
| 1737 | POST_INIT(UnboundLocalError) |
| 1738 | POST_INIT(AttributeError) |
| 1739 | POST_INIT(SyntaxError) |
| 1740 | POST_INIT(IndentationError) |
| 1741 | POST_INIT(TabError) |
| 1742 | POST_INIT(LookupError) |
| 1743 | POST_INIT(IndexError) |
| 1744 | POST_INIT(KeyError) |
| 1745 | POST_INIT(ValueError) |
| 1746 | POST_INIT(UnicodeError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1747 | POST_INIT(UnicodeEncodeError) |
| 1748 | POST_INIT(UnicodeDecodeError) |
| 1749 | POST_INIT(UnicodeTranslateError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1750 | POST_INIT(AssertionError) |
| 1751 | POST_INIT(ArithmeticError) |
| 1752 | POST_INIT(FloatingPointError) |
| 1753 | POST_INIT(OverflowError) |
| 1754 | POST_INIT(ZeroDivisionError) |
| 1755 | POST_INIT(SystemError) |
| 1756 | POST_INIT(ReferenceError) |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 1757 | POST_INIT(BufferError) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1758 | POST_INIT(MemoryError) |
| 1759 | POST_INIT(Warning) |
| 1760 | POST_INIT(UserWarning) |
| 1761 | POST_INIT(DeprecationWarning) |
| 1762 | POST_INIT(PendingDeprecationWarning) |
| 1763 | POST_INIT(SyntaxWarning) |
| 1764 | POST_INIT(RuntimeWarning) |
| 1765 | POST_INIT(FutureWarning) |
| 1766 | POST_INIT(ImportWarning) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1767 | POST_INIT(UnicodeWarning) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1768 | |
| 1769 | PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL); |
| 1770 | if (!PyExc_MemoryErrorInst) |
| 1771 | Py_FatalError("Cannot pre-allocate MemoryError instance\n"); |
| 1772 | |
| 1773 | Py_DECREF(bltinmod); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1774 | |
| 1775 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 1776 | /* Set CRT argument error handler */ |
| 1777 | prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler); |
| 1778 | /* turn off assertions in debug mode */ |
| 1779 | prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0); |
| 1780 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | void |
| 1784 | _PyExc_Fini(void) |
| 1785 | { |
| 1786 | Py_XDECREF(PyExc_MemoryErrorInst); |
| 1787 | PyExc_MemoryErrorInst = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1788 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 1789 | /* reset CRT error handling */ |
| 1790 | _set_invalid_parameter_handler(prevCrtHandler); |
| 1791 | _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode); |
| 1792 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1793 | } |