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