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