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