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