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