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