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