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