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 | |
| 12 | #define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None); |
| 13 | #define EXC_MODULE_NAME "exceptions." |
| 14 | |
Richard Jones | c5b2a2e | 2006-05-27 16:07:28 +0000 | [diff] [blame] | 15 | /* NOTE: If the exception class hierarchy changes, don't forget to update |
| 16 | * Lib/test/exception_hierarchy.txt |
| 17 | */ |
| 18 | |
| 19 | PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\ |
| 20 | \n\ |
| 21 | Exceptions found here are defined both in the exceptions module and the\n\ |
| 22 | built-in namespace. It is recommended that user-defined exceptions\n\ |
| 23 | inherit from Exception. See the documentation for the exception\n\ |
| 24 | inheritance hierarchy.\n\ |
| 25 | "); |
| 26 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 27 | /* |
| 28 | * BaseException |
| 29 | */ |
| 30 | static PyObject * |
| 31 | BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 32 | { |
| 33 | PyBaseExceptionObject *self; |
| 34 | |
| 35 | self = (PyBaseExceptionObject *)type->tp_alloc(type, 0); |
Georg Brandl | c02e131 | 2007-04-11 17:16:24 +0000 | [diff] [blame] | 36 | if (!self) |
| 37 | return NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 38 | /* the dict is created on the fly in PyObject_GenericSetAttr */ |
| 39 | self->message = self->dict = NULL; |
| 40 | |
Georg Brandl | d7e9f60 | 2007-08-21 06:03:43 +0000 | [diff] [blame] | 41 | self->args = PyTuple_New(0); |
| 42 | if (!self->args) { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 43 | Py_DECREF(self); |
| 44 | return NULL; |
| 45 | } |
Georg Brandl | d7e9f60 | 2007-08-21 06:03:43 +0000 | [diff] [blame] | 46 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 47 | self->message = PyString_FromString(""); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 48 | if (!self->message) { |
| 49 | Py_DECREF(self); |
| 50 | return NULL; |
| 51 | } |
Georg Brandl | d7e9f60 | 2007-08-21 06:03:43 +0000 | [diff] [blame] | 52 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 53 | return (PyObject *)self; |
| 54 | } |
| 55 | |
| 56 | static int |
| 57 | BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) |
| 58 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 59 | if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) |
Georg Brandl | b0432bc | 2006-05-30 08:17:00 +0000 | [diff] [blame] | 60 | return -1; |
| 61 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 62 | Py_DECREF(self->args); |
| 63 | self->args = args; |
| 64 | Py_INCREF(self->args); |
| 65 | |
| 66 | if (PyTuple_GET_SIZE(self->args) == 1) { |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 67 | Py_CLEAR(self->message); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 68 | self->message = PyTuple_GET_ITEM(self->args, 0); |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 69 | Py_INCREF(self->message); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 74 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 75 | BaseException_clear(PyBaseExceptionObject *self) |
| 76 | { |
| 77 | Py_CLEAR(self->dict); |
| 78 | Py_CLEAR(self->args); |
| 79 | Py_CLEAR(self->message); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | BaseException_dealloc(PyBaseExceptionObject *self) |
| 85 | { |
Georg Brandl | 38f6237 | 2006-09-06 06:50:05 +0000 | [diff] [blame] | 86 | _PyObject_GC_UNTRACK(self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 87 | BaseException_clear(self); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 88 | Py_TYPE(self)->tp_free((PyObject *)self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 91 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 92 | BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg) |
| 93 | { |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 94 | Py_VISIT(self->dict); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 95 | Py_VISIT(self->args); |
| 96 | Py_VISIT(self->message); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static PyObject * |
| 101 | BaseException_str(PyBaseExceptionObject *self) |
| 102 | { |
| 103 | PyObject *out; |
| 104 | |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 105 | switch (PyTuple_GET_SIZE(self->args)) { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 106 | case 0: |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 107 | out = PyString_FromString(""); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 108 | break; |
| 109 | case 1: |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 110 | out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0)); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 111 | break; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 112 | default: |
| 113 | out = PyObject_Str(self->args); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | return out; |
| 118 | } |
| 119 | |
Nick Coghlan | 524b777 | 2008-07-08 14:08:04 +0000 | [diff] [blame] | 120 | #ifdef Py_USING_UNICODE |
| 121 | static PyObject * |
| 122 | BaseException_unicode(PyBaseExceptionObject *self) |
| 123 | { |
| 124 | PyObject *out; |
| 125 | |
Ezio Melotti | e748238 | 2009-12-24 22:32:25 +0000 | [diff] [blame] | 126 | /* issue6108: if __str__ has been overridden in the subclass, unicode() |
| 127 | should return the message returned by __str__ as used to happen |
| 128 | before this method was implemented. */ |
| 129 | if (Py_TYPE(self)->tp_str != (reprfunc)BaseException_str) { |
| 130 | PyObject *str; |
| 131 | /* Unlike PyObject_Str, tp_str can return unicode (i.e. return the |
| 132 | equivalent of unicode(e.__str__()) instead of unicode(str(e))). */ |
| 133 | str = Py_TYPE(self)->tp_str((PyObject*)self); |
| 134 | if (str == NULL) |
| 135 | return NULL; |
| 136 | out = PyObject_Unicode(str); |
| 137 | Py_DECREF(str); |
| 138 | return out; |
| 139 | } |
| 140 | |
Nick Coghlan | 524b777 | 2008-07-08 14:08:04 +0000 | [diff] [blame] | 141 | switch (PyTuple_GET_SIZE(self->args)) { |
| 142 | case 0: |
| 143 | out = PyUnicode_FromString(""); |
| 144 | break; |
| 145 | case 1: |
| 146 | out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0)); |
| 147 | break; |
| 148 | default: |
| 149 | out = PyObject_Unicode(self->args); |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | return out; |
| 154 | } |
| 155 | #endif |
| 156 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 157 | static PyObject * |
| 158 | BaseException_repr(PyBaseExceptionObject *self) |
| 159 | { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 160 | PyObject *repr_suffix; |
| 161 | PyObject *repr; |
| 162 | char *name; |
| 163 | char *dot; |
| 164 | |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 165 | repr_suffix = PyObject_Repr(self->args); |
| 166 | if (!repr_suffix) |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 167 | return NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 168 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 169 | name = (char *)Py_TYPE(self)->tp_name; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 170 | dot = strrchr(name, '.'); |
| 171 | if (dot != NULL) name = dot+1; |
| 172 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 173 | repr = PyString_FromString(name); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 174 | if (!repr) { |
| 175 | Py_DECREF(repr_suffix); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 179 | PyString_ConcatAndDel(&repr, repr_suffix); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 180 | return repr; |
| 181 | } |
| 182 | |
| 183 | /* Pickling support */ |
| 184 | static PyObject * |
| 185 | BaseException_reduce(PyBaseExceptionObject *self) |
| 186 | { |
Georg Brandl | ddba473 | 2006-05-30 07:04:55 +0000 | [diff] [blame] | 187 | if (self->args && self->dict) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 188 | return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict); |
Georg Brandl | 05f97bf | 2006-05-30 07:13:29 +0000 | [diff] [blame] | 189 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 190 | return PyTuple_Pack(2, Py_TYPE(self), self->args); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Georg Brandl | 85ac850 | 2006-06-01 06:39:19 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Needed for backward compatibility, since exceptions used to store |
| 195 | * all their attributes in the __dict__. Code is taken from cPickle's |
| 196 | * load_build function. |
| 197 | */ |
| 198 | static PyObject * |
| 199 | BaseException_setstate(PyObject *self, PyObject *state) |
| 200 | { |
| 201 | PyObject *d_key, *d_value; |
| 202 | Py_ssize_t i = 0; |
| 203 | |
| 204 | if (state != Py_None) { |
| 205 | if (!PyDict_Check(state)) { |
| 206 | PyErr_SetString(PyExc_TypeError, "state is not a dictionary"); |
| 207 | return NULL; |
| 208 | } |
| 209 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 210 | if (PyObject_SetAttr(self, d_key, d_value) < 0) |
| 211 | return NULL; |
| 212 | } |
| 213 | } |
| 214 | Py_RETURN_NONE; |
| 215 | } |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 216 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 217 | |
| 218 | static PyMethodDef BaseException_methods[] = { |
| 219 | {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS }, |
Georg Brandl | 85ac850 | 2006-06-01 06:39:19 +0000 | [diff] [blame] | 220 | {"__setstate__", (PyCFunction)BaseException_setstate, METH_O }, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 221 | #ifdef Py_USING_UNICODE |
Nick Coghlan | 524b777 | 2008-07-08 14:08:04 +0000 | [diff] [blame] | 222 | {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS }, |
| 223 | #endif |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 224 | {NULL, NULL, 0, NULL}, |
| 225 | }; |
| 226 | |
| 227 | |
| 228 | |
| 229 | static PyObject * |
| 230 | BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index) |
| 231 | { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 232 | if (PyErr_WarnPy3k("__getitem__ not supported for exception " |
| 233 | "classes in 3.x; use args attribute", 1) < 0) |
| 234 | return NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 235 | return PySequence_GetItem(self->args, index); |
| 236 | } |
| 237 | |
Brett Cannon | f6aa86e | 2006-09-20 18:43:13 +0000 | [diff] [blame] | 238 | static PyObject * |
| 239 | BaseException_getslice(PyBaseExceptionObject *self, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 240 | Py_ssize_t start, Py_ssize_t stop) |
Brett Cannon | f6aa86e | 2006-09-20 18:43:13 +0000 | [diff] [blame] | 241 | { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 242 | if (PyErr_WarnPy3k("__getslice__ not supported for exception " |
| 243 | "classes in 3.x; use args attribute", 1) < 0) |
| 244 | return NULL; |
Brett Cannon | f6aa86e | 2006-09-20 18:43:13 +0000 | [diff] [blame] | 245 | return PySequence_GetSlice(self->args, start, stop); |
| 246 | } |
| 247 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 248 | static PySequenceMethods BaseException_as_sequence = { |
| 249 | 0, /* sq_length; */ |
| 250 | 0, /* sq_concat; */ |
| 251 | 0, /* sq_repeat; */ |
| 252 | (ssizeargfunc)BaseException_getitem, /* sq_item; */ |
Brett Cannon | f6aa86e | 2006-09-20 18:43:13 +0000 | [diff] [blame] | 253 | (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */ |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 254 | 0, /* sq_ass_item; */ |
| 255 | 0, /* sq_ass_slice; */ |
| 256 | 0, /* sq_contains; */ |
| 257 | 0, /* sq_inplace_concat; */ |
| 258 | 0 /* sq_inplace_repeat; */ |
| 259 | }; |
| 260 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 261 | static PyObject * |
| 262 | BaseException_get_dict(PyBaseExceptionObject *self) |
| 263 | { |
| 264 | if (self->dict == NULL) { |
| 265 | self->dict = PyDict_New(); |
| 266 | if (!self->dict) |
| 267 | return NULL; |
| 268 | } |
| 269 | Py_INCREF(self->dict); |
| 270 | return self->dict; |
| 271 | } |
| 272 | |
| 273 | static int |
| 274 | BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val) |
| 275 | { |
| 276 | if (val == NULL) { |
| 277 | PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted"); |
| 278 | return -1; |
| 279 | } |
| 280 | if (!PyDict_Check(val)) { |
| 281 | PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary"); |
| 282 | return -1; |
| 283 | } |
| 284 | Py_CLEAR(self->dict); |
| 285 | Py_INCREF(val); |
| 286 | self->dict = val; |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static PyObject * |
| 291 | BaseException_get_args(PyBaseExceptionObject *self) |
| 292 | { |
| 293 | if (self->args == NULL) { |
| 294 | Py_INCREF(Py_None); |
| 295 | return Py_None; |
| 296 | } |
| 297 | Py_INCREF(self->args); |
| 298 | return self->args; |
| 299 | } |
| 300 | |
| 301 | static int |
| 302 | BaseException_set_args(PyBaseExceptionObject *self, PyObject *val) |
| 303 | { |
| 304 | PyObject *seq; |
| 305 | if (val == NULL) { |
| 306 | PyErr_SetString(PyExc_TypeError, "args may not be deleted"); |
| 307 | return -1; |
| 308 | } |
| 309 | seq = PySequence_Tuple(val); |
| 310 | if (!seq) 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 | 8997103 | 2009-09-16 20:34:51 +0000 | [diff] [blame] | 319 | PyObject *msg; |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 320 | |
Georg Brandl | 8997103 | 2009-09-16 20:34:51 +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 | 8997103 | 2009-09-16 20:34:51 +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 | 8997103 | 2009-09-16 20:34:51 +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 | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 356 | |
Georg Brandl | 8997103 | 2009-09-16 20:34:51 +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 | c7c96a9 | 2010-05-09 15:15:40 +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 | c7c96a9 | 2010-05-09 15:15:40 +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); |
| 777 | if (!args) return NULL; |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 778 | |
| 779 | tmp = PyTuple_GET_ITEM(self->args, 0); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 780 | Py_INCREF(tmp); |
| 781 | PyTuple_SET_ITEM(args, 0, tmp); |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 782 | |
| 783 | tmp = PyTuple_GET_ITEM(self->args, 1); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 784 | Py_INCREF(tmp); |
| 785 | PyTuple_SET_ITEM(args, 1, tmp); |
| 786 | |
| 787 | Py_INCREF(self->filename); |
| 788 | PyTuple_SET_ITEM(args, 2, self->filename); |
Georg Brandl | 05f97bf | 2006-05-30 07:13:29 +0000 | [diff] [blame] | 789 | } else |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 790 | Py_INCREF(args); |
Georg Brandl | 05f97bf | 2006-05-30 07:13:29 +0000 | [diff] [blame] | 791 | |
| 792 | if (self->dict) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 793 | res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict); |
Georg Brandl | 05f97bf | 2006-05-30 07:13:29 +0000 | [diff] [blame] | 794 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 795 | res = PyTuple_Pack(2, Py_TYPE(self), args); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 796 | Py_DECREF(args); |
| 797 | return res; |
| 798 | } |
| 799 | |
| 800 | |
| 801 | static PyMethodDef EnvironmentError_methods[] = { |
| 802 | {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS}, |
| 803 | {NULL} |
| 804 | }; |
| 805 | |
| 806 | ComplexExtendsException(PyExc_StandardError, EnvironmentError, |
| 807 | EnvironmentError, EnvironmentError_dealloc, |
| 808 | EnvironmentError_methods, EnvironmentError_members, |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 809 | EnvironmentError_str, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 810 | "Base class for I/O related errors."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 811 | |
| 812 | |
| 813 | /* |
| 814 | * IOError extends EnvironmentError |
| 815 | */ |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 816 | MiddlingExtendsException(PyExc_EnvironmentError, IOError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 817 | EnvironmentError, "I/O operation failed."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 818 | |
| 819 | |
| 820 | /* |
| 821 | * OSError extends EnvironmentError |
| 822 | */ |
| 823 | MiddlingExtendsException(PyExc_EnvironmentError, OSError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 824 | EnvironmentError, "OS system call failed."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 825 | |
| 826 | |
| 827 | /* |
| 828 | * WindowsError extends OSError |
| 829 | */ |
| 830 | #ifdef MS_WINDOWS |
| 831 | #include "errmap.h" |
| 832 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 833 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 834 | WindowsError_clear(PyWindowsErrorObject *self) |
| 835 | { |
| 836 | Py_CLEAR(self->myerrno); |
| 837 | Py_CLEAR(self->strerror); |
| 838 | Py_CLEAR(self->filename); |
| 839 | Py_CLEAR(self->winerror); |
| 840 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 841 | } |
| 842 | |
| 843 | static void |
| 844 | WindowsError_dealloc(PyWindowsErrorObject *self) |
| 845 | { |
Georg Brandl | 38f6237 | 2006-09-06 06:50:05 +0000 | [diff] [blame] | 846 | _PyObject_GC_UNTRACK(self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 847 | WindowsError_clear(self); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 848 | Py_TYPE(self)->tp_free((PyObject *)self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 851 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 852 | WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg) |
| 853 | { |
| 854 | Py_VISIT(self->myerrno); |
| 855 | Py_VISIT(self->strerror); |
| 856 | Py_VISIT(self->filename); |
| 857 | Py_VISIT(self->winerror); |
| 858 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 859 | } |
| 860 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 861 | static int |
| 862 | WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds) |
| 863 | { |
| 864 | PyObject *o_errcode = NULL; |
| 865 | long errcode; |
| 866 | long posix_errno; |
| 867 | |
| 868 | if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds) |
| 869 | == -1) |
| 870 | return -1; |
| 871 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 872 | if (self->myerrno == NULL) |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 873 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 874 | |
| 875 | /* Set errno to the POSIX errno, and winerror to the Win32 |
| 876 | error code. */ |
| 877 | errcode = PyInt_AsLong(self->myerrno); |
| 878 | if (errcode == -1 && PyErr_Occurred()) |
| 879 | return -1; |
| 880 | posix_errno = winerror_to_errno(errcode); |
| 881 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 882 | Py_CLEAR(self->winerror); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 883 | self->winerror = self->myerrno; |
| 884 | |
| 885 | o_errcode = PyInt_FromLong(posix_errno); |
| 886 | if (!o_errcode) |
| 887 | return -1; |
| 888 | |
| 889 | self->myerrno = o_errcode; |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | |
| 895 | static PyObject * |
| 896 | WindowsError_str(PyWindowsErrorObject *self) |
| 897 | { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 898 | PyObject *rtnval = NULL; |
| 899 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 900 | if (self->filename) { |
| 901 | PyObject *fmt; |
| 902 | PyObject *repr; |
| 903 | PyObject *tuple; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 904 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 905 | fmt = PyString_FromString("[Error %s] %s: %s"); |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 906 | if (!fmt) |
| 907 | return NULL; |
| 908 | |
| 909 | repr = PyObject_Repr(self->filename); |
| 910 | if (!repr) { |
| 911 | Py_DECREF(fmt); |
| 912 | return NULL; |
| 913 | } |
| 914 | tuple = PyTuple_New(3); |
| 915 | if (!tuple) { |
| 916 | Py_DECREF(repr); |
| 917 | Py_DECREF(fmt); |
| 918 | return NULL; |
| 919 | } |
| 920 | |
Thomas Heller | df08f0b | 2006-10-27 18:31:36 +0000 | [diff] [blame] | 921 | if (self->winerror) { |
| 922 | Py_INCREF(self->winerror); |
| 923 | PyTuple_SET_ITEM(tuple, 0, self->winerror); |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 924 | } |
| 925 | else { |
| 926 | Py_INCREF(Py_None); |
| 927 | PyTuple_SET_ITEM(tuple, 0, Py_None); |
| 928 | } |
| 929 | if (self->strerror) { |
| 930 | Py_INCREF(self->strerror); |
| 931 | PyTuple_SET_ITEM(tuple, 1, self->strerror); |
| 932 | } |
| 933 | else { |
| 934 | Py_INCREF(Py_None); |
| 935 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
| 936 | } |
| 937 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 938 | PyTuple_SET_ITEM(tuple, 2, repr); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 939 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 940 | rtnval = PyString_Format(fmt, tuple); |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 941 | |
| 942 | Py_DECREF(fmt); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 943 | Py_DECREF(tuple); |
| 944 | } |
Thomas Heller | df08f0b | 2006-10-27 18:31:36 +0000 | [diff] [blame] | 945 | else if (self->winerror && self->strerror) { |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 946 | PyObject *fmt; |
| 947 | PyObject *tuple; |
| 948 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 949 | fmt = PyString_FromString("[Error %s] %s"); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 950 | if (!fmt) |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 951 | return NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 952 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 953 | tuple = PyTuple_New(2); |
| 954 | if (!tuple) { |
| 955 | Py_DECREF(fmt); |
| 956 | return NULL; |
| 957 | } |
| 958 | |
Thomas Heller | df08f0b | 2006-10-27 18:31:36 +0000 | [diff] [blame] | 959 | if (self->winerror) { |
| 960 | Py_INCREF(self->winerror); |
| 961 | PyTuple_SET_ITEM(tuple, 0, self->winerror); |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 962 | } |
| 963 | else { |
| 964 | Py_INCREF(Py_None); |
| 965 | PyTuple_SET_ITEM(tuple, 0, Py_None); |
| 966 | } |
| 967 | if (self->strerror) { |
| 968 | Py_INCREF(self->strerror); |
| 969 | PyTuple_SET_ITEM(tuple, 1, self->strerror); |
| 970 | } |
| 971 | else { |
| 972 | Py_INCREF(Py_None); |
| 973 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
| 974 | } |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 975 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 976 | rtnval = PyString_Format(fmt, tuple); |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 977 | |
| 978 | Py_DECREF(fmt); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 979 | Py_DECREF(tuple); |
| 980 | } |
| 981 | else |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 982 | rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 983 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 984 | return rtnval; |
| 985 | } |
| 986 | |
| 987 | static PyMemberDef WindowsError_members[] = { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 988 | {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0, |
Richard Jones | c5b2a2e | 2006-05-27 16:07:28 +0000 | [diff] [blame] | 989 | PyDoc_STR("POSIX exception code")}, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 990 | {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0, |
Richard Jones | c5b2a2e | 2006-05-27 16:07:28 +0000 | [diff] [blame] | 991 | PyDoc_STR("exception strerror")}, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 992 | {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0, |
Richard Jones | c5b2a2e | 2006-05-27 16:07:28 +0000 | [diff] [blame] | 993 | PyDoc_STR("exception filename")}, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 994 | {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0, |
Richard Jones | c5b2a2e | 2006-05-27 16:07:28 +0000 | [diff] [blame] | 995 | PyDoc_STR("Win32 exception code")}, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 996 | {NULL} /* Sentinel */ |
| 997 | }; |
| 998 | |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 999 | ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError, |
| 1000 | WindowsError_dealloc, 0, WindowsError_members, |
| 1001 | WindowsError_str, "MS-Windows OS system call failed."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1002 | |
| 1003 | #endif /* MS_WINDOWS */ |
| 1004 | |
| 1005 | |
| 1006 | /* |
| 1007 | * VMSError extends OSError (I think) |
| 1008 | */ |
| 1009 | #ifdef __VMS |
| 1010 | MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1011 | "OpenVMS OS system call failed."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1012 | #endif |
| 1013 | |
| 1014 | |
| 1015 | /* |
| 1016 | * EOFError extends StandardError |
| 1017 | */ |
| 1018 | SimpleExtendsException(PyExc_StandardError, EOFError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1019 | "Read beyond end of file."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1020 | |
| 1021 | |
| 1022 | /* |
| 1023 | * RuntimeError extends StandardError |
| 1024 | */ |
| 1025 | SimpleExtendsException(PyExc_StandardError, RuntimeError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1026 | "Unspecified run-time error."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1027 | |
| 1028 | |
| 1029 | /* |
| 1030 | * NotImplementedError extends RuntimeError |
| 1031 | */ |
| 1032 | SimpleExtendsException(PyExc_RuntimeError, NotImplementedError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1033 | "Method or function hasn't been implemented yet."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* |
| 1036 | * NameError extends StandardError |
| 1037 | */ |
| 1038 | SimpleExtendsException(PyExc_StandardError, NameError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1039 | "Name not found globally."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* |
| 1042 | * UnboundLocalError extends NameError |
| 1043 | */ |
| 1044 | SimpleExtendsException(PyExc_NameError, UnboundLocalError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1045 | "Local name referenced but not bound to a value."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* |
| 1048 | * AttributeError extends StandardError |
| 1049 | */ |
| 1050 | SimpleExtendsException(PyExc_StandardError, AttributeError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1051 | "Attribute not found."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1052 | |
| 1053 | |
| 1054 | /* |
| 1055 | * SyntaxError extends StandardError |
| 1056 | */ |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1057 | |
| 1058 | static int |
| 1059 | SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds) |
| 1060 | { |
| 1061 | PyObject *info = NULL; |
| 1062 | Py_ssize_t lenargs = PyTuple_GET_SIZE(args); |
| 1063 | |
| 1064 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1065 | return -1; |
| 1066 | |
| 1067 | if (lenargs >= 1) { |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1068 | Py_CLEAR(self->msg); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1069 | self->msg = PyTuple_GET_ITEM(args, 0); |
| 1070 | Py_INCREF(self->msg); |
| 1071 | } |
| 1072 | if (lenargs == 2) { |
| 1073 | info = PyTuple_GET_ITEM(args, 1); |
| 1074 | info = PySequence_Tuple(info); |
| 1075 | if (!info) return -1; |
| 1076 | |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1077 | if (PyTuple_GET_SIZE(info) != 4) { |
| 1078 | /* not a very good error message, but it's what Python 2.4 gives */ |
| 1079 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 1080 | Py_DECREF(info); |
| 1081 | return -1; |
| 1082 | } |
| 1083 | |
| 1084 | Py_CLEAR(self->filename); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1085 | self->filename = PyTuple_GET_ITEM(info, 0); |
| 1086 | Py_INCREF(self->filename); |
| 1087 | |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1088 | Py_CLEAR(self->lineno); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1089 | self->lineno = PyTuple_GET_ITEM(info, 1); |
| 1090 | Py_INCREF(self->lineno); |
| 1091 | |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1092 | Py_CLEAR(self->offset); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1093 | self->offset = PyTuple_GET_ITEM(info, 2); |
| 1094 | Py_INCREF(self->offset); |
| 1095 | |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1096 | Py_CLEAR(self->text); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1097 | self->text = PyTuple_GET_ITEM(info, 3); |
| 1098 | Py_INCREF(self->text); |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1099 | |
| 1100 | Py_DECREF(info); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1101 | } |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1105 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1106 | SyntaxError_clear(PySyntaxErrorObject *self) |
| 1107 | { |
| 1108 | Py_CLEAR(self->msg); |
| 1109 | Py_CLEAR(self->filename); |
| 1110 | Py_CLEAR(self->lineno); |
| 1111 | Py_CLEAR(self->offset); |
| 1112 | Py_CLEAR(self->text); |
| 1113 | Py_CLEAR(self->print_file_and_line); |
| 1114 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 1115 | } |
| 1116 | |
| 1117 | static void |
| 1118 | SyntaxError_dealloc(PySyntaxErrorObject *self) |
| 1119 | { |
Georg Brandl | 38f6237 | 2006-09-06 06:50:05 +0000 | [diff] [blame] | 1120 | _PyObject_GC_UNTRACK(self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1121 | SyntaxError_clear(self); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1122 | Py_TYPE(self)->tp_free((PyObject *)self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1125 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1126 | SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg) |
| 1127 | { |
| 1128 | Py_VISIT(self->msg); |
| 1129 | Py_VISIT(self->filename); |
| 1130 | Py_VISIT(self->lineno); |
| 1131 | Py_VISIT(self->offset); |
| 1132 | Py_VISIT(self->text); |
| 1133 | Py_VISIT(self->print_file_and_line); |
| 1134 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 1135 | } |
| 1136 | |
| 1137 | /* This is called "my_basename" instead of just "basename" to avoid name |
| 1138 | conflicts with glibc; basename is already prototyped if _GNU_SOURCE is |
| 1139 | defined, and Python does define that. */ |
| 1140 | static char * |
| 1141 | my_basename(char *name) |
| 1142 | { |
| 1143 | char *cp = name; |
| 1144 | char *result = name; |
| 1145 | |
| 1146 | if (name == NULL) |
| 1147 | return "???"; |
| 1148 | while (*cp != '\0') { |
| 1149 | if (*cp == SEP) |
| 1150 | result = cp + 1; |
| 1151 | ++cp; |
| 1152 | } |
| 1153 | return result; |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | static PyObject * |
| 1158 | SyntaxError_str(PySyntaxErrorObject *self) |
| 1159 | { |
| 1160 | PyObject *str; |
| 1161 | PyObject *result; |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1162 | int have_filename = 0; |
| 1163 | int have_lineno = 0; |
| 1164 | char *buffer = NULL; |
Thomas Wouters | c1282ee | 2006-05-28 21:32:12 +0000 | [diff] [blame] | 1165 | Py_ssize_t bufsize; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1166 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1167 | if (self->msg) |
| 1168 | str = PyObject_Str(self->msg); |
| 1169 | else |
| 1170 | str = PyObject_Str(Py_None); |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1171 | if (!str) return NULL; |
| 1172 | /* Don't fiddle with non-string return (shouldn't happen anyway) */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1173 | if (!PyString_Check(str)) return str; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1174 | |
| 1175 | /* XXX -- do all the additional formatting with filename and |
| 1176 | lineno here */ |
| 1177 | |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1178 | have_filename = (self->filename != NULL) && |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1179 | PyString_Check(self->filename); |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1180 | have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1181 | |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1182 | if (!have_filename && !have_lineno) |
| 1183 | return str; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1184 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1185 | bufsize = PyString_GET_SIZE(str) + 64; |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1186 | if (have_filename) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1187 | bufsize += PyString_GET_SIZE(self->filename); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1188 | |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1189 | buffer = PyMem_MALLOC(bufsize); |
| 1190 | if (buffer == NULL) |
| 1191 | return str; |
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 | if (have_filename && have_lineno) |
| 1194 | PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1195 | PyString_AS_STRING(str), |
| 1196 | my_basename(PyString_AS_STRING(self->filename)), |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1197 | PyInt_AsLong(self->lineno)); |
| 1198 | else if (have_filename) |
| 1199 | PyOS_snprintf(buffer, bufsize, "%s (%s)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1200 | PyString_AS_STRING(str), |
| 1201 | my_basename(PyString_AS_STRING(self->filename))); |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1202 | else /* only have_lineno */ |
| 1203 | PyOS_snprintf(buffer, bufsize, "%s (line %ld)", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1204 | PyString_AS_STRING(str), |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1205 | PyInt_AsLong(self->lineno)); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1206 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1207 | result = PyString_FromString(buffer); |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1208 | PyMem_FREE(buffer); |
| 1209 | |
| 1210 | if (result == NULL) |
| 1211 | result = str; |
| 1212 | else |
| 1213 | Py_DECREF(str); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1214 | return result; |
| 1215 | } |
| 1216 | |
| 1217 | static PyMemberDef SyntaxError_members[] = { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1218 | {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0, |
| 1219 | PyDoc_STR("exception msg")}, |
| 1220 | {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0, |
| 1221 | PyDoc_STR("exception filename")}, |
| 1222 | {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0, |
| 1223 | PyDoc_STR("exception lineno")}, |
| 1224 | {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0, |
| 1225 | PyDoc_STR("exception offset")}, |
| 1226 | {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0, |
| 1227 | PyDoc_STR("exception text")}, |
| 1228 | {"print_file_and_line", T_OBJECT, |
| 1229 | offsetof(PySyntaxErrorObject, print_file_and_line), 0, |
| 1230 | PyDoc_STR("exception print_file_and_line")}, |
| 1231 | {NULL} /* Sentinel */ |
| 1232 | }; |
| 1233 | |
| 1234 | ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError, |
| 1235 | SyntaxError_dealloc, 0, SyntaxError_members, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1236 | SyntaxError_str, "Invalid syntax."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1237 | |
| 1238 | |
| 1239 | /* |
| 1240 | * IndentationError extends SyntaxError |
| 1241 | */ |
| 1242 | MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1243 | "Improper indentation."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1244 | |
| 1245 | |
| 1246 | /* |
| 1247 | * TabError extends IndentationError |
| 1248 | */ |
| 1249 | MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1250 | "Improper mixture of spaces and tabs."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1251 | |
| 1252 | |
| 1253 | /* |
| 1254 | * LookupError extends StandardError |
| 1255 | */ |
| 1256 | SimpleExtendsException(PyExc_StandardError, LookupError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1257 | "Base class for lookup errors."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1258 | |
| 1259 | |
| 1260 | /* |
| 1261 | * IndexError extends LookupError |
| 1262 | */ |
| 1263 | SimpleExtendsException(PyExc_LookupError, IndexError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1264 | "Sequence index out of range."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1265 | |
| 1266 | |
| 1267 | /* |
| 1268 | * KeyError extends LookupError |
| 1269 | */ |
| 1270 | static PyObject * |
| 1271 | KeyError_str(PyBaseExceptionObject *self) |
| 1272 | { |
| 1273 | /* If args is a tuple of exactly one item, apply repr to args[0]. |
| 1274 | This is done so that e.g. the exception raised by {}[''] prints |
| 1275 | KeyError: '' |
| 1276 | rather than the confusing |
| 1277 | KeyError |
| 1278 | alone. The downside is that if KeyError is raised with an explanatory |
| 1279 | string, that string will be displayed in quotes. Too bad. |
| 1280 | If args is anything else, use the default BaseException__str__(). |
| 1281 | */ |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1282 | if (PyTuple_GET_SIZE(self->args) == 1) { |
| 1283 | return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1284 | } |
| 1285 | return BaseException_str(self); |
| 1286 | } |
| 1287 | |
| 1288 | ComplexExtendsException(PyExc_LookupError, KeyError, BaseException, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1289 | 0, 0, 0, KeyError_str, "Mapping key not found."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1290 | |
| 1291 | |
| 1292 | /* |
| 1293 | * ValueError extends StandardError |
| 1294 | */ |
| 1295 | SimpleExtendsException(PyExc_StandardError, ValueError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1296 | "Inappropriate argument value (of correct type)."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1297 | |
| 1298 | /* |
| 1299 | * UnicodeError extends ValueError |
| 1300 | */ |
| 1301 | |
| 1302 | SimpleExtendsException(PyExc_ValueError, UnicodeError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1303 | "Unicode related error."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1304 | |
| 1305 | #ifdef Py_USING_UNICODE |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1306 | static PyObject * |
| 1307 | get_string(PyObject *attr, const char *name) |
| 1308 | { |
| 1309 | if (!attr) { |
| 1310 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1314 | if (!PyString_Check(attr)) { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1315 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name); |
| 1316 | return NULL; |
| 1317 | } |
| 1318 | Py_INCREF(attr); |
| 1319 | return attr; |
| 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | static int |
| 1324 | set_string(PyObject **attr, const char *value) |
| 1325 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1326 | PyObject *obj = PyString_FromString(value); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1327 | if (!obj) |
| 1328 | return -1; |
Georg Brandl | 43ab100 | 2006-05-28 20:57:09 +0000 | [diff] [blame] | 1329 | Py_CLEAR(*attr); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1330 | *attr = obj; |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | static PyObject * |
| 1336 | get_unicode(PyObject *attr, const char *name) |
| 1337 | { |
| 1338 | if (!attr) { |
| 1339 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | |
| 1343 | if (!PyUnicode_Check(attr)) { |
| 1344 | PyErr_Format(PyExc_TypeError, |
| 1345 | "%.200s attribute must be unicode", name); |
| 1346 | return NULL; |
| 1347 | } |
| 1348 | Py_INCREF(attr); |
| 1349 | return attr; |
| 1350 | } |
| 1351 | |
| 1352 | PyObject * |
| 1353 | PyUnicodeEncodeError_GetEncoding(PyObject *exc) |
| 1354 | { |
| 1355 | return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
| 1356 | } |
| 1357 | |
| 1358 | PyObject * |
| 1359 | PyUnicodeDecodeError_GetEncoding(PyObject *exc) |
| 1360 | { |
| 1361 | return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
| 1362 | } |
| 1363 | |
| 1364 | PyObject * |
| 1365 | PyUnicodeEncodeError_GetObject(PyObject *exc) |
| 1366 | { |
| 1367 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1368 | } |
| 1369 | |
| 1370 | PyObject * |
| 1371 | PyUnicodeDecodeError_GetObject(PyObject *exc) |
| 1372 | { |
| 1373 | return get_string(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1374 | } |
| 1375 | |
| 1376 | PyObject * |
| 1377 | PyUnicodeTranslateError_GetObject(PyObject *exc) |
| 1378 | { |
| 1379 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1380 | } |
| 1381 | |
| 1382 | int |
| 1383 | PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1384 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1385 | Py_ssize_t size; |
| 1386 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1387 | "object"); |
| 1388 | if (!obj) |
| 1389 | return -1; |
| 1390 | *start = ((PyUnicodeErrorObject *)exc)->start; |
| 1391 | size = PyUnicode_GET_SIZE(obj); |
| 1392 | if (*start<0) |
| 1393 | *start = 0; /*XXX check for values <0*/ |
| 1394 | if (*start>=size) |
| 1395 | *start = size-1; |
| 1396 | Py_DECREF(obj); |
| 1397 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | int |
| 1402 | PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1403 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1404 | Py_ssize_t size; |
| 1405 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, |
| 1406 | "object"); |
| 1407 | if (!obj) |
| 1408 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1409 | size = PyString_GET_SIZE(obj); |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1410 | *start = ((PyUnicodeErrorObject *)exc)->start; |
| 1411 | if (*start<0) |
| 1412 | *start = 0; |
| 1413 | if (*start>=size) |
| 1414 | *start = size-1; |
| 1415 | Py_DECREF(obj); |
| 1416 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | int |
| 1421 | PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1422 | { |
| 1423 | return PyUnicodeEncodeError_GetStart(exc, start); |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | int |
| 1428 | PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1429 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1430 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1431 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | int |
| 1436 | PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1437 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1438 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1439 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | |
| 1443 | int |
| 1444 | PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1445 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1446 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1447 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | |
| 1451 | int |
| 1452 | PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1453 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1454 | Py_ssize_t size; |
| 1455 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1456 | "object"); |
| 1457 | if (!obj) |
| 1458 | return -1; |
| 1459 | *end = ((PyUnicodeErrorObject *)exc)->end; |
| 1460 | size = PyUnicode_GET_SIZE(obj); |
| 1461 | if (*end<1) |
| 1462 | *end = 1; |
| 1463 | if (*end>size) |
| 1464 | *end = size; |
| 1465 | Py_DECREF(obj); |
| 1466 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | int |
| 1471 | PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1472 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1473 | Py_ssize_t size; |
| 1474 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, |
| 1475 | "object"); |
| 1476 | if (!obj) |
| 1477 | return -1; |
| 1478 | *end = ((PyUnicodeErrorObject *)exc)->end; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1479 | size = PyString_GET_SIZE(obj); |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1480 | if (*end<1) |
| 1481 | *end = 1; |
| 1482 | if (*end>size) |
| 1483 | *end = size; |
| 1484 | Py_DECREF(obj); |
| 1485 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | int |
| 1490 | PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start) |
| 1491 | { |
| 1492 | return PyUnicodeEncodeError_GetEnd(exc, start); |
| 1493 | } |
| 1494 | |
| 1495 | |
| 1496 | int |
| 1497 | PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1498 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1499 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1500 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | |
| 1504 | int |
| 1505 | PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1506 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1507 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1508 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | int |
| 1513 | PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1514 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1515 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1516 | return 0; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | PyObject * |
| 1520 | PyUnicodeEncodeError_GetReason(PyObject *exc) |
| 1521 | { |
| 1522 | return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | PyObject * |
| 1527 | PyUnicodeDecodeError_GetReason(PyObject *exc) |
| 1528 | { |
| 1529 | return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
| 1530 | } |
| 1531 | |
| 1532 | |
| 1533 | PyObject * |
| 1534 | PyUnicodeTranslateError_GetReason(PyObject *exc) |
| 1535 | { |
| 1536 | return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
| 1537 | } |
| 1538 | |
| 1539 | |
| 1540 | int |
| 1541 | PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason) |
| 1542 | { |
| 1543 | return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason); |
| 1544 | } |
| 1545 | |
| 1546 | |
| 1547 | int |
| 1548 | PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason) |
| 1549 | { |
| 1550 | return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason); |
| 1551 | } |
| 1552 | |
| 1553 | |
| 1554 | int |
| 1555 | PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason) |
| 1556 | { |
| 1557 | return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason); |
| 1558 | } |
| 1559 | |
| 1560 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1561 | static int |
| 1562 | UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds, |
| 1563 | PyTypeObject *objecttype) |
| 1564 | { |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1565 | Py_CLEAR(self->encoding); |
| 1566 | Py_CLEAR(self->object); |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1567 | Py_CLEAR(self->reason); |
| 1568 | |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1569 | if (!PyArg_ParseTuple(args, "O!O!nnO!", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1570 | &PyString_Type, &self->encoding, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1571 | objecttype, &self->object, |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1572 | &self->start, |
| 1573 | &self->end, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1574 | &PyString_Type, &self->reason)) { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1575 | self->encoding = self->object = self->reason = NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1576 | return -1; |
| 1577 | } |
| 1578 | |
| 1579 | Py_INCREF(self->encoding); |
| 1580 | Py_INCREF(self->object); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1581 | Py_INCREF(self->reason); |
| 1582 | |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1586 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1587 | UnicodeError_clear(PyUnicodeErrorObject *self) |
| 1588 | { |
| 1589 | Py_CLEAR(self->encoding); |
| 1590 | Py_CLEAR(self->object); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1591 | Py_CLEAR(self->reason); |
| 1592 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 1593 | } |
| 1594 | |
| 1595 | static void |
| 1596 | UnicodeError_dealloc(PyUnicodeErrorObject *self) |
| 1597 | { |
Georg Brandl | 38f6237 | 2006-09-06 06:50:05 +0000 | [diff] [blame] | 1598 | _PyObject_GC_UNTRACK(self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1599 | UnicodeError_clear(self); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1600 | Py_TYPE(self)->tp_free((PyObject *)self); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1603 | static int |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1604 | UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg) |
| 1605 | { |
| 1606 | Py_VISIT(self->encoding); |
| 1607 | Py_VISIT(self->object); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1608 | Py_VISIT(self->reason); |
| 1609 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 1610 | } |
| 1611 | |
| 1612 | static PyMemberDef UnicodeError_members[] = { |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1613 | {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0, |
| 1614 | PyDoc_STR("exception encoding")}, |
| 1615 | {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0, |
| 1616 | PyDoc_STR("exception object")}, |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1617 | {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1618 | PyDoc_STR("exception start")}, |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1619 | {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1620 | PyDoc_STR("exception end")}, |
| 1621 | {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0, |
| 1622 | PyDoc_STR("exception reason")}, |
| 1623 | {NULL} /* Sentinel */ |
| 1624 | }; |
| 1625 | |
| 1626 | |
| 1627 | /* |
| 1628 | * UnicodeEncodeError extends UnicodeError |
| 1629 | */ |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1630 | |
| 1631 | static int |
| 1632 | UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1633 | { |
| 1634 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1635 | return -1; |
| 1636 | return UnicodeError_init((PyUnicodeErrorObject *)self, args, |
| 1637 | kwds, &PyUnicode_Type); |
| 1638 | } |
| 1639 | |
| 1640 | static PyObject * |
| 1641 | UnicodeEncodeError_str(PyObject *self) |
| 1642 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1643 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1644 | PyObject *result = NULL; |
| 1645 | PyObject *reason_str = NULL; |
| 1646 | PyObject *encoding_str = NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1647 | |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1648 | /* Get reason and encoding as strings, which they might not be if |
| 1649 | they've been modified after we were contructed. */ |
| 1650 | reason_str = PyObject_Str(uself->reason); |
| 1651 | if (reason_str == NULL) |
| 1652 | goto done; |
| 1653 | encoding_str = PyObject_Str(uself->encoding); |
| 1654 | if (encoding_str == NULL) |
| 1655 | goto done; |
| 1656 | |
| 1657 | 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] | 1658 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1659 | char badchar_str[20]; |
| 1660 | if (badchar <= 0xff) |
| 1661 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar); |
| 1662 | else if (badchar <= 0xffff) |
| 1663 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar); |
| 1664 | else |
| 1665 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar); |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1666 | result = PyString_FromFormat( |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1667 | "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s", |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1668 | PyString_AS_STRING(encoding_str), |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1669 | badchar_str, |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1670 | uself->start, |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1671 | PyString_AS_STRING(reason_str)); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1672 | } |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1673 | else { |
| 1674 | result = PyString_FromFormat( |
| 1675 | "'%.400s' codec can't encode characters in position %zd-%zd: %.400s", |
| 1676 | PyString_AS_STRING(encoding_str), |
| 1677 | uself->start, |
| 1678 | uself->end-1, |
| 1679 | PyString_AS_STRING(reason_str)); |
| 1680 | } |
| 1681 | done: |
| 1682 | Py_XDECREF(reason_str); |
| 1683 | Py_XDECREF(encoding_str); |
| 1684 | return result; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | static PyTypeObject _PyExc_UnicodeEncodeError = { |
| 1688 | PyObject_HEAD_INIT(NULL) |
| 1689 | 0, |
Georg Brandl | 38f6237 | 2006-09-06 06:50:05 +0000 | [diff] [blame] | 1690 | EXC_MODULE_NAME "UnicodeEncodeError", |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1691 | sizeof(PyUnicodeErrorObject), 0, |
| 1692 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1693 | (reprfunc)UnicodeEncodeError_str, 0, 0, 0, |
| 1694 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Michael W. Hudson | 2759627 | 2006-05-28 21:19:03 +0000 | [diff] [blame] | 1695 | PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse, |
| 1696 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1697 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1698 | (initproc)UnicodeEncodeError_init, 0, BaseException_new, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1699 | }; |
| 1700 | PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError; |
| 1701 | |
| 1702 | PyObject * |
| 1703 | PyUnicodeEncodeError_Create( |
| 1704 | const char *encoding, const Py_UNICODE *object, Py_ssize_t length, |
| 1705 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1706 | { |
| 1707 | return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns", |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1708 | encoding, object, length, start, end, reason); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | /* |
| 1713 | * UnicodeDecodeError extends UnicodeError |
| 1714 | */ |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1715 | |
| 1716 | static int |
| 1717 | UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1718 | { |
| 1719 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1720 | return -1; |
| 1721 | return UnicodeError_init((PyUnicodeErrorObject *)self, args, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1722 | kwds, &PyString_Type); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | static PyObject * |
| 1726 | UnicodeDecodeError_str(PyObject *self) |
| 1727 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1728 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1729 | PyObject *result = NULL; |
| 1730 | PyObject *reason_str = NULL; |
| 1731 | PyObject *encoding_str = NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1732 | |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1733 | /* Get reason and encoding as strings, which they might not be if |
| 1734 | they've been modified after we were contructed. */ |
| 1735 | reason_str = PyObject_Str(uself->reason); |
| 1736 | if (reason_str == NULL) |
| 1737 | goto done; |
| 1738 | encoding_str = PyObject_Str(uself->encoding); |
| 1739 | if (encoding_str == NULL) |
| 1740 | goto done; |
| 1741 | |
| 1742 | 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] | 1743 | /* FromFormat does not support %02x, so format that separately */ |
| 1744 | char byte[4]; |
| 1745 | PyOS_snprintf(byte, sizeof(byte), "%02x", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1746 | ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff); |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1747 | result = PyString_FromFormat( |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1748 | "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s", |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1749 | PyString_AS_STRING(encoding_str), |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1750 | byte, |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1751 | uself->start, |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1752 | PyString_AS_STRING(reason_str)); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1753 | } |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1754 | else { |
| 1755 | result = PyString_FromFormat( |
| 1756 | "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s", |
| 1757 | PyString_AS_STRING(encoding_str), |
| 1758 | uself->start, |
| 1759 | uself->end-1, |
| 1760 | PyString_AS_STRING(reason_str)); |
| 1761 | } |
| 1762 | done: |
| 1763 | Py_XDECREF(reason_str); |
| 1764 | Py_XDECREF(encoding_str); |
| 1765 | return result; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | static PyTypeObject _PyExc_UnicodeDecodeError = { |
| 1769 | PyObject_HEAD_INIT(NULL) |
| 1770 | 0, |
| 1771 | EXC_MODULE_NAME "UnicodeDecodeError", |
| 1772 | sizeof(PyUnicodeErrorObject), 0, |
| 1773 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1774 | (reprfunc)UnicodeDecodeError_str, 0, 0, 0, |
| 1775 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Michael W. Hudson | 2759627 | 2006-05-28 21:19:03 +0000 | [diff] [blame] | 1776 | PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse, |
| 1777 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1778 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1779 | (initproc)UnicodeDecodeError_init, 0, BaseException_new, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1780 | }; |
| 1781 | PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError; |
| 1782 | |
| 1783 | PyObject * |
| 1784 | PyUnicodeDecodeError_Create( |
| 1785 | const char *encoding, const char *object, Py_ssize_t length, |
| 1786 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1787 | { |
| 1788 | assert(length < INT_MAX); |
| 1789 | assert(start < INT_MAX); |
| 1790 | assert(end < INT_MAX); |
| 1791 | return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns", |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1792 | encoding, object, length, start, end, reason); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | /* |
| 1797 | * UnicodeTranslateError extends UnicodeError |
| 1798 | */ |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1799 | |
| 1800 | static int |
| 1801 | UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args, |
| 1802 | PyObject *kwds) |
| 1803 | { |
| 1804 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1805 | return -1; |
| 1806 | |
| 1807 | Py_CLEAR(self->object); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1808 | Py_CLEAR(self->reason); |
| 1809 | |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1810 | if (!PyArg_ParseTuple(args, "O!nnO!", |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1811 | &PyUnicode_Type, &self->object, |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1812 | &self->start, |
| 1813 | &self->end, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1814 | &PyString_Type, &self->reason)) { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1815 | self->object = self->reason = NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1816 | return -1; |
| 1817 | } |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1818 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1819 | Py_INCREF(self->object); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1820 | Py_INCREF(self->reason); |
| 1821 | |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
| 1825 | |
| 1826 | static PyObject * |
| 1827 | UnicodeTranslateError_str(PyObject *self) |
| 1828 | { |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1829 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1830 | PyObject *result = NULL; |
| 1831 | PyObject *reason_str = NULL; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1832 | |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1833 | /* Get reason as a string, which it might not be if it's been |
| 1834 | modified after we were contructed. */ |
| 1835 | reason_str = PyObject_Str(uself->reason); |
| 1836 | if (reason_str == NULL) |
| 1837 | goto done; |
| 1838 | |
| 1839 | 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] | 1840 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1841 | char badchar_str[20]; |
| 1842 | if (badchar <= 0xff) |
| 1843 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar); |
| 1844 | else if (badchar <= 0xffff) |
| 1845 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar); |
| 1846 | else |
| 1847 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar); |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1848 | result = PyString_FromFormat( |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1849 | "can't translate character u'\\%s' in position %zd: %.400s", |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1850 | badchar_str, |
Walter Dörwald | 84a3efe | 2007-06-13 16:57:12 +0000 | [diff] [blame] | 1851 | uself->start, |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1852 | PyString_AS_STRING(reason_str)); |
| 1853 | } else { |
| 1854 | result = PyString_FromFormat( |
| 1855 | "can't translate characters in position %zd-%zd: %.400s", |
| 1856 | uself->start, |
| 1857 | uself->end-1, |
| 1858 | PyString_AS_STRING(reason_str)); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1859 | } |
Eric Smith | 6680785 | 2010-02-24 14:27:37 +0000 | [diff] [blame] | 1860 | done: |
| 1861 | Py_XDECREF(reason_str); |
| 1862 | return result; |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | static PyTypeObject _PyExc_UnicodeTranslateError = { |
| 1866 | PyObject_HEAD_INIT(NULL) |
| 1867 | 0, |
| 1868 | EXC_MODULE_NAME "UnicodeTranslateError", |
| 1869 | sizeof(PyUnicodeErrorObject), 0, |
| 1870 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1871 | (reprfunc)UnicodeTranslateError_str, 0, 0, 0, |
| 1872 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Georg Brandl | 38f6237 | 2006-09-06 06:50:05 +0000 | [diff] [blame] | 1873 | PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1874 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
| 1875 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Michael W. Hudson | 96495ee | 2006-05-28 17:40:29 +0000 | [diff] [blame] | 1876 | (initproc)UnicodeTranslateError_init, 0, BaseException_new, |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1877 | }; |
| 1878 | PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError; |
| 1879 | |
| 1880 | PyObject * |
| 1881 | PyUnicodeTranslateError_Create( |
| 1882 | const Py_UNICODE *object, Py_ssize_t length, |
| 1883 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1884 | { |
| 1885 | return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns", |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 1886 | object, length, start, end, reason); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1887 | } |
| 1888 | #endif |
| 1889 | |
| 1890 | |
| 1891 | /* |
| 1892 | * AssertionError extends StandardError |
| 1893 | */ |
| 1894 | SimpleExtendsException(PyExc_StandardError, AssertionError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1895 | "Assertion failed."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1896 | |
| 1897 | |
| 1898 | /* |
| 1899 | * ArithmeticError extends StandardError |
| 1900 | */ |
| 1901 | SimpleExtendsException(PyExc_StandardError, ArithmeticError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1902 | "Base class for arithmetic errors."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1903 | |
| 1904 | |
| 1905 | /* |
| 1906 | * FloatingPointError extends ArithmeticError |
| 1907 | */ |
| 1908 | SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1909 | "Floating point operation failed."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1910 | |
| 1911 | |
| 1912 | /* |
| 1913 | * OverflowError extends ArithmeticError |
| 1914 | */ |
| 1915 | SimpleExtendsException(PyExc_ArithmeticError, OverflowError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1916 | "Result too large to be represented."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1917 | |
| 1918 | |
| 1919 | /* |
| 1920 | * ZeroDivisionError extends ArithmeticError |
| 1921 | */ |
| 1922 | SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1923 | "Second argument to a division or modulo operation was zero."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1924 | |
| 1925 | |
| 1926 | /* |
| 1927 | * SystemError extends StandardError |
| 1928 | */ |
| 1929 | SimpleExtendsException(PyExc_StandardError, SystemError, |
| 1930 | "Internal error in the Python interpreter.\n" |
| 1931 | "\n" |
| 1932 | "Please report this to the Python maintainer, along with the traceback,\n" |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1933 | "the Python version, and the hardware/OS platform and version."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1934 | |
| 1935 | |
| 1936 | /* |
| 1937 | * ReferenceError extends StandardError |
| 1938 | */ |
| 1939 | SimpleExtendsException(PyExc_StandardError, ReferenceError, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1940 | "Weak ref proxy used after referent went away."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1941 | |
| 1942 | |
| 1943 | /* |
| 1944 | * MemoryError extends StandardError |
| 1945 | */ |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1946 | SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1947 | |
Travis E. Oliphant | 3781aef | 2008-03-18 04:44:57 +0000 | [diff] [blame] | 1948 | /* |
| 1949 | * BufferError extends StandardError |
| 1950 | */ |
| 1951 | SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error."); |
| 1952 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1953 | |
| 1954 | /* Warning category docstrings */ |
| 1955 | |
| 1956 | /* |
| 1957 | * Warning extends Exception |
| 1958 | */ |
| 1959 | SimpleExtendsException(PyExc_Exception, Warning, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1960 | "Base class for warning categories."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1961 | |
| 1962 | |
| 1963 | /* |
| 1964 | * UserWarning extends Warning |
| 1965 | */ |
| 1966 | SimpleExtendsException(PyExc_Warning, UserWarning, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1967 | "Base class for warnings generated by user code."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1968 | |
| 1969 | |
| 1970 | /* |
| 1971 | * DeprecationWarning extends Warning |
| 1972 | */ |
| 1973 | SimpleExtendsException(PyExc_Warning, DeprecationWarning, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1974 | "Base class for warnings about deprecated features."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1975 | |
| 1976 | |
| 1977 | /* |
| 1978 | * PendingDeprecationWarning extends Warning |
| 1979 | */ |
| 1980 | SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning, |
| 1981 | "Base class for warnings about features which will be deprecated\n" |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1982 | "in the future."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1983 | |
| 1984 | |
| 1985 | /* |
| 1986 | * SyntaxWarning extends Warning |
| 1987 | */ |
| 1988 | SimpleExtendsException(PyExc_Warning, SyntaxWarning, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1989 | "Base class for warnings about dubious syntax."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1990 | |
| 1991 | |
| 1992 | /* |
| 1993 | * RuntimeWarning extends Warning |
| 1994 | */ |
| 1995 | SimpleExtendsException(PyExc_Warning, RuntimeWarning, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 1996 | "Base class for warnings about dubious runtime behavior."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 1997 | |
| 1998 | |
| 1999 | /* |
| 2000 | * FutureWarning extends Warning |
| 2001 | */ |
| 2002 | SimpleExtendsException(PyExc_Warning, FutureWarning, |
| 2003 | "Base class for warnings about constructs that will change semantically\n" |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 2004 | "in the future."); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2005 | |
| 2006 | |
| 2007 | /* |
| 2008 | * ImportWarning extends Warning |
| 2009 | */ |
| 2010 | SimpleExtendsException(PyExc_Warning, ImportWarning, |
Richard Jones | 2d555b3 | 2006-05-27 16:15:11 +0000 | [diff] [blame] | 2011 | "Base class for warnings about probable mistakes in module imports"); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2012 | |
| 2013 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 2014 | /* |
| 2015 | * UnicodeWarning extends Warning |
| 2016 | */ |
| 2017 | SimpleExtendsException(PyExc_Warning, UnicodeWarning, |
| 2018 | "Base class for warnings about Unicode related problems, mostly\n" |
| 2019 | "related to conversion problems."); |
| 2020 | |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 2021 | /* |
| 2022 | * BytesWarning extends Warning |
| 2023 | */ |
| 2024 | SimpleExtendsException(PyExc_Warning, BytesWarning, |
| 2025 | "Base class for warnings about bytes and buffer related problems, mostly\n" |
| 2026 | "related to conversion from str or comparing to str."); |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 2027 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2028 | /* Pre-computed MemoryError instance. Best to create this as early as |
| 2029 | * possible and not wait until a MemoryError is actually raised! |
| 2030 | */ |
| 2031 | PyObject *PyExc_MemoryErrorInst=NULL; |
| 2032 | |
Brett Cannon | 1e534b5 | 2007-09-07 04:18:30 +0000 | [diff] [blame] | 2033 | /* Pre-computed RuntimeError instance for when recursion depth is reached. |
| 2034 | Meant to be used when normalizing the exception for exceeding the recursion |
| 2035 | depth will cause its own infinite recursion. |
| 2036 | */ |
| 2037 | PyObject *PyExc_RecursionErrorInst = NULL; |
| 2038 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2039 | /* module global functions */ |
| 2040 | static PyMethodDef functions[] = { |
| 2041 | /* Sentinel */ |
| 2042 | {NULL, NULL} |
| 2043 | }; |
| 2044 | |
| 2045 | #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \ |
| 2046 | Py_FatalError("exceptions bootstrapping error."); |
| 2047 | |
| 2048 | #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \ |
| 2049 | PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \ |
| 2050 | if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \ |
| 2051 | Py_FatalError("Module dictionary insertion problem."); |
| 2052 | |
Kristján Valur Jónsson | 74c3ea0 | 2006-07-03 14:59:05 +0000 | [diff] [blame] | 2053 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
Kristján Valur Jónsson | f608317 | 2006-06-12 15:45:12 +0000 | [diff] [blame] | 2054 | /* crt variable checking in VisualStudio .NET 2005 */ |
| 2055 | #include <crtdbg.h> |
| 2056 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 2057 | static int prevCrtReportMode; |
| 2058 | static _invalid_parameter_handler prevCrtHandler; |
Kristján Valur Jónsson | f608317 | 2006-06-12 15:45:12 +0000 | [diff] [blame] | 2059 | |
| 2060 | /* Invalid parameter handler. Sets a ValueError exception */ |
| 2061 | static void |
| 2062 | InvalidParameterHandler( |
| 2063 | const wchar_t * expression, |
| 2064 | const wchar_t * function, |
| 2065 | const wchar_t * file, |
| 2066 | unsigned int line, |
| 2067 | uintptr_t pReserved) |
| 2068 | { |
| 2069 | /* Do nothing, allow execution to continue. Usually this |
| 2070 | * means that the CRT will set errno to EINVAL |
| 2071 | */ |
| 2072 | } |
| 2073 | #endif |
| 2074 | |
| 2075 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2076 | PyMODINIT_FUNC |
Michael W. Hudson | 22a80e7 | 2006-05-28 15:51:40 +0000 | [diff] [blame] | 2077 | _PyExc_Init(void) |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2078 | { |
| 2079 | PyObject *m, *bltinmod, *bdict; |
| 2080 | |
| 2081 | PRE_INIT(BaseException) |
| 2082 | PRE_INIT(Exception) |
| 2083 | PRE_INIT(StandardError) |
| 2084 | PRE_INIT(TypeError) |
| 2085 | PRE_INIT(StopIteration) |
| 2086 | PRE_INIT(GeneratorExit) |
| 2087 | PRE_INIT(SystemExit) |
| 2088 | PRE_INIT(KeyboardInterrupt) |
| 2089 | PRE_INIT(ImportError) |
| 2090 | PRE_INIT(EnvironmentError) |
| 2091 | PRE_INIT(IOError) |
| 2092 | PRE_INIT(OSError) |
| 2093 | #ifdef MS_WINDOWS |
| 2094 | PRE_INIT(WindowsError) |
| 2095 | #endif |
| 2096 | #ifdef __VMS |
| 2097 | PRE_INIT(VMSError) |
| 2098 | #endif |
| 2099 | PRE_INIT(EOFError) |
| 2100 | PRE_INIT(RuntimeError) |
| 2101 | PRE_INIT(NotImplementedError) |
| 2102 | PRE_INIT(NameError) |
| 2103 | PRE_INIT(UnboundLocalError) |
| 2104 | PRE_INIT(AttributeError) |
| 2105 | PRE_INIT(SyntaxError) |
| 2106 | PRE_INIT(IndentationError) |
| 2107 | PRE_INIT(TabError) |
| 2108 | PRE_INIT(LookupError) |
| 2109 | PRE_INIT(IndexError) |
| 2110 | PRE_INIT(KeyError) |
| 2111 | PRE_INIT(ValueError) |
| 2112 | PRE_INIT(UnicodeError) |
| 2113 | #ifdef Py_USING_UNICODE |
| 2114 | PRE_INIT(UnicodeEncodeError) |
| 2115 | PRE_INIT(UnicodeDecodeError) |
| 2116 | PRE_INIT(UnicodeTranslateError) |
| 2117 | #endif |
| 2118 | PRE_INIT(AssertionError) |
| 2119 | PRE_INIT(ArithmeticError) |
| 2120 | PRE_INIT(FloatingPointError) |
| 2121 | PRE_INIT(OverflowError) |
| 2122 | PRE_INIT(ZeroDivisionError) |
| 2123 | PRE_INIT(SystemError) |
| 2124 | PRE_INIT(ReferenceError) |
| 2125 | PRE_INIT(MemoryError) |
Benjamin Peterson | c0bf76d | 2008-07-30 17:45:10 +0000 | [diff] [blame] | 2126 | PRE_INIT(BufferError) |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2127 | PRE_INIT(Warning) |
| 2128 | PRE_INIT(UserWarning) |
| 2129 | PRE_INIT(DeprecationWarning) |
| 2130 | PRE_INIT(PendingDeprecationWarning) |
| 2131 | PRE_INIT(SyntaxWarning) |
| 2132 | PRE_INIT(RuntimeWarning) |
| 2133 | PRE_INIT(FutureWarning) |
| 2134 | PRE_INIT(ImportWarning) |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 2135 | PRE_INIT(UnicodeWarning) |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 2136 | PRE_INIT(BytesWarning) |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2137 | |
Richard Jones | c5b2a2e | 2006-05-27 16:07:28 +0000 | [diff] [blame] | 2138 | m = Py_InitModule4("exceptions", functions, exceptions_doc, |
| 2139 | (PyObject *)NULL, PYTHON_API_VERSION); |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2140 | if (m == NULL) return; |
| 2141 | |
| 2142 | bltinmod = PyImport_ImportModule("__builtin__"); |
| 2143 | if (bltinmod == NULL) |
| 2144 | Py_FatalError("exceptions bootstrapping error."); |
| 2145 | bdict = PyModule_GetDict(bltinmod); |
| 2146 | if (bdict == NULL) |
| 2147 | Py_FatalError("exceptions bootstrapping error."); |
| 2148 | |
| 2149 | POST_INIT(BaseException) |
| 2150 | POST_INIT(Exception) |
| 2151 | POST_INIT(StandardError) |
| 2152 | POST_INIT(TypeError) |
| 2153 | POST_INIT(StopIteration) |
| 2154 | POST_INIT(GeneratorExit) |
| 2155 | POST_INIT(SystemExit) |
| 2156 | POST_INIT(KeyboardInterrupt) |
| 2157 | POST_INIT(ImportError) |
| 2158 | POST_INIT(EnvironmentError) |
| 2159 | POST_INIT(IOError) |
| 2160 | POST_INIT(OSError) |
| 2161 | #ifdef MS_WINDOWS |
| 2162 | POST_INIT(WindowsError) |
| 2163 | #endif |
| 2164 | #ifdef __VMS |
| 2165 | POST_INIT(VMSError) |
| 2166 | #endif |
| 2167 | POST_INIT(EOFError) |
| 2168 | POST_INIT(RuntimeError) |
| 2169 | POST_INIT(NotImplementedError) |
| 2170 | POST_INIT(NameError) |
| 2171 | POST_INIT(UnboundLocalError) |
| 2172 | POST_INIT(AttributeError) |
| 2173 | POST_INIT(SyntaxError) |
| 2174 | POST_INIT(IndentationError) |
| 2175 | POST_INIT(TabError) |
| 2176 | POST_INIT(LookupError) |
| 2177 | POST_INIT(IndexError) |
| 2178 | POST_INIT(KeyError) |
| 2179 | POST_INIT(ValueError) |
| 2180 | POST_INIT(UnicodeError) |
| 2181 | #ifdef Py_USING_UNICODE |
| 2182 | POST_INIT(UnicodeEncodeError) |
| 2183 | POST_INIT(UnicodeDecodeError) |
| 2184 | POST_INIT(UnicodeTranslateError) |
| 2185 | #endif |
| 2186 | POST_INIT(AssertionError) |
| 2187 | POST_INIT(ArithmeticError) |
| 2188 | POST_INIT(FloatingPointError) |
| 2189 | POST_INIT(OverflowError) |
| 2190 | POST_INIT(ZeroDivisionError) |
| 2191 | POST_INIT(SystemError) |
| 2192 | POST_INIT(ReferenceError) |
| 2193 | POST_INIT(MemoryError) |
Benjamin Peterson | c0bf76d | 2008-07-30 17:45:10 +0000 | [diff] [blame] | 2194 | POST_INIT(BufferError) |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2195 | POST_INIT(Warning) |
| 2196 | POST_INIT(UserWarning) |
| 2197 | POST_INIT(DeprecationWarning) |
| 2198 | POST_INIT(PendingDeprecationWarning) |
| 2199 | POST_INIT(SyntaxWarning) |
| 2200 | POST_INIT(RuntimeWarning) |
| 2201 | POST_INIT(FutureWarning) |
| 2202 | POST_INIT(ImportWarning) |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 2203 | POST_INIT(UnicodeWarning) |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 2204 | POST_INIT(BytesWarning) |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2205 | |
| 2206 | PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL); |
| 2207 | if (!PyExc_MemoryErrorInst) |
| 2208 | Py_FatalError("Cannot pre-allocate MemoryError instance\n"); |
| 2209 | |
Brett Cannon | 1e534b5 | 2007-09-07 04:18:30 +0000 | [diff] [blame] | 2210 | PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL); |
| 2211 | if (!PyExc_RecursionErrorInst) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 2212 | Py_FatalError("Cannot pre-allocate RuntimeError instance for " |
| 2213 | "recursion errors"); |
Brett Cannon | 1e534b5 | 2007-09-07 04:18:30 +0000 | [diff] [blame] | 2214 | else { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 2215 | PyBaseExceptionObject *err_inst = |
| 2216 | (PyBaseExceptionObject *)PyExc_RecursionErrorInst; |
| 2217 | PyObject *args_tuple; |
| 2218 | PyObject *exc_message; |
| 2219 | exc_message = PyString_FromString("maximum recursion depth exceeded"); |
| 2220 | if (!exc_message) |
| 2221 | Py_FatalError("cannot allocate argument for RuntimeError " |
| 2222 | "pre-allocation"); |
| 2223 | args_tuple = PyTuple_Pack(1, exc_message); |
| 2224 | if (!args_tuple) |
| 2225 | Py_FatalError("cannot allocate tuple for RuntimeError " |
| 2226 | "pre-allocation"); |
| 2227 | Py_DECREF(exc_message); |
| 2228 | if (BaseException_init(err_inst, args_tuple, NULL)) |
| 2229 | Py_FatalError("init of pre-allocated RuntimeError failed"); |
| 2230 | Py_DECREF(args_tuple); |
Brett Cannon | 1e534b5 | 2007-09-07 04:18:30 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2233 | Py_DECREF(bltinmod); |
Kristján Valur Jónsson | f608317 | 2006-06-12 15:45:12 +0000 | [diff] [blame] | 2234 | |
Kristján Valur Jónsson | 74c3ea0 | 2006-07-03 14:59:05 +0000 | [diff] [blame] | 2235 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
Kristján Valur Jónsson | f608317 | 2006-06-12 15:45:12 +0000 | [diff] [blame] | 2236 | /* Set CRT argument error handler */ |
| 2237 | prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler); |
| 2238 | /* turn off assertions in debug mode */ |
| 2239 | prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0); |
| 2240 | #endif |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | void |
| 2244 | _PyExc_Fini(void) |
| 2245 | { |
| 2246 | Py_XDECREF(PyExc_MemoryErrorInst); |
| 2247 | PyExc_MemoryErrorInst = NULL; |
Kristján Valur Jónsson | 74c3ea0 | 2006-07-03 14:59:05 +0000 | [diff] [blame] | 2248 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
Kristján Valur Jónsson | f608317 | 2006-06-12 15:45:12 +0000 | [diff] [blame] | 2249 | /* reset CRT error handling */ |
| 2250 | _set_invalid_parameter_handler(prevCrtHandler); |
| 2251 | _CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode); |
| 2252 | #endif |
Richard Jones | 7b9558d | 2006-05-27 12:29:24 +0000 | [diff] [blame] | 2253 | } |