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