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