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