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