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