Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | #define PY_SSIZE_T_CLEAN |
| 8 | #include <Python.h> |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 9 | #include "pycore_initconfig.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 10 | #include "pycore_object.h" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 11 | #include "structmember.h" // PyMemberDef |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 12 | #include "osdefs.h" // SEP |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 15 | /* Compatibility aliases */ |
| 16 | PyObject *PyExc_EnvironmentError = NULL; |
| 17 | PyObject *PyExc_IOError = NULL; |
| 18 | #ifdef MS_WINDOWS |
| 19 | PyObject *PyExc_WindowsError = NULL; |
| 20 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 21 | |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 22 | |
| 23 | static struct _Py_exc_state* |
| 24 | get_exc_state(void) |
| 25 | { |
| 26 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 27 | return &interp->exc_state; |
| 28 | } |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 29 | |
| 30 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 31 | /* NOTE: If the exception class hierarchy changes, don't forget to update |
| 32 | * Lib/test/exception_hierarchy.txt |
| 33 | */ |
| 34 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 35 | /* |
| 36 | * BaseException |
| 37 | */ |
| 38 | static PyObject * |
| 39 | BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 40 | { |
| 41 | PyBaseExceptionObject *self; |
| 42 | |
| 43 | self = (PyBaseExceptionObject *)type->tp_alloc(type, 0); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 44 | if (!self) |
| 45 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 46 | /* the dict is created on the fly in PyObject_GenericSetAttr */ |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 47 | self->dict = NULL; |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 48 | self->traceback = self->cause = self->context = NULL; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 49 | self->suppress_context = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 50 | |
Richard Oudkerk | 5562d9d | 2012-07-28 17:45:28 +0100 | [diff] [blame] | 51 | if (args) { |
| 52 | self->args = args; |
| 53 | Py_INCREF(args); |
| 54 | return (PyObject *)self; |
| 55 | } |
| 56 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 57 | self->args = PyTuple_New(0); |
| 58 | if (!self->args) { |
| 59 | Py_DECREF(self); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 63 | return (PyObject *)self; |
| 64 | } |
| 65 | |
| 66 | static int |
| 67 | BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) |
| 68 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 69 | if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 70 | return -1; |
| 71 | |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 72 | Py_INCREF(args); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 73 | Py_XSETREF(self->args, args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 78 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 79 | BaseException_clear(PyBaseExceptionObject *self) |
| 80 | { |
| 81 | Py_CLEAR(self->dict); |
| 82 | Py_CLEAR(self->args); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 83 | Py_CLEAR(self->traceback); |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 84 | Py_CLEAR(self->cause); |
| 85 | Py_CLEAR(self->context); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | BaseException_dealloc(PyBaseExceptionObject *self) |
| 91 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 92 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 93 | BaseException_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 94 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 97 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 98 | BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg) |
| 99 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 100 | Py_VISIT(self->dict); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 101 | Py_VISIT(self->args); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 102 | Py_VISIT(self->traceback); |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 103 | Py_VISIT(self->cause); |
| 104 | Py_VISIT(self->context); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static PyObject * |
| 109 | BaseException_str(PyBaseExceptionObject *self) |
| 110 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 111 | switch (PyTuple_GET_SIZE(self->args)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 112 | case 0: |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 113 | return PyUnicode_FromString(""); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 114 | case 1: |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 115 | return PyObject_Str(PyTuple_GET_ITEM(self->args, 0)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 116 | default: |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 117 | return PyObject_Str(self->args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 118 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static PyObject * |
| 122 | BaseException_repr(PyBaseExceptionObject *self) |
| 123 | { |
Serhiy Storchaka | 4ab46d7 | 2017-09-17 21:11:04 +0300 | [diff] [blame] | 124 | const char *name = _PyType_Name(Py_TYPE(self)); |
Serhiy Storchaka | f8a4c03 | 2017-11-15 17:53:28 +0200 | [diff] [blame] | 125 | if (PyTuple_GET_SIZE(self->args) == 1) |
| 126 | return PyUnicode_FromFormat("%s(%R)", name, |
| 127 | PyTuple_GET_ITEM(self->args, 0)); |
| 128 | else |
| 129 | return PyUnicode_FromFormat("%s%R", name, self->args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Pickling support */ |
| 133 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 134 | BaseException_reduce(PyBaseExceptionObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 135 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 136 | if (self->args && self->dict) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 137 | return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 138 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 139 | return PyTuple_Pack(2, Py_TYPE(self), self->args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 142 | /* |
| 143 | * Needed for backward compatibility, since exceptions used to store |
| 144 | * all their attributes in the __dict__. Code is taken from cPickle's |
| 145 | * load_build function. |
| 146 | */ |
| 147 | static PyObject * |
| 148 | BaseException_setstate(PyObject *self, PyObject *state) |
| 149 | { |
| 150 | PyObject *d_key, *d_value; |
| 151 | Py_ssize_t i = 0; |
| 152 | |
| 153 | if (state != Py_None) { |
| 154 | if (!PyDict_Check(state)) { |
| 155 | PyErr_SetString(PyExc_TypeError, "state is not a dictionary"); |
| 156 | return NULL; |
| 157 | } |
| 158 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 159 | if (PyObject_SetAttr(self, d_key, d_value) < 0) |
| 160 | return NULL; |
| 161 | } |
| 162 | } |
| 163 | Py_RETURN_NONE; |
| 164 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 165 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 166 | static PyObject * |
| 167 | BaseException_with_traceback(PyObject *self, PyObject *tb) { |
| 168 | if (PyException_SetTraceback(self, tb)) |
| 169 | return NULL; |
| 170 | |
| 171 | Py_INCREF(self); |
| 172 | return self; |
| 173 | } |
| 174 | |
Georg Brandl | 7694100 | 2008-05-05 21:38:47 +0000 | [diff] [blame] | 175 | PyDoc_STRVAR(with_traceback_doc, |
| 176 | "Exception.with_traceback(tb) --\n\ |
| 177 | set self.__traceback__ to tb and return self."); |
| 178 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 179 | |
| 180 | static PyMethodDef BaseException_methods[] = { |
| 181 | {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS }, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 182 | {"__setstate__", (PyCFunction)BaseException_setstate, METH_O }, |
Georg Brandl | 7694100 | 2008-05-05 21:38:47 +0000 | [diff] [blame] | 183 | {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O, |
| 184 | with_traceback_doc}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 185 | {NULL, NULL, 0, NULL}, |
| 186 | }; |
| 187 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 188 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 189 | BaseException_get_args(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 190 | { |
| 191 | if (self->args == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 192 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 193 | } |
| 194 | Py_INCREF(self->args); |
| 195 | return self->args; |
| 196 | } |
| 197 | |
| 198 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 199 | BaseException_set_args(PyBaseExceptionObject *self, PyObject *val, void *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 200 | { |
| 201 | PyObject *seq; |
| 202 | if (val == NULL) { |
| 203 | PyErr_SetString(PyExc_TypeError, "args may not be deleted"); |
| 204 | return -1; |
| 205 | } |
| 206 | seq = PySequence_Tuple(val); |
Benjamin Peterson | 90b1358 | 2012-02-03 19:22:31 -0500 | [diff] [blame] | 207 | if (!seq) |
| 208 | return -1; |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 209 | Py_XSETREF(self->args, seq); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 213 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 214 | BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 215 | { |
| 216 | if (self->traceback == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 217 | Py_RETURN_NONE; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 218 | } |
| 219 | Py_INCREF(self->traceback); |
| 220 | return self->traceback; |
| 221 | } |
| 222 | |
| 223 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 224 | BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(ignored)) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 225 | { |
| 226 | if (tb == NULL) { |
| 227 | PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted"); |
| 228 | return -1; |
| 229 | } |
| 230 | else if (!(tb == Py_None || PyTraceBack_Check(tb))) { |
| 231 | PyErr_SetString(PyExc_TypeError, |
| 232 | "__traceback__ must be a traceback or None"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
Serhiy Storchaka | 3766572 | 2016-08-20 21:22:03 +0300 | [diff] [blame] | 236 | Py_INCREF(tb); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 237 | Py_XSETREF(self->traceback, tb); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 241 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 242 | BaseException_get_context(PyObject *self, void *Py_UNUSED(ignored)) |
| 243 | { |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 244 | PyObject *res = PyException_GetContext(self); |
Benjamin Peterson | 90b1358 | 2012-02-03 19:22:31 -0500 | [diff] [blame] | 245 | if (res) |
| 246 | return res; /* new reference already returned above */ |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 247 | Py_RETURN_NONE; |
| 248 | } |
| 249 | |
| 250 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 251 | BaseException_set_context(PyObject *self, PyObject *arg, void *Py_UNUSED(ignored)) |
| 252 | { |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 253 | if (arg == NULL) { |
| 254 | PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted"); |
| 255 | return -1; |
| 256 | } else if (arg == Py_None) { |
| 257 | arg = NULL; |
| 258 | } else if (!PyExceptionInstance_Check(arg)) { |
| 259 | PyErr_SetString(PyExc_TypeError, "exception context must be None " |
| 260 | "or derive from BaseException"); |
| 261 | return -1; |
| 262 | } else { |
| 263 | /* PyException_SetContext steals this reference */ |
| 264 | Py_INCREF(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | } |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 266 | PyException_SetContext(self, arg); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 271 | BaseException_get_cause(PyObject *self, void *Py_UNUSED(ignored)) |
| 272 | { |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 273 | PyObject *res = PyException_GetCause(self); |
Benjamin Peterson | 90b1358 | 2012-02-03 19:22:31 -0500 | [diff] [blame] | 274 | if (res) |
| 275 | return res; /* new reference already returned above */ |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 276 | Py_RETURN_NONE; |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 280 | BaseException_set_cause(PyObject *self, PyObject *arg, void *Py_UNUSED(ignored)) |
| 281 | { |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 282 | if (arg == NULL) { |
| 283 | PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted"); |
| 284 | return -1; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 285 | } else if (arg == Py_None) { |
| 286 | arg = NULL; |
| 287 | } else if (!PyExceptionInstance_Check(arg)) { |
| 288 | PyErr_SetString(PyExc_TypeError, "exception cause must be None " |
| 289 | "or derive from BaseException"); |
| 290 | return -1; |
| 291 | } else { |
| 292 | /* PyException_SetCause steals this reference */ |
| 293 | Py_INCREF(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 295 | PyException_SetCause(self, arg); |
| 296 | return 0; |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 299 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 300 | static PyGetSetDef BaseException_getset[] = { |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 301 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 302 | {"args", (getter)BaseException_get_args, (setter)BaseException_set_args}, |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 303 | {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb}, |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 304 | {"__context__", BaseException_get_context, |
| 305 | BaseException_set_context, PyDoc_STR("exception context")}, |
| 306 | {"__cause__", BaseException_get_cause, |
| 307 | BaseException_set_cause, PyDoc_STR("exception cause")}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 308 | {NULL}, |
| 309 | }; |
| 310 | |
| 311 | |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 312 | static inline PyBaseExceptionObject* |
| 313 | _PyBaseExceptionObject_cast(PyObject *exc) |
| 314 | { |
| 315 | assert(PyExceptionInstance_Check(exc)); |
| 316 | return (PyBaseExceptionObject *)exc; |
| 317 | } |
| 318 | |
| 319 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 320 | PyObject * |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 321 | PyException_GetTraceback(PyObject *self) |
| 322 | { |
| 323 | PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 324 | Py_XINCREF(base_self->traceback); |
| 325 | return base_self->traceback; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | int |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 330 | PyException_SetTraceback(PyObject *self, PyObject *tb) |
| 331 | { |
| 332 | return BaseException_set_tb(_PyBaseExceptionObject_cast(self), tb, NULL); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | PyObject * |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 336 | PyException_GetCause(PyObject *self) |
| 337 | { |
| 338 | PyObject *cause = _PyBaseExceptionObject_cast(self)->cause; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 339 | Py_XINCREF(cause); |
| 340 | return cause; |
| 341 | } |
| 342 | |
| 343 | /* Steals a reference to cause */ |
| 344 | void |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 345 | PyException_SetCause(PyObject *self, PyObject *cause) |
| 346 | { |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 347 | PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self); |
| 348 | base_self->suppress_context = 1; |
| 349 | Py_XSETREF(base_self->cause, cause); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | PyObject * |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 353 | PyException_GetContext(PyObject *self) |
| 354 | { |
| 355 | PyObject *context = _PyBaseExceptionObject_cast(self)->context; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 356 | Py_XINCREF(context); |
| 357 | return context; |
| 358 | } |
| 359 | |
| 360 | /* Steals a reference to context */ |
| 361 | void |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 362 | PyException_SetContext(PyObject *self, PyObject *context) |
| 363 | { |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 364 | Py_XSETREF(_PyBaseExceptionObject_cast(self)->context, context); |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Christian Tismer | ea62ce7 | 2018-06-09 20:32:25 +0200 | [diff] [blame] | 367 | #undef PyExceptionClass_Name |
Christian Tismer | 8398713 | 2018-06-11 00:48:28 +0200 | [diff] [blame] | 368 | |
Serhiy Storchaka | ceeef10 | 2018-06-15 11:09:43 +0300 | [diff] [blame] | 369 | const char * |
Christian Tismer | ea62ce7 | 2018-06-09 20:32:25 +0200 | [diff] [blame] | 370 | PyExceptionClass_Name(PyObject *ob) |
| 371 | { |
Victor Stinner | b0be6b3 | 2020-05-05 17:07:41 +0200 | [diff] [blame] | 372 | assert(PyExceptionClass_Check(ob)); |
Serhiy Storchaka | ceeef10 | 2018-06-15 11:09:43 +0300 | [diff] [blame] | 373 | return ((PyTypeObject*)ob)->tp_name; |
Christian Tismer | ea62ce7 | 2018-06-09 20:32:25 +0200 | [diff] [blame] | 374 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 375 | |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 376 | static struct PyMemberDef BaseException_members[] = { |
| 377 | {"__suppress_context__", T_BOOL, |
Antoine Pitrou | 32bc80c | 2012-05-16 12:51:55 +0200 | [diff] [blame] | 378 | offsetof(PyBaseExceptionObject, suppress_context)}, |
| 379 | {NULL} |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 383 | static PyTypeObject _PyExc_BaseException = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 384 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 385 | "BaseException", /*tp_name*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 386 | sizeof(PyBaseExceptionObject), /*tp_basicsize*/ |
| 387 | 0, /*tp_itemsize*/ |
| 388 | (destructor)BaseException_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 389 | 0, /*tp_vectorcall_offset*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 390 | 0, /*tp_getattr*/ |
| 391 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 392 | 0, /*tp_as_async*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 393 | (reprfunc)BaseException_repr, /*tp_repr*/ |
| 394 | 0, /*tp_as_number*/ |
Brett Cannon | ba7bf49 | 2007-02-27 00:15:55 +0000 | [diff] [blame] | 395 | 0, /*tp_as_sequence*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 396 | 0, /*tp_as_mapping*/ |
| 397 | 0, /*tp_hash */ |
| 398 | 0, /*tp_call*/ |
| 399 | (reprfunc)BaseException_str, /*tp_str*/ |
| 400 | PyObject_GenericGetAttr, /*tp_getattro*/ |
| 401 | PyObject_GenericSetAttr, /*tp_setattro*/ |
| 402 | 0, /*tp_as_buffer*/ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 403 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 405 | PyDoc_STR("Common base class for all exceptions"), /* tp_doc */ |
| 406 | (traverseproc)BaseException_traverse, /* tp_traverse */ |
| 407 | (inquiry)BaseException_clear, /* tp_clear */ |
| 408 | 0, /* tp_richcompare */ |
| 409 | 0, /* tp_weaklistoffset */ |
| 410 | 0, /* tp_iter */ |
| 411 | 0, /* tp_iternext */ |
| 412 | BaseException_methods, /* tp_methods */ |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 413 | BaseException_members, /* tp_members */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 414 | BaseException_getset, /* tp_getset */ |
| 415 | 0, /* tp_base */ |
| 416 | 0, /* tp_dict */ |
| 417 | 0, /* tp_descr_get */ |
| 418 | 0, /* tp_descr_set */ |
| 419 | offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */ |
| 420 | (initproc)BaseException_init, /* tp_init */ |
| 421 | 0, /* tp_alloc */ |
| 422 | BaseException_new, /* tp_new */ |
| 423 | }; |
| 424 | /* the CPython API expects exceptions to be (PyObject *) - both a hold-over |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 425 | from the previous implementation and also allowing Python objects to be used |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 426 | in the API */ |
| 427 | PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException; |
| 428 | |
| 429 | /* note these macros omit the last semicolon so the macro invocation may |
| 430 | * include it and not look strange. |
| 431 | */ |
| 432 | #define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \ |
| 433 | static PyTypeObject _PyExc_ ## EXCNAME = { \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 434 | PyVarObject_HEAD_INIT(NULL, 0) \ |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 435 | # EXCNAME, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 436 | sizeof(PyBaseExceptionObject), \ |
| 437 | 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \ |
| 438 | 0, 0, 0, 0, 0, 0, 0, \ |
| 439 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ |
| 440 | PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \ |
| 441 | (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ |
| 442 | 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \ |
| 443 | (initproc)BaseException_init, 0, BaseException_new,\ |
| 444 | }; \ |
| 445 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME |
| 446 | |
| 447 | #define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \ |
| 448 | static PyTypeObject _PyExc_ ## EXCNAME = { \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 449 | PyVarObject_HEAD_INIT(NULL, 0) \ |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 450 | # EXCNAME, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 451 | sizeof(Py ## EXCSTORE ## Object), \ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 452 | 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 453 | 0, 0, 0, 0, 0, \ |
| 454 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 455 | PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ |
| 456 | (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 457 | 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 458 | (initproc)EXCSTORE ## _init, 0, 0, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 459 | }; \ |
| 460 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME |
| 461 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 462 | #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \ |
| 463 | EXCMETHODS, EXCMEMBERS, EXCGETSET, \ |
| 464 | EXCSTR, EXCDOC) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 465 | static PyTypeObject _PyExc_ ## EXCNAME = { \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 466 | PyVarObject_HEAD_INIT(NULL, 0) \ |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 467 | # EXCNAME, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 468 | sizeof(Py ## EXCSTORE ## Object), 0, \ |
| 469 | (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 470 | (reprfunc)EXCSTR, 0, 0, 0, \ |
| 471 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ |
| 472 | PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ |
| 473 | (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 474 | EXCMEMBERS, EXCGETSET, &_ ## EXCBASE, \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 475 | 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 476 | (initproc)EXCSTORE ## _init, 0, EXCNEW,\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 477 | }; \ |
| 478 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME |
| 479 | |
| 480 | |
| 481 | /* |
| 482 | * Exception extends BaseException |
| 483 | */ |
| 484 | SimpleExtendsException(PyExc_BaseException, Exception, |
| 485 | "Common base class for all non-exit exceptions."); |
| 486 | |
| 487 | |
| 488 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 489 | * TypeError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 490 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 491 | SimpleExtendsException(PyExc_Exception, TypeError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 492 | "Inappropriate argument type."); |
| 493 | |
| 494 | |
| 495 | /* |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 496 | * StopAsyncIteration extends Exception |
| 497 | */ |
| 498 | SimpleExtendsException(PyExc_Exception, StopAsyncIteration, |
| 499 | "Signal the end from iterator.__anext__()."); |
| 500 | |
| 501 | |
| 502 | /* |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 503 | * StopIteration extends Exception |
| 504 | */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 505 | |
| 506 | static PyMemberDef StopIteration_members[] = { |
| 507 | {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0, |
| 508 | PyDoc_STR("generator return value")}, |
| 509 | {NULL} /* Sentinel */ |
| 510 | }; |
| 511 | |
| 512 | static int |
| 513 | StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds) |
| 514 | { |
| 515 | Py_ssize_t size = PyTuple_GET_SIZE(args); |
| 516 | PyObject *value; |
| 517 | |
| 518 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 519 | return -1; |
| 520 | Py_CLEAR(self->value); |
| 521 | if (size > 0) |
| 522 | value = PyTuple_GET_ITEM(args, 0); |
| 523 | else |
| 524 | value = Py_None; |
| 525 | Py_INCREF(value); |
| 526 | self->value = value; |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static int |
| 531 | StopIteration_clear(PyStopIterationObject *self) |
| 532 | { |
| 533 | Py_CLEAR(self->value); |
| 534 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | StopIteration_dealloc(PyStopIterationObject *self) |
| 539 | { |
| 540 | _PyObject_GC_UNTRACK(self); |
| 541 | StopIteration_clear(self); |
| 542 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 543 | } |
| 544 | |
| 545 | static int |
| 546 | StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg) |
| 547 | { |
| 548 | Py_VISIT(self->value); |
| 549 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 550 | } |
| 551 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 552 | ComplexExtendsException( |
| 553 | PyExc_Exception, /* base */ |
| 554 | StopIteration, /* name */ |
| 555 | StopIteration, /* prefix for *_init, etc */ |
| 556 | 0, /* new */ |
| 557 | 0, /* methods */ |
| 558 | StopIteration_members, /* members */ |
| 559 | 0, /* getset */ |
| 560 | 0, /* str */ |
| 561 | "Signal the end from iterator.__next__()." |
| 562 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 563 | |
| 564 | |
| 565 | /* |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 566 | * GeneratorExit extends BaseException |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 567 | */ |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 568 | SimpleExtendsException(PyExc_BaseException, GeneratorExit, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 569 | "Request that a generator exit."); |
| 570 | |
| 571 | |
| 572 | /* |
| 573 | * SystemExit extends BaseException |
| 574 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 575 | |
| 576 | static int |
| 577 | SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds) |
| 578 | { |
| 579 | Py_ssize_t size = PyTuple_GET_SIZE(args); |
| 580 | |
| 581 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 582 | return -1; |
| 583 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 584 | if (size == 0) |
| 585 | return 0; |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 586 | if (size == 1) { |
| 587 | Py_INCREF(PyTuple_GET_ITEM(args, 0)); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 588 | Py_XSETREF(self->code, PyTuple_GET_ITEM(args, 0)); |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 589 | } |
| 590 | else { /* size > 1 */ |
| 591 | Py_INCREF(args); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 592 | Py_XSETREF(self->code, args); |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 593 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 594 | return 0; |
| 595 | } |
| 596 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 597 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 598 | SystemExit_clear(PySystemExitObject *self) |
| 599 | { |
| 600 | Py_CLEAR(self->code); |
| 601 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 602 | } |
| 603 | |
| 604 | static void |
| 605 | SystemExit_dealloc(PySystemExitObject *self) |
| 606 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 607 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 608 | SystemExit_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 609 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 612 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 613 | SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg) |
| 614 | { |
| 615 | Py_VISIT(self->code); |
| 616 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 617 | } |
| 618 | |
| 619 | static PyMemberDef SystemExit_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 620 | {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0, |
| 621 | PyDoc_STR("exception code")}, |
| 622 | {NULL} /* Sentinel */ |
| 623 | }; |
| 624 | |
| 625 | ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit, |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 626 | 0, 0, SystemExit_members, 0, 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 627 | "Request to exit from the interpreter."); |
| 628 | |
| 629 | /* |
| 630 | * KeyboardInterrupt extends BaseException |
| 631 | */ |
| 632 | SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, |
| 633 | "Program interrupted by user."); |
| 634 | |
| 635 | |
| 636 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 637 | * ImportError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 638 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 639 | |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 640 | static int |
| 641 | ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) |
| 642 | { |
Serhiy Storchaka | 47dee11 | 2016-09-27 20:45:35 +0300 | [diff] [blame] | 643 | static char *kwlist[] = {"name", "path", 0}; |
| 644 | PyObject *empty_tuple; |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 645 | PyObject *msg = NULL; |
| 646 | PyObject *name = NULL; |
| 647 | PyObject *path = NULL; |
| 648 | |
Serhiy Storchaka | 47dee11 | 2016-09-27 20:45:35 +0300 | [diff] [blame] | 649 | if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 650 | return -1; |
| 651 | |
Serhiy Storchaka | 47dee11 | 2016-09-27 20:45:35 +0300 | [diff] [blame] | 652 | empty_tuple = PyTuple_New(0); |
| 653 | if (!empty_tuple) |
| 654 | return -1; |
| 655 | if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist, |
| 656 | &name, &path)) { |
| 657 | Py_DECREF(empty_tuple); |
| 658 | return -1; |
| 659 | } |
| 660 | Py_DECREF(empty_tuple); |
| 661 | |
Serhiy Storchaka | e9e4448 | 2016-09-28 07:53:32 +0300 | [diff] [blame] | 662 | Py_XINCREF(name); |
| 663 | Py_XSETREF(self->name, name); |
| 664 | |
| 665 | Py_XINCREF(path); |
| 666 | Py_XSETREF(self->path, path); |
| 667 | |
Serhiy Storchaka | 47dee11 | 2016-09-27 20:45:35 +0300 | [diff] [blame] | 668 | if (PyTuple_GET_SIZE(args) == 1) { |
| 669 | msg = PyTuple_GET_ITEM(args, 0); |
| 670 | Py_INCREF(msg); |
Serhiy Storchaka | 47dee11 | 2016-09-27 20:45:35 +0300 | [diff] [blame] | 671 | } |
Serhiy Storchaka | e9e4448 | 2016-09-28 07:53:32 +0300 | [diff] [blame] | 672 | Py_XSETREF(self->msg, msg); |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int |
| 678 | ImportError_clear(PyImportErrorObject *self) |
| 679 | { |
| 680 | Py_CLEAR(self->msg); |
| 681 | Py_CLEAR(self->name); |
| 682 | Py_CLEAR(self->path); |
| 683 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 684 | } |
| 685 | |
| 686 | static void |
| 687 | ImportError_dealloc(PyImportErrorObject *self) |
| 688 | { |
| 689 | _PyObject_GC_UNTRACK(self); |
| 690 | ImportError_clear(self); |
| 691 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 692 | } |
| 693 | |
| 694 | static int |
| 695 | ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg) |
| 696 | { |
| 697 | Py_VISIT(self->msg); |
| 698 | Py_VISIT(self->name); |
| 699 | Py_VISIT(self->path); |
| 700 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 701 | } |
| 702 | |
| 703 | static PyObject * |
| 704 | ImportError_str(PyImportErrorObject *self) |
| 705 | { |
Brett Cannon | 07c6e71 | 2012-08-24 13:05:09 -0400 | [diff] [blame] | 706 | if (self->msg && PyUnicode_CheckExact(self->msg)) { |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 707 | Py_INCREF(self->msg); |
| 708 | return self->msg; |
| 709 | } |
| 710 | else { |
| 711 | return BaseException_str((PyBaseExceptionObject *)self); |
| 712 | } |
| 713 | } |
| 714 | |
Serhiy Storchaka | b785396 | 2017-04-08 09:55:07 +0300 | [diff] [blame] | 715 | static PyObject * |
| 716 | ImportError_getstate(PyImportErrorObject *self) |
| 717 | { |
| 718 | PyObject *dict = ((PyBaseExceptionObject *)self)->dict; |
| 719 | if (self->name || self->path) { |
| 720 | _Py_IDENTIFIER(name); |
| 721 | _Py_IDENTIFIER(path); |
| 722 | dict = dict ? PyDict_Copy(dict) : PyDict_New(); |
| 723 | if (dict == NULL) |
| 724 | return NULL; |
| 725 | if (self->name && _PyDict_SetItemId(dict, &PyId_name, self->name) < 0) { |
| 726 | Py_DECREF(dict); |
| 727 | return NULL; |
| 728 | } |
| 729 | if (self->path && _PyDict_SetItemId(dict, &PyId_path, self->path) < 0) { |
| 730 | Py_DECREF(dict); |
| 731 | return NULL; |
| 732 | } |
| 733 | return dict; |
| 734 | } |
| 735 | else if (dict) { |
| 736 | Py_INCREF(dict); |
| 737 | return dict; |
| 738 | } |
| 739 | else { |
| 740 | Py_RETURN_NONE; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | /* Pickling support */ |
| 745 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 746 | ImportError_reduce(PyImportErrorObject *self, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | b785396 | 2017-04-08 09:55:07 +0300 | [diff] [blame] | 747 | { |
| 748 | PyObject *res; |
| 749 | PyObject *args; |
| 750 | PyObject *state = ImportError_getstate(self); |
| 751 | if (state == NULL) |
| 752 | return NULL; |
| 753 | args = ((PyBaseExceptionObject *)self)->args; |
| 754 | if (state == Py_None) |
| 755 | res = PyTuple_Pack(2, Py_TYPE(self), args); |
| 756 | else |
| 757 | res = PyTuple_Pack(3, Py_TYPE(self), args, state); |
| 758 | Py_DECREF(state); |
| 759 | return res; |
| 760 | } |
| 761 | |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 762 | static PyMemberDef ImportError_members[] = { |
| 763 | {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0, |
| 764 | PyDoc_STR("exception message")}, |
| 765 | {"name", T_OBJECT, offsetof(PyImportErrorObject, name), 0, |
| 766 | PyDoc_STR("module name")}, |
| 767 | {"path", T_OBJECT, offsetof(PyImportErrorObject, path), 0, |
| 768 | PyDoc_STR("module path")}, |
| 769 | {NULL} /* Sentinel */ |
| 770 | }; |
| 771 | |
| 772 | static PyMethodDef ImportError_methods[] = { |
Serhiy Storchaka | b785396 | 2017-04-08 09:55:07 +0300 | [diff] [blame] | 773 | {"__reduce__", (PyCFunction)ImportError_reduce, METH_NOARGS}, |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 774 | {NULL} |
| 775 | }; |
| 776 | |
| 777 | ComplexExtendsException(PyExc_Exception, ImportError, |
| 778 | ImportError, 0 /* new */, |
| 779 | ImportError_methods, ImportError_members, |
| 780 | 0 /* getset */, ImportError_str, |
| 781 | "Import can't find module, or can't find name in " |
| 782 | "module."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 783 | |
| 784 | /* |
Eric Snow | c943265 | 2016-09-07 15:42:32 -0700 | [diff] [blame] | 785 | * ModuleNotFoundError extends ImportError |
| 786 | */ |
| 787 | |
| 788 | MiddlingExtendsException(PyExc_ImportError, ModuleNotFoundError, ImportError, |
| 789 | "Module not found."); |
| 790 | |
| 791 | /* |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 792 | * OSError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 793 | */ |
| 794 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 795 | #ifdef MS_WINDOWS |
| 796 | #include "errmap.h" |
| 797 | #endif |
| 798 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 799 | /* Where a function has a single filename, such as open() or some |
| 800 | * of the os module functions, PyErr_SetFromErrnoWithFilename() is |
| 801 | * called, giving a third argument which is the filename. But, so |
| 802 | * that old code using in-place unpacking doesn't break, e.g.: |
| 803 | * |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 804 | * except OSError, (errno, strerror): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 805 | * |
| 806 | * we hack args so that it only contains two items. This also |
| 807 | * means we need our own __str__() which prints out the filename |
| 808 | * when it was supplied. |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 809 | * |
| 810 | * (If a function has two filenames, such as rename(), symlink(), |
Larry Hastings | 8f9f0f1 | 2014-02-10 03:43:57 -0800 | [diff] [blame] | 811 | * or copy(), PyErr_SetFromErrnoWithFilenameObjects() is called, |
| 812 | * which allows passing in a second filename.) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 813 | */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 814 | |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 815 | /* This function doesn't cleanup on error, the caller should */ |
| 816 | static int |
| 817 | oserror_parse_args(PyObject **p_args, |
| 818 | PyObject **myerrno, PyObject **strerror, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 819 | PyObject **filename, PyObject **filename2 |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 820 | #ifdef MS_WINDOWS |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 821 | , PyObject **winerror |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 822 | #endif |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 823 | ) |
| 824 | { |
| 825 | Py_ssize_t nargs; |
| 826 | PyObject *args = *p_args; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 827 | #ifndef MS_WINDOWS |
| 828 | /* |
| 829 | * ignored on non-Windows platforms, |
| 830 | * but parsed so OSError has a consistent signature |
| 831 | */ |
| 832 | PyObject *_winerror = NULL; |
| 833 | PyObject **winerror = &_winerror; |
| 834 | #endif /* MS_WINDOWS */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 836 | nargs = PyTuple_GET_SIZE(args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 837 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 838 | if (nargs >= 2 && nargs <= 5) { |
| 839 | if (!PyArg_UnpackTuple(args, "OSError", 2, 5, |
| 840 | myerrno, strerror, |
| 841 | filename, winerror, filename2)) |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 842 | return -1; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 843 | #ifdef MS_WINDOWS |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 844 | if (*winerror && PyLong_Check(*winerror)) { |
| 845 | long errcode, winerrcode; |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 846 | PyObject *newargs; |
| 847 | Py_ssize_t i; |
| 848 | |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 849 | winerrcode = PyLong_AsLong(*winerror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 850 | if (winerrcode == -1 && PyErr_Occurred()) |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 851 | return -1; |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 852 | /* Set errno to the corresponding POSIX errno (overriding |
| 853 | first argument). Windows Socket error codes (>= 10000) |
| 854 | have the same value as their POSIX counterparts. |
| 855 | */ |
| 856 | if (winerrcode < 10000) |
| 857 | errcode = winerror_to_errno(winerrcode); |
| 858 | else |
| 859 | errcode = winerrcode; |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 860 | *myerrno = PyLong_FromLong(errcode); |
| 861 | if (!*myerrno) |
| 862 | return -1; |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 863 | newargs = PyTuple_New(nargs); |
| 864 | if (!newargs) |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 865 | return -1; |
| 866 | PyTuple_SET_ITEM(newargs, 0, *myerrno); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 867 | for (i = 1; i < nargs; i++) { |
| 868 | PyObject *val = PyTuple_GET_ITEM(args, i); |
| 869 | Py_INCREF(val); |
| 870 | PyTuple_SET_ITEM(newargs, i, val); |
| 871 | } |
| 872 | Py_DECREF(args); |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 873 | args = *p_args = newargs; |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 874 | } |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 875 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 876 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 878 | return 0; |
| 879 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 880 | |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 881 | static int |
| 882 | oserror_init(PyOSErrorObject *self, PyObject **p_args, |
| 883 | PyObject *myerrno, PyObject *strerror, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 884 | PyObject *filename, PyObject *filename2 |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 885 | #ifdef MS_WINDOWS |
| 886 | , PyObject *winerror |
| 887 | #endif |
| 888 | ) |
| 889 | { |
| 890 | PyObject *args = *p_args; |
| 891 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 892 | |
| 893 | /* self->filename will remain Py_None otherwise */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 894 | if (filename && filename != Py_None) { |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 895 | if (Py_IS_TYPE(self, (PyTypeObject *) PyExc_BlockingIOError) && |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 896 | PyNumber_Check(filename)) { |
| 897 | /* BlockingIOError's 3rd argument can be the number of |
| 898 | * characters written. |
| 899 | */ |
| 900 | self->written = PyNumber_AsSsize_t(filename, PyExc_ValueError); |
| 901 | if (self->written == -1 && PyErr_Occurred()) |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 902 | return -1; |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 903 | } |
| 904 | else { |
| 905 | Py_INCREF(filename); |
| 906 | self->filename = filename; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 907 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 908 | if (filename2 && filename2 != Py_None) { |
| 909 | Py_INCREF(filename2); |
| 910 | self->filename2 = filename2; |
| 911 | } |
| 912 | |
| 913 | if (nargs >= 2 && nargs <= 5) { |
| 914 | /* filename, filename2, and winerror are removed from the args tuple |
| 915 | (for compatibility purposes, see test_exceptions.py) */ |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 916 | PyObject *subslice = PyTuple_GetSlice(args, 0, 2); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 917 | if (!subslice) |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 918 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 920 | Py_DECREF(args); /* replacing args */ |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 921 | *p_args = args = subslice; |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 922 | } |
| 923 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 924 | } |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 925 | Py_XINCREF(myerrno); |
| 926 | self->myerrno = myerrno; |
| 927 | |
| 928 | Py_XINCREF(strerror); |
| 929 | self->strerror = strerror; |
| 930 | |
| 931 | #ifdef MS_WINDOWS |
| 932 | Py_XINCREF(winerror); |
| 933 | self->winerror = winerror; |
| 934 | #endif |
| 935 | |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 936 | /* Steals the reference to args */ |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 937 | Py_XSETREF(self->args, args); |
Victor Stinner | 46ef319 | 2013-11-14 22:31:41 +0100 | [diff] [blame] | 938 | *p_args = args = NULL; |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | static PyObject * |
| 944 | OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 945 | static int |
| 946 | OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds); |
| 947 | |
| 948 | static int |
| 949 | oserror_use_init(PyTypeObject *type) |
| 950 | { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 951 | /* When __init__ is defined in an OSError subclass, we want any |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 952 | extraneous argument to __new__ to be ignored. The only reasonable |
| 953 | solution, given __new__ takes a variable number of arguments, |
| 954 | is to defer arg parsing and initialization to __init__. |
| 955 | |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 956 | But when __new__ is overridden as well, it should call our __new__ |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 957 | with the right arguments. |
| 958 | |
| 959 | (see http://bugs.python.org/issue12555#msg148829 ) |
| 960 | */ |
| 961 | if (type->tp_init != (initproc) OSError_init && |
| 962 | type->tp_new == (newfunc) OSError_new) { |
| 963 | assert((PyObject *) type != PyExc_OSError); |
| 964 | return 1; |
| 965 | } |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static PyObject * |
| 970 | OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 971 | { |
| 972 | PyOSErrorObject *self = NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 973 | PyObject *myerrno = NULL, *strerror = NULL; |
| 974 | PyObject *filename = NULL, *filename2 = NULL; |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 975 | #ifdef MS_WINDOWS |
| 976 | PyObject *winerror = NULL; |
| 977 | #endif |
| 978 | |
Victor Stinner | 46ef319 | 2013-11-14 22:31:41 +0100 | [diff] [blame] | 979 | Py_INCREF(args); |
| 980 | |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 981 | if (!oserror_use_init(type)) { |
| 982 | if (!_PyArg_NoKeywords(type->tp_name, kwds)) |
Victor Stinner | 46ef319 | 2013-11-14 22:31:41 +0100 | [diff] [blame] | 983 | goto error; |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 984 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 985 | if (oserror_parse_args(&args, &myerrno, &strerror, |
| 986 | &filename, &filename2 |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 987 | #ifdef MS_WINDOWS |
| 988 | , &winerror |
| 989 | #endif |
| 990 | )) |
| 991 | goto error; |
| 992 | |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 993 | struct _Py_exc_state *state = get_exc_state(); |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 994 | if (myerrno && PyLong_Check(myerrno) && |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 995 | state->errnomap && (PyObject *) type == PyExc_OSError) { |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 996 | PyObject *newtype; |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 997 | newtype = PyDict_GetItemWithError(state->errnomap, myerrno); |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 998 | if (newtype) { |
| 999 | assert(PyType_Check(newtype)); |
| 1000 | type = (PyTypeObject *) newtype; |
| 1001 | } |
| 1002 | else if (PyErr_Occurred()) |
| 1003 | goto error; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | self = (PyOSErrorObject *) type->tp_alloc(type, 0); |
| 1008 | if (!self) |
| 1009 | goto error; |
| 1010 | |
| 1011 | self->dict = NULL; |
| 1012 | self->traceback = self->cause = self->context = NULL; |
| 1013 | self->written = -1; |
| 1014 | |
| 1015 | if (!oserror_use_init(type)) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1016 | if (oserror_init(self, &args, myerrno, strerror, filename, filename2 |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1017 | #ifdef MS_WINDOWS |
| 1018 | , winerror |
| 1019 | #endif |
| 1020 | )) |
| 1021 | goto error; |
| 1022 | } |
Antoine Pitrou | f87289b | 2012-06-30 23:37:47 +0200 | [diff] [blame] | 1023 | else { |
| 1024 | self->args = PyTuple_New(0); |
| 1025 | if (self->args == NULL) |
| 1026 | goto error; |
| 1027 | } |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1028 | |
Victor Stinner | 46ef319 | 2013-11-14 22:31:41 +0100 | [diff] [blame] | 1029 | Py_XDECREF(args); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1030 | return (PyObject *) self; |
| 1031 | |
| 1032 | error: |
| 1033 | Py_XDECREF(args); |
| 1034 | Py_XDECREF(self); |
| 1035 | return NULL; |
| 1036 | } |
| 1037 | |
| 1038 | static int |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1039 | OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1040 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1041 | PyObject *myerrno = NULL, *strerror = NULL; |
| 1042 | PyObject *filename = NULL, *filename2 = NULL; |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1043 | #ifdef MS_WINDOWS |
| 1044 | PyObject *winerror = NULL; |
| 1045 | #endif |
| 1046 | |
| 1047 | if (!oserror_use_init(Py_TYPE(self))) |
| 1048 | /* Everything already done in OSError_new */ |
| 1049 | return 0; |
| 1050 | |
| 1051 | if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) |
| 1052 | return -1; |
| 1053 | |
| 1054 | Py_INCREF(args); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1055 | if (oserror_parse_args(&args, &myerrno, &strerror, &filename, &filename2 |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1056 | #ifdef MS_WINDOWS |
| 1057 | , &winerror |
| 1058 | #endif |
| 1059 | )) |
| 1060 | goto error; |
| 1061 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1062 | if (oserror_init(self, &args, myerrno, strerror, filename, filename2 |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1063 | #ifdef MS_WINDOWS |
| 1064 | , winerror |
| 1065 | #endif |
| 1066 | )) |
| 1067 | goto error; |
| 1068 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1069 | return 0; |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1070 | |
| 1071 | error: |
Serhiy Storchaka | 3766572 | 2016-08-20 21:22:03 +0300 | [diff] [blame] | 1072 | Py_DECREF(args); |
Antoine Pitrou | e0e2735 | 2011-12-15 14:31:28 +0100 | [diff] [blame] | 1073 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1076 | static int |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1077 | OSError_clear(PyOSErrorObject *self) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1078 | { |
| 1079 | Py_CLEAR(self->myerrno); |
| 1080 | Py_CLEAR(self->strerror); |
| 1081 | Py_CLEAR(self->filename); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1082 | Py_CLEAR(self->filename2); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1083 | #ifdef MS_WINDOWS |
| 1084 | Py_CLEAR(self->winerror); |
| 1085 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1086 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 1087 | } |
| 1088 | |
| 1089 | static void |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1090 | OSError_dealloc(PyOSErrorObject *self) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1091 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1092 | _PyObject_GC_UNTRACK(self); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1093 | OSError_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1094 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1097 | static int |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1098 | OSError_traverse(PyOSErrorObject *self, visitproc visit, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1099 | void *arg) |
| 1100 | { |
| 1101 | Py_VISIT(self->myerrno); |
| 1102 | Py_VISIT(self->strerror); |
| 1103 | Py_VISIT(self->filename); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1104 | Py_VISIT(self->filename2); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1105 | #ifdef MS_WINDOWS |
| 1106 | Py_VISIT(self->winerror); |
| 1107 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1108 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 1109 | } |
| 1110 | |
| 1111 | static PyObject * |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1112 | OSError_str(PyOSErrorObject *self) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1113 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1114 | #define OR_NONE(x) ((x)?(x):Py_None) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1115 | #ifdef MS_WINDOWS |
| 1116 | /* If available, winerror has the priority over myerrno */ |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1117 | if (self->winerror && self->filename) { |
| 1118 | if (self->filename2) { |
| 1119 | return PyUnicode_FromFormat("[WinError %S] %S: %R -> %R", |
| 1120 | OR_NONE(self->winerror), |
| 1121 | OR_NONE(self->strerror), |
| 1122 | self->filename, |
| 1123 | self->filename2); |
| 1124 | } else { |
| 1125 | return PyUnicode_FromFormat("[WinError %S] %S: %R", |
| 1126 | OR_NONE(self->winerror), |
| 1127 | OR_NONE(self->strerror), |
| 1128 | self->filename); |
| 1129 | } |
| 1130 | } |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1131 | if (self->winerror && self->strerror) |
Richard Oudkerk | 3014771 | 2012-08-28 19:33:26 +0100 | [diff] [blame] | 1132 | return PyUnicode_FromFormat("[WinError %S] %S", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1133 | self->winerror ? self->winerror: Py_None, |
| 1134 | self->strerror ? self->strerror: Py_None); |
| 1135 | #endif |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1136 | if (self->filename) { |
| 1137 | if (self->filename2) { |
| 1138 | return PyUnicode_FromFormat("[Errno %S] %S: %R -> %R", |
| 1139 | OR_NONE(self->myerrno), |
| 1140 | OR_NONE(self->strerror), |
| 1141 | self->filename, |
| 1142 | self->filename2); |
| 1143 | } else { |
| 1144 | return PyUnicode_FromFormat("[Errno %S] %S: %R", |
| 1145 | OR_NONE(self->myerrno), |
| 1146 | OR_NONE(self->strerror), |
| 1147 | self->filename); |
| 1148 | } |
| 1149 | } |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1150 | if (self->myerrno && self->strerror) |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 1151 | return PyUnicode_FromFormat("[Errno %S] %S", |
Serhiy Storchaka | 3766572 | 2016-08-20 21:22:03 +0300 | [diff] [blame] | 1152 | self->myerrno, self->strerror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1153 | return BaseException_str((PyBaseExceptionObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1156 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1157 | OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1158 | { |
| 1159 | PyObject *args = self->args; |
| 1160 | PyObject *res = NULL, *tmp; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1161 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1162 | /* self->args is only the first two real arguments if there was a |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1163 | * file name given to OSError. */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1164 | if (PyTuple_GET_SIZE(args) == 2 && self->filename) { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1165 | Py_ssize_t size = self->filename2 ? 5 : 3; |
| 1166 | args = PyTuple_New(size); |
Benjamin Peterson | 90b1358 | 2012-02-03 19:22:31 -0500 | [diff] [blame] | 1167 | if (!args) |
| 1168 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1169 | |
| 1170 | tmp = PyTuple_GET_ITEM(self->args, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1171 | Py_INCREF(tmp); |
| 1172 | PyTuple_SET_ITEM(args, 0, tmp); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1173 | |
| 1174 | tmp = PyTuple_GET_ITEM(self->args, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1175 | Py_INCREF(tmp); |
| 1176 | PyTuple_SET_ITEM(args, 1, tmp); |
| 1177 | |
| 1178 | Py_INCREF(self->filename); |
| 1179 | PyTuple_SET_ITEM(args, 2, self->filename); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1180 | |
| 1181 | if (self->filename2) { |
| 1182 | /* |
| 1183 | * This tuple is essentially used as OSError(*args). |
| 1184 | * So, to recreate filename2, we need to pass in |
| 1185 | * winerror as well. |
| 1186 | */ |
| 1187 | Py_INCREF(Py_None); |
| 1188 | PyTuple_SET_ITEM(args, 3, Py_None); |
| 1189 | |
| 1190 | /* filename2 */ |
| 1191 | Py_INCREF(self->filename2); |
| 1192 | PyTuple_SET_ITEM(args, 4, self->filename2); |
| 1193 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1194 | } else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1195 | Py_INCREF(args); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (self->dict) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1198 | res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1199 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1200 | res = PyTuple_Pack(2, Py_TYPE(self), args); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1201 | Py_DECREF(args); |
| 1202 | return res; |
| 1203 | } |
| 1204 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1205 | static PyObject * |
| 1206 | OSError_written_get(PyOSErrorObject *self, void *context) |
| 1207 | { |
| 1208 | if (self->written == -1) { |
| 1209 | PyErr_SetString(PyExc_AttributeError, "characters_written"); |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | return PyLong_FromSsize_t(self->written); |
| 1213 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1215 | static int |
| 1216 | OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context) |
| 1217 | { |
Serhiy Storchaka | e2af34f | 2018-12-17 16:43:14 +0200 | [diff] [blame] | 1218 | if (arg == NULL) { |
| 1219 | if (self->written == -1) { |
| 1220 | PyErr_SetString(PyExc_AttributeError, "characters_written"); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | self->written = -1; |
| 1224 | return 0; |
| 1225 | } |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1226 | Py_ssize_t n; |
| 1227 | n = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 1228 | if (n == -1 && PyErr_Occurred()) |
| 1229 | return -1; |
| 1230 | self->written = n; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static PyMemberDef OSError_members[] = { |
| 1235 | {"errno", T_OBJECT, offsetof(PyOSErrorObject, myerrno), 0, |
| 1236 | PyDoc_STR("POSIX exception code")}, |
| 1237 | {"strerror", T_OBJECT, offsetof(PyOSErrorObject, strerror), 0, |
| 1238 | PyDoc_STR("exception strerror")}, |
| 1239 | {"filename", T_OBJECT, offsetof(PyOSErrorObject, filename), 0, |
| 1240 | PyDoc_STR("exception filename")}, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1241 | {"filename2", T_OBJECT, offsetof(PyOSErrorObject, filename2), 0, |
| 1242 | PyDoc_STR("second exception filename")}, |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1243 | #ifdef MS_WINDOWS |
| 1244 | {"winerror", T_OBJECT, offsetof(PyOSErrorObject, winerror), 0, |
| 1245 | PyDoc_STR("Win32 exception code")}, |
| 1246 | #endif |
| 1247 | {NULL} /* Sentinel */ |
| 1248 | }; |
| 1249 | |
| 1250 | static PyMethodDef OSError_methods[] = { |
| 1251 | {"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1252 | {NULL} |
| 1253 | }; |
| 1254 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1255 | static PyGetSetDef OSError_getset[] = { |
| 1256 | {"characters_written", (getter) OSError_written_get, |
| 1257 | (setter) OSError_written_set, NULL}, |
| 1258 | {NULL} |
| 1259 | }; |
| 1260 | |
| 1261 | |
| 1262 | ComplexExtendsException(PyExc_Exception, OSError, |
| 1263 | OSError, OSError_new, |
| 1264 | OSError_methods, OSError_members, OSError_getset, |
| 1265 | OSError_str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1266 | "Base class for I/O related errors."); |
| 1267 | |
| 1268 | |
| 1269 | /* |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1270 | * Various OSError subclasses |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1271 | */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1272 | MiddlingExtendsException(PyExc_OSError, BlockingIOError, OSError, |
| 1273 | "I/O operation would block."); |
| 1274 | MiddlingExtendsException(PyExc_OSError, ConnectionError, OSError, |
| 1275 | "Connection error."); |
| 1276 | MiddlingExtendsException(PyExc_OSError, ChildProcessError, OSError, |
| 1277 | "Child process error."); |
| 1278 | MiddlingExtendsException(PyExc_ConnectionError, BrokenPipeError, OSError, |
| 1279 | "Broken pipe."); |
| 1280 | MiddlingExtendsException(PyExc_ConnectionError, ConnectionAbortedError, OSError, |
| 1281 | "Connection aborted."); |
| 1282 | MiddlingExtendsException(PyExc_ConnectionError, ConnectionRefusedError, OSError, |
| 1283 | "Connection refused."); |
| 1284 | MiddlingExtendsException(PyExc_ConnectionError, ConnectionResetError, OSError, |
| 1285 | "Connection reset."); |
| 1286 | MiddlingExtendsException(PyExc_OSError, FileExistsError, OSError, |
| 1287 | "File already exists."); |
| 1288 | MiddlingExtendsException(PyExc_OSError, FileNotFoundError, OSError, |
| 1289 | "File not found."); |
| 1290 | MiddlingExtendsException(PyExc_OSError, IsADirectoryError, OSError, |
| 1291 | "Operation doesn't work on directories."); |
| 1292 | MiddlingExtendsException(PyExc_OSError, NotADirectoryError, OSError, |
| 1293 | "Operation only works on directories."); |
| 1294 | MiddlingExtendsException(PyExc_OSError, InterruptedError, OSError, |
| 1295 | "Interrupted by signal."); |
| 1296 | MiddlingExtendsException(PyExc_OSError, PermissionError, OSError, |
| 1297 | "Not enough permissions."); |
| 1298 | MiddlingExtendsException(PyExc_OSError, ProcessLookupError, OSError, |
| 1299 | "Process not found."); |
| 1300 | MiddlingExtendsException(PyExc_OSError, TimeoutError, OSError, |
| 1301 | "Timeout expired."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1302 | |
| 1303 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1304 | * EOFError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1305 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1306 | SimpleExtendsException(PyExc_Exception, EOFError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1307 | "Read beyond end of file."); |
| 1308 | |
| 1309 | |
| 1310 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1311 | * RuntimeError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1312 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1313 | SimpleExtendsException(PyExc_Exception, RuntimeError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1314 | "Unspecified run-time error."); |
| 1315 | |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 1316 | /* |
| 1317 | * RecursionError extends RuntimeError |
| 1318 | */ |
| 1319 | SimpleExtendsException(PyExc_RuntimeError, RecursionError, |
| 1320 | "Recursion limit exceeded."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * NotImplementedError extends RuntimeError |
| 1324 | */ |
| 1325 | SimpleExtendsException(PyExc_RuntimeError, NotImplementedError, |
| 1326 | "Method or function hasn't been implemented yet."); |
| 1327 | |
| 1328 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1329 | * NameError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1330 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1331 | SimpleExtendsException(PyExc_Exception, NameError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1332 | "Name not found globally."); |
| 1333 | |
| 1334 | /* |
| 1335 | * UnboundLocalError extends NameError |
| 1336 | */ |
| 1337 | SimpleExtendsException(PyExc_NameError, UnboundLocalError, |
| 1338 | "Local name referenced but not bound to a value."); |
| 1339 | |
| 1340 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1341 | * AttributeError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1342 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1343 | SimpleExtendsException(PyExc_Exception, AttributeError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1344 | "Attribute not found."); |
| 1345 | |
| 1346 | |
| 1347 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1348 | * SyntaxError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1349 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1350 | |
Raymond Hettinger | 7ea386e | 2016-08-25 21:11:50 -0700 | [diff] [blame] | 1351 | /* Helper function to customize error message for some syntax errors */ |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 1352 | static int _report_missing_parentheses(PySyntaxErrorObject *self); |
| 1353 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1354 | static int |
| 1355 | SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds) |
| 1356 | { |
| 1357 | PyObject *info = NULL; |
| 1358 | Py_ssize_t lenargs = PyTuple_GET_SIZE(args); |
| 1359 | |
| 1360 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1361 | return -1; |
| 1362 | |
| 1363 | if (lenargs >= 1) { |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 1364 | Py_INCREF(PyTuple_GET_ITEM(args, 0)); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1365 | Py_XSETREF(self->msg, PyTuple_GET_ITEM(args, 0)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1366 | } |
| 1367 | if (lenargs == 2) { |
| 1368 | info = PyTuple_GET_ITEM(args, 1); |
| 1369 | info = PySequence_Tuple(info); |
Benjamin Peterson | 90b1358 | 2012-02-03 19:22:31 -0500 | [diff] [blame] | 1370 | if (!info) |
| 1371 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1373 | if (PyTuple_GET_SIZE(info) != 4) { |
| 1374 | /* not a very good error message, but it's what Python 2.4 gives */ |
| 1375 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 1376 | Py_DECREF(info); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 1380 | Py_INCREF(PyTuple_GET_ITEM(info, 0)); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1381 | Py_XSETREF(self->filename, PyTuple_GET_ITEM(info, 0)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1382 | |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 1383 | Py_INCREF(PyTuple_GET_ITEM(info, 1)); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1384 | Py_XSETREF(self->lineno, PyTuple_GET_ITEM(info, 1)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1385 | |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 1386 | Py_INCREF(PyTuple_GET_ITEM(info, 2)); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1387 | Py_XSETREF(self->offset, PyTuple_GET_ITEM(info, 2)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1388 | |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 1389 | Py_INCREF(PyTuple_GET_ITEM(info, 3)); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1390 | Py_XSETREF(self->text, PyTuple_GET_ITEM(info, 3)); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1391 | |
| 1392 | Py_DECREF(info); |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 1393 | |
Martijn Pieters | 772d809 | 2017-08-22 21:16:23 +0100 | [diff] [blame] | 1394 | /* |
| 1395 | * Issue #21669: Custom error for 'print' & 'exec' as statements |
| 1396 | * |
| 1397 | * Only applies to SyntaxError instances, not to subclasses such |
| 1398 | * as TabError or IndentationError (see issue #31161) |
| 1399 | */ |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 1400 | if (Py_IS_TYPE(self, (PyTypeObject *)PyExc_SyntaxError) && |
Martijn Pieters | 772d809 | 2017-08-22 21:16:23 +0100 | [diff] [blame] | 1401 | self->text && PyUnicode_Check(self->text) && |
| 1402 | _report_missing_parentheses(self) < 0) { |
| 1403 | return -1; |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 1404 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1405 | } |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1409 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1410 | SyntaxError_clear(PySyntaxErrorObject *self) |
| 1411 | { |
| 1412 | Py_CLEAR(self->msg); |
| 1413 | Py_CLEAR(self->filename); |
| 1414 | Py_CLEAR(self->lineno); |
| 1415 | Py_CLEAR(self->offset); |
| 1416 | Py_CLEAR(self->text); |
| 1417 | Py_CLEAR(self->print_file_and_line); |
| 1418 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 1419 | } |
| 1420 | |
| 1421 | static void |
| 1422 | SyntaxError_dealloc(PySyntaxErrorObject *self) |
| 1423 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1424 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1425 | SyntaxError_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1426 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1429 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1430 | SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg) |
| 1431 | { |
| 1432 | Py_VISIT(self->msg); |
| 1433 | Py_VISIT(self->filename); |
| 1434 | Py_VISIT(self->lineno); |
| 1435 | Py_VISIT(self->offset); |
| 1436 | Py_VISIT(self->text); |
| 1437 | Py_VISIT(self->print_file_and_line); |
| 1438 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 1439 | } |
| 1440 | |
| 1441 | /* This is called "my_basename" instead of just "basename" to avoid name |
| 1442 | conflicts with glibc; basename is already prototyped if _GNU_SOURCE is |
| 1443 | defined, and Python does define that. */ |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1444 | static PyObject* |
| 1445 | my_basename(PyObject *name) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1446 | { |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1447 | Py_ssize_t i, size, offset; |
Victor Stinner | 31392e7 | 2011-10-05 20:14:23 +0200 | [diff] [blame] | 1448 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 1449 | const void *data; |
Victor Stinner | 31392e7 | 2011-10-05 20:14:23 +0200 | [diff] [blame] | 1450 | |
| 1451 | if (PyUnicode_READY(name)) |
| 1452 | return NULL; |
| 1453 | kind = PyUnicode_KIND(name); |
| 1454 | data = PyUnicode_DATA(name); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1455 | size = PyUnicode_GET_LENGTH(name); |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1456 | offset = 0; |
| 1457 | for(i=0; i < size; i++) { |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 1458 | if (PyUnicode_READ(kind, data, i) == SEP) { |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1459 | offset = i + 1; |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 1460 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1461 | } |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 1462 | if (offset != 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | return PyUnicode_Substring(name, offset, size); |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 1464 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | else { |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1466 | Py_INCREF(name); |
| 1467 | return name; |
| 1468 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | |
| 1472 | static PyObject * |
| 1473 | SyntaxError_str(PySyntaxErrorObject *self) |
| 1474 | { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1475 | int have_lineno = 0; |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1476 | PyObject *filename; |
| 1477 | PyObject *result; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 1478 | /* Below, we always ignore overflow errors, just printing -1. |
| 1479 | Still, we cannot allow an OverflowError to be raised, so |
| 1480 | we need to call PyLong_AsLongAndOverflow. */ |
| 1481 | int overflow; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1482 | |
| 1483 | /* XXX -- do all the additional formatting with filename and |
| 1484 | lineno here */ |
| 1485 | |
Neal Norwitz | ed2b739 | 2007-08-26 04:51:10 +0000 | [diff] [blame] | 1486 | if (self->filename && PyUnicode_Check(self->filename)) { |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1487 | filename = my_basename(self->filename); |
| 1488 | if (filename == NULL) |
| 1489 | return NULL; |
| 1490 | } else { |
| 1491 | filename = NULL; |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 1492 | } |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 1493 | have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1494 | |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 1495 | if (!filename && !have_lineno) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1496 | return PyObject_Str(self->msg ? self->msg : Py_None); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1497 | |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 1498 | if (filename && have_lineno) |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1499 | result = PyUnicode_FromFormat("%S (%U, line %ld)", |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 1500 | self->msg ? self->msg : Py_None, |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1501 | filename, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | PyLong_AsLongAndOverflow(self->lineno, &overflow)); |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 1503 | else if (filename) |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1504 | result = PyUnicode_FromFormat("%S (%U)", |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 1505 | self->msg ? self->msg : Py_None, |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1506 | filename); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1507 | else /* only have_lineno */ |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1508 | result = PyUnicode_FromFormat("%S (line %ld)", |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 1509 | self->msg ? self->msg : Py_None, |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 1510 | PyLong_AsLongAndOverflow(self->lineno, &overflow)); |
Victor Stinner | 6237daf | 2010-04-28 17:26:19 +0000 | [diff] [blame] | 1511 | Py_XDECREF(filename); |
| 1512 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | static PyMemberDef SyntaxError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1516 | {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0, |
| 1517 | PyDoc_STR("exception msg")}, |
| 1518 | {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0, |
| 1519 | PyDoc_STR("exception filename")}, |
| 1520 | {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0, |
| 1521 | PyDoc_STR("exception lineno")}, |
| 1522 | {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0, |
| 1523 | PyDoc_STR("exception offset")}, |
| 1524 | {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0, |
| 1525 | PyDoc_STR("exception text")}, |
| 1526 | {"print_file_and_line", T_OBJECT, |
| 1527 | offsetof(PySyntaxErrorObject, print_file_and_line), 0, |
| 1528 | PyDoc_STR("exception print_file_and_line")}, |
| 1529 | {NULL} /* Sentinel */ |
| 1530 | }; |
| 1531 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1532 | ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError, |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1533 | 0, 0, SyntaxError_members, 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1534 | SyntaxError_str, "Invalid syntax."); |
| 1535 | |
| 1536 | |
| 1537 | /* |
| 1538 | * IndentationError extends SyntaxError |
| 1539 | */ |
| 1540 | MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError, |
| 1541 | "Improper indentation."); |
| 1542 | |
| 1543 | |
| 1544 | /* |
| 1545 | * TabError extends IndentationError |
| 1546 | */ |
| 1547 | MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError, |
| 1548 | "Improper mixture of spaces and tabs."); |
| 1549 | |
| 1550 | |
| 1551 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1552 | * LookupError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1553 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1554 | SimpleExtendsException(PyExc_Exception, LookupError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1555 | "Base class for lookup errors."); |
| 1556 | |
| 1557 | |
| 1558 | /* |
| 1559 | * IndexError extends LookupError |
| 1560 | */ |
| 1561 | SimpleExtendsException(PyExc_LookupError, IndexError, |
| 1562 | "Sequence index out of range."); |
| 1563 | |
| 1564 | |
| 1565 | /* |
| 1566 | * KeyError extends LookupError |
| 1567 | */ |
| 1568 | static PyObject * |
| 1569 | KeyError_str(PyBaseExceptionObject *self) |
| 1570 | { |
| 1571 | /* If args is a tuple of exactly one item, apply repr to args[0]. |
| 1572 | This is done so that e.g. the exception raised by {}[''] prints |
| 1573 | KeyError: '' |
| 1574 | rather than the confusing |
| 1575 | KeyError |
| 1576 | alone. The downside is that if KeyError is raised with an explanatory |
| 1577 | string, that string will be displayed in quotes. Too bad. |
| 1578 | If args is anything else, use the default BaseException__str__(). |
| 1579 | */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1580 | if (PyTuple_GET_SIZE(self->args) == 1) { |
Walter Dörwald | f5bec7c | 2007-05-26 15:03:32 +0000 | [diff] [blame] | 1581 | return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1582 | } |
| 1583 | return BaseException_str(self); |
| 1584 | } |
| 1585 | |
| 1586 | ComplexExtendsException(PyExc_LookupError, KeyError, BaseException, |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1587 | 0, 0, 0, 0, KeyError_str, "Mapping key not found."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1588 | |
| 1589 | |
| 1590 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1591 | * ValueError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1592 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1593 | SimpleExtendsException(PyExc_Exception, ValueError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1594 | "Inappropriate argument value (of correct type)."); |
| 1595 | |
| 1596 | /* |
| 1597 | * UnicodeError extends ValueError |
| 1598 | */ |
| 1599 | |
| 1600 | SimpleExtendsException(PyExc_ValueError, UnicodeError, |
| 1601 | "Unicode related error."); |
| 1602 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1603 | static PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1604 | get_string(PyObject *attr, const char *name) |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 1605 | { |
| 1606 | if (!attr) { |
| 1607 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 1608 | return NULL; |
| 1609 | } |
| 1610 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1611 | if (!PyBytes_Check(attr)) { |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 1612 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name); |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | Py_INCREF(attr); |
| 1616 | return attr; |
| 1617 | } |
| 1618 | |
| 1619 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1620 | get_unicode(PyObject *attr, const char *name) |
| 1621 | { |
| 1622 | if (!attr) { |
| 1623 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name); |
| 1624 | return NULL; |
| 1625 | } |
| 1626 | |
| 1627 | if (!PyUnicode_Check(attr)) { |
| 1628 | PyErr_Format(PyExc_TypeError, |
| 1629 | "%.200s attribute must be unicode", name); |
| 1630 | return NULL; |
| 1631 | } |
| 1632 | Py_INCREF(attr); |
| 1633 | return attr; |
| 1634 | } |
| 1635 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1636 | static int |
| 1637 | set_unicodefromstring(PyObject **attr, const char *value) |
| 1638 | { |
| 1639 | PyObject *obj = PyUnicode_FromString(value); |
| 1640 | if (!obj) |
| 1641 | return -1; |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1642 | Py_XSETREF(*attr, obj); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1643 | return 0; |
| 1644 | } |
| 1645 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1646 | PyObject * |
| 1647 | PyUnicodeEncodeError_GetEncoding(PyObject *exc) |
| 1648 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1649 | return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | PyObject * |
| 1653 | PyUnicodeDecodeError_GetEncoding(PyObject *exc) |
| 1654 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1655 | return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | PyObject * |
| 1659 | PyUnicodeEncodeError_GetObject(PyObject *exc) |
| 1660 | { |
| 1661 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1662 | } |
| 1663 | |
| 1664 | PyObject * |
| 1665 | PyUnicodeDecodeError_GetObject(PyObject *exc) |
| 1666 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1667 | return get_string(((PyUnicodeErrorObject *)exc)->object, "object"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | PyObject * |
| 1671 | PyUnicodeTranslateError_GetObject(PyObject *exc) |
| 1672 | { |
| 1673 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object"); |
| 1674 | } |
| 1675 | |
| 1676 | int |
| 1677 | PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1678 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1679 | Py_ssize_t size; |
| 1680 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1681 | "object"); |
| 1682 | if (!obj) |
| 1683 | return -1; |
| 1684 | *start = ((PyUnicodeErrorObject *)exc)->start; |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1685 | size = PyUnicode_GET_LENGTH(obj); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1686 | if (*start<0) |
| 1687 | *start = 0; /*XXX check for values <0*/ |
| 1688 | if (*start>=size) |
| 1689 | *start = size-1; |
| 1690 | Py_DECREF(obj); |
| 1691 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | |
| 1695 | int |
| 1696 | PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1697 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1698 | Py_ssize_t size; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1699 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object"); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1700 | if (!obj) |
| 1701 | return -1; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1702 | size = PyBytes_GET_SIZE(obj); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1703 | *start = ((PyUnicodeErrorObject *)exc)->start; |
| 1704 | if (*start<0) |
| 1705 | *start = 0; |
| 1706 | if (*start>=size) |
| 1707 | *start = size-1; |
| 1708 | Py_DECREF(obj); |
| 1709 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | |
| 1713 | int |
| 1714 | PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start) |
| 1715 | { |
| 1716 | return PyUnicodeEncodeError_GetStart(exc, start); |
| 1717 | } |
| 1718 | |
| 1719 | |
| 1720 | int |
| 1721 | PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1722 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1723 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1724 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | |
| 1728 | int |
| 1729 | PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1730 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1731 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1732 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | |
| 1736 | int |
| 1737 | PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start) |
| 1738 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1739 | ((PyUnicodeErrorObject *)exc)->start = start; |
| 1740 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | int |
| 1745 | PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1746 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1747 | Py_ssize_t size; |
| 1748 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, |
| 1749 | "object"); |
| 1750 | if (!obj) |
| 1751 | return -1; |
| 1752 | *end = ((PyUnicodeErrorObject *)exc)->end; |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1753 | size = PyUnicode_GET_LENGTH(obj); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1754 | if (*end<1) |
| 1755 | *end = 1; |
| 1756 | if (*end>size) |
| 1757 | *end = size; |
| 1758 | Py_DECREF(obj); |
| 1759 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | |
| 1763 | int |
| 1764 | PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
| 1765 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1766 | Py_ssize_t size; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1767 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object"); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1768 | if (!obj) |
| 1769 | return -1; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1770 | size = PyBytes_GET_SIZE(obj); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1771 | *end = ((PyUnicodeErrorObject *)exc)->end; |
| 1772 | if (*end<1) |
| 1773 | *end = 1; |
| 1774 | if (*end>size) |
| 1775 | *end = size; |
| 1776 | Py_DECREF(obj); |
| 1777 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | int |
Max Bernstein | e6a0e80 | 2019-04-02 01:16:22 -0700 | [diff] [blame] | 1782 | PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *end) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1783 | { |
Max Bernstein | e6a0e80 | 2019-04-02 01:16:22 -0700 | [diff] [blame] | 1784 | return PyUnicodeEncodeError_GetEnd(exc, end); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | int |
| 1789 | PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1790 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1791 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1792 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | int |
| 1797 | PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1798 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1799 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1800 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | |
| 1804 | int |
| 1805 | PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end) |
| 1806 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1807 | ((PyUnicodeErrorObject *)exc)->end = end; |
| 1808 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | PyObject * |
| 1812 | PyUnicodeEncodeError_GetReason(PyObject *exc) |
| 1813 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1814 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | PyObject * |
| 1819 | PyUnicodeDecodeError_GetReason(PyObject *exc) |
| 1820 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1821 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | |
| 1825 | PyObject * |
| 1826 | PyUnicodeTranslateError_GetReason(PyObject *exc) |
| 1827 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1828 | return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | int |
| 1833 | PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason) |
| 1834 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1835 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1836 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | |
| 1840 | int |
| 1841 | PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason) |
| 1842 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1843 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1844 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | int |
| 1849 | PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason) |
| 1850 | { |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1851 | return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason, |
| 1852 | reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1856 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1857 | UnicodeError_clear(PyUnicodeErrorObject *self) |
| 1858 | { |
| 1859 | Py_CLEAR(self->encoding); |
| 1860 | Py_CLEAR(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1861 | Py_CLEAR(self->reason); |
| 1862 | return BaseException_clear((PyBaseExceptionObject *)self); |
| 1863 | } |
| 1864 | |
| 1865 | static void |
| 1866 | UnicodeError_dealloc(PyUnicodeErrorObject *self) |
| 1867 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1868 | _PyObject_GC_UNTRACK(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1869 | UnicodeError_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1870 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1873 | static int |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1874 | UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg) |
| 1875 | { |
| 1876 | Py_VISIT(self->encoding); |
| 1877 | Py_VISIT(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1878 | Py_VISIT(self->reason); |
| 1879 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); |
| 1880 | } |
| 1881 | |
| 1882 | static PyMemberDef UnicodeError_members[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1883 | {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0, |
| 1884 | PyDoc_STR("exception encoding")}, |
| 1885 | {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0, |
| 1886 | PyDoc_STR("exception object")}, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1887 | {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1888 | PyDoc_STR("exception start")}, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1889 | {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1890 | PyDoc_STR("exception end")}, |
| 1891 | {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0, |
| 1892 | PyDoc_STR("exception reason")}, |
| 1893 | {NULL} /* Sentinel */ |
| 1894 | }; |
| 1895 | |
| 1896 | |
| 1897 | /* |
| 1898 | * UnicodeEncodeError extends UnicodeError |
| 1899 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1900 | |
| 1901 | static int |
| 1902 | UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1903 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1904 | PyUnicodeErrorObject *err; |
| 1905 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1906 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 1907 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1908 | |
| 1909 | err = (PyUnicodeErrorObject *)self; |
| 1910 | |
| 1911 | Py_CLEAR(err->encoding); |
| 1912 | Py_CLEAR(err->object); |
| 1913 | Py_CLEAR(err->reason); |
| 1914 | |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 1915 | if (!PyArg_ParseTuple(args, "UUnnU", |
| 1916 | &err->encoding, &err->object, |
| 1917 | &err->start, &err->end, &err->reason)) { |
| 1918 | err->encoding = err->object = err->reason = NULL; |
Martin v. Löwis | b09af03 | 2011-11-04 11:16:41 +0100 | [diff] [blame] | 1919 | return -1; |
| 1920 | } |
| 1921 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1922 | Py_INCREF(err->encoding); |
| 1923 | Py_INCREF(err->object); |
| 1924 | Py_INCREF(err->reason); |
| 1925 | |
| 1926 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | static PyObject * |
| 1930 | UnicodeEncodeError_str(PyObject *self) |
| 1931 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1932 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 1933 | PyObject *result = NULL; |
| 1934 | PyObject *reason_str = NULL; |
| 1935 | PyObject *encoding_str = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1936 | |
Benjamin Peterson | 9b09ba1 | 2014-04-02 12:15:06 -0400 | [diff] [blame] | 1937 | if (!uself->object) |
| 1938 | /* Not properly initialized. */ |
| 1939 | return PyUnicode_FromString(""); |
| 1940 | |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 1941 | /* Get reason and encoding as strings, which they might not be if |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 1942 | they've been modified after we were constructed. */ |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 1943 | reason_str = PyObject_Str(uself->reason); |
| 1944 | if (reason_str == NULL) |
| 1945 | goto done; |
| 1946 | encoding_str = PyObject_Str(uself->encoding); |
| 1947 | if (encoding_str == NULL) |
| 1948 | goto done; |
| 1949 | |
Victor Stinner | da1ddf3 | 2011-11-20 22:50:23 +0100 | [diff] [blame] | 1950 | if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) { |
| 1951 | Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start); |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1952 | const char *fmt; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1953 | if (badchar <= 0xff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1954 | fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1955 | else if (badchar <= 0xffff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1956 | fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1957 | else |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1958 | fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U"; |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 1959 | result = PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 1960 | fmt, |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 1961 | encoding_str, |
Victor Stinner | da1ddf3 | 2011-11-20 22:50:23 +0100 | [diff] [blame] | 1962 | (int)badchar, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 1963 | uself->start, |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 1964 | reason_str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1965 | } |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 1966 | else { |
| 1967 | result = PyUnicode_FromFormat( |
| 1968 | "'%U' codec can't encode characters in position %zd-%zd: %U", |
| 1969 | encoding_str, |
| 1970 | uself->start, |
| 1971 | uself->end-1, |
| 1972 | reason_str); |
| 1973 | } |
| 1974 | done: |
| 1975 | Py_XDECREF(reason_str); |
| 1976 | Py_XDECREF(encoding_str); |
| 1977 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | static PyTypeObject _PyExc_UnicodeEncodeError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1981 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 1982 | "UnicodeEncodeError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1983 | sizeof(PyUnicodeErrorObject), 0, |
| 1984 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1985 | (reprfunc)UnicodeEncodeError_str, 0, 0, 0, |
| 1986 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1987 | PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse, |
| 1988 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1989 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1990 | (initproc)UnicodeEncodeError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1991 | }; |
| 1992 | PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError; |
| 1993 | |
| 1994 | PyObject * |
| 1995 | PyUnicodeEncodeError_Create( |
| 1996 | const char *encoding, const Py_UNICODE *object, Py_ssize_t length, |
| 1997 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 1998 | { |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 1999 | return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2000 | encoding, object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | |
| 2004 | /* |
| 2005 | * UnicodeDecodeError extends UnicodeError |
| 2006 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2007 | |
| 2008 | static int |
| 2009 | UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2010 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2011 | PyUnicodeErrorObject *ude; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2012 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2013 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 2014 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2015 | |
| 2016 | ude = (PyUnicodeErrorObject *)self; |
| 2017 | |
| 2018 | Py_CLEAR(ude->encoding); |
| 2019 | Py_CLEAR(ude->object); |
| 2020 | Py_CLEAR(ude->reason); |
| 2021 | |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 2022 | if (!PyArg_ParseTuple(args, "UOnnU", |
| 2023 | &ude->encoding, &ude->object, |
| 2024 | &ude->start, &ude->end, &ude->reason)) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2025 | ude->encoding = ude->object = ude->reason = NULL; |
| 2026 | return -1; |
| 2027 | } |
| 2028 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2029 | Py_INCREF(ude->encoding); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2030 | Py_INCREF(ude->object); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2031 | Py_INCREF(ude->reason); |
| 2032 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2033 | if (!PyBytes_Check(ude->object)) { |
| 2034 | Py_buffer view; |
| 2035 | if (PyObject_GetBuffer(ude->object, &view, PyBUF_SIMPLE) != 0) |
| 2036 | goto error; |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2037 | Py_XSETREF(ude->object, PyBytes_FromStringAndSize(view.buf, view.len)); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2038 | PyBuffer_Release(&view); |
| 2039 | if (!ude->object) |
| 2040 | goto error; |
| 2041 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2042 | return 0; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2043 | |
| 2044 | error: |
| 2045 | Py_CLEAR(ude->encoding); |
| 2046 | Py_CLEAR(ude->object); |
| 2047 | Py_CLEAR(ude->reason); |
| 2048 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | static PyObject * |
| 2052 | UnicodeDecodeError_str(PyObject *self) |
| 2053 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 2054 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2055 | PyObject *result = NULL; |
| 2056 | PyObject *reason_str = NULL; |
| 2057 | PyObject *encoding_str = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2058 | |
Benjamin Peterson | 9b09ba1 | 2014-04-02 12:15:06 -0400 | [diff] [blame] | 2059 | if (!uself->object) |
| 2060 | /* Not properly initialized. */ |
| 2061 | return PyUnicode_FromString(""); |
| 2062 | |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2063 | /* Get reason and encoding as strings, which they might not be if |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 2064 | they've been modified after we were constructed. */ |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2065 | reason_str = PyObject_Str(uself->reason); |
| 2066 | if (reason_str == NULL) |
| 2067 | goto done; |
| 2068 | encoding_str = PyObject_Str(uself->encoding); |
| 2069 | if (encoding_str == NULL) |
| 2070 | goto done; |
| 2071 | |
| 2072 | if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2073 | int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff); |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2074 | result = PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 2075 | "'%U' codec can't decode byte 0x%02x in position %zd: %U", |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2076 | encoding_str, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2077 | byte, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 2078 | uself->start, |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2079 | reason_str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2080 | } |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2081 | else { |
| 2082 | result = PyUnicode_FromFormat( |
| 2083 | "'%U' codec can't decode bytes in position %zd-%zd: %U", |
| 2084 | encoding_str, |
| 2085 | uself->start, |
| 2086 | uself->end-1, |
| 2087 | reason_str |
| 2088 | ); |
| 2089 | } |
| 2090 | done: |
| 2091 | Py_XDECREF(reason_str); |
| 2092 | Py_XDECREF(encoding_str); |
| 2093 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | static PyTypeObject _PyExc_UnicodeDecodeError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2097 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 2098 | "UnicodeDecodeError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2099 | sizeof(PyUnicodeErrorObject), 0, |
| 2100 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2101 | (reprfunc)UnicodeDecodeError_str, 0, 0, 0, |
| 2102 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2103 | PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse, |
| 2104 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2105 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2106 | (initproc)UnicodeDecodeError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2107 | }; |
| 2108 | PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError; |
| 2109 | |
| 2110 | PyObject * |
| 2111 | PyUnicodeDecodeError_Create( |
| 2112 | const char *encoding, const char *object, Py_ssize_t length, |
| 2113 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 2114 | { |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 2115 | return PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nns", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2116 | encoding, object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | |
| 2120 | /* |
| 2121 | * UnicodeTranslateError extends UnicodeError |
| 2122 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2123 | |
| 2124 | static int |
| 2125 | UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args, |
| 2126 | PyObject *kwds) |
| 2127 | { |
| 2128 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) |
| 2129 | return -1; |
| 2130 | |
| 2131 | Py_CLEAR(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2132 | Py_CLEAR(self->reason); |
| 2133 | |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 2134 | if (!PyArg_ParseTuple(args, "UnnU", |
| 2135 | &self->object, |
| 2136 | &self->start, &self->end, &self->reason)) { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 2137 | self->object = self->reason = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2138 | return -1; |
| 2139 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2140 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2141 | Py_INCREF(self->object); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2142 | Py_INCREF(self->reason); |
| 2143 | |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | |
| 2148 | static PyObject * |
| 2149 | UnicodeTranslateError_str(PyObject *self) |
| 2150 | { |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 2151 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2152 | PyObject *result = NULL; |
| 2153 | PyObject *reason_str = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2154 | |
Benjamin Peterson | 9b09ba1 | 2014-04-02 12:15:06 -0400 | [diff] [blame] | 2155 | if (!uself->object) |
| 2156 | /* Not properly initialized. */ |
| 2157 | return PyUnicode_FromString(""); |
| 2158 | |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2159 | /* Get reason as a string, which it might not be if it's been |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 2160 | modified after we were constructed. */ |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2161 | reason_str = PyObject_Str(uself->reason); |
| 2162 | if (reason_str == NULL) |
| 2163 | goto done; |
| 2164 | |
Victor Stinner | 53b33e7 | 2011-11-21 01:17:27 +0100 | [diff] [blame] | 2165 | if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) { |
| 2166 | Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start); |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 2167 | const char *fmt; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2168 | if (badchar <= 0xff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 2169 | fmt = "can't translate character '\\x%02x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2170 | else if (badchar <= 0xffff) |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 2171 | fmt = "can't translate character '\\u%04x' in position %zd: %U"; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2172 | else |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 2173 | fmt = "can't translate character '\\U%08x' in position %zd: %U"; |
Benjamin Peterson | c5f4e1e | 2010-02-25 01:22:28 +0000 | [diff] [blame] | 2174 | result = PyUnicode_FromFormat( |
Walter Dörwald | 787b03b | 2007-06-05 13:29:29 +0000 | [diff] [blame] | 2175 | fmt, |
Victor Stinner | 53b33e7 | 2011-11-21 01:17:27 +0100 | [diff] [blame] | 2176 | (int)badchar, |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 2177 | uself->start, |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2178 | reason_str |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2179 | ); |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2180 | } else { |
| 2181 | result = PyUnicode_FromFormat( |
| 2182 | "can't translate characters in position %zd-%zd: %U", |
| 2183 | uself->start, |
| 2184 | uself->end-1, |
| 2185 | reason_str |
| 2186 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2187 | } |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 2188 | done: |
| 2189 | Py_XDECREF(reason_str); |
| 2190 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | static PyTypeObject _PyExc_UnicodeTranslateError = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2194 | PyVarObject_HEAD_INIT(NULL, 0) |
Neal Norwitz | 2633c69 | 2007-02-26 22:22:47 +0000 | [diff] [blame] | 2195 | "UnicodeTranslateError", |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2196 | sizeof(PyUnicodeErrorObject), 0, |
| 2197 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2198 | (reprfunc)UnicodeTranslateError_str, 0, 0, 0, |
| 2199 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2200 | PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2201 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, |
| 2202 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2203 | (initproc)UnicodeTranslateError_init, 0, BaseException_new, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2204 | }; |
| 2205 | PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError; |
| 2206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2207 | /* Deprecated. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2208 | PyObject * |
| 2209 | PyUnicodeTranslateError_Create( |
| 2210 | const Py_UNICODE *object, Py_ssize_t length, |
| 2211 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 2212 | { |
| 2213 | return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns", |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2214 | object, length, start, end, reason); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2215 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2217 | PyObject * |
| 2218 | _PyUnicodeTranslateError_Create( |
| 2219 | PyObject *object, |
| 2220 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
| 2221 | { |
Victor Stinner | 69598d4 | 2014-04-04 20:59:44 +0200 | [diff] [blame] | 2222 | return PyObject_CallFunction(PyExc_UnicodeTranslateError, "Onns", |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2223 | object, start, end, reason); |
| 2224 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2225 | |
| 2226 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2227 | * AssertionError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2228 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2229 | SimpleExtendsException(PyExc_Exception, AssertionError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2230 | "Assertion failed."); |
| 2231 | |
| 2232 | |
| 2233 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2234 | * ArithmeticError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2235 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2236 | SimpleExtendsException(PyExc_Exception, ArithmeticError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2237 | "Base class for arithmetic errors."); |
| 2238 | |
| 2239 | |
| 2240 | /* |
| 2241 | * FloatingPointError extends ArithmeticError |
| 2242 | */ |
| 2243 | SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError, |
| 2244 | "Floating point operation failed."); |
| 2245 | |
| 2246 | |
| 2247 | /* |
| 2248 | * OverflowError extends ArithmeticError |
| 2249 | */ |
| 2250 | SimpleExtendsException(PyExc_ArithmeticError, OverflowError, |
| 2251 | "Result too large to be represented."); |
| 2252 | |
| 2253 | |
| 2254 | /* |
| 2255 | * ZeroDivisionError extends ArithmeticError |
| 2256 | */ |
| 2257 | SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError, |
| 2258 | "Second argument to a division or modulo operation was zero."); |
| 2259 | |
| 2260 | |
| 2261 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2262 | * SystemError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2263 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2264 | SimpleExtendsException(PyExc_Exception, SystemError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2265 | "Internal error in the Python interpreter.\n" |
| 2266 | "\n" |
| 2267 | "Please report this to the Python maintainer, along with the traceback,\n" |
| 2268 | "the Python version, and the hardware/OS platform and version."); |
| 2269 | |
| 2270 | |
| 2271 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2272 | * ReferenceError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2273 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2274 | SimpleExtendsException(PyExc_Exception, ReferenceError, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2275 | "Weak ref proxy used after referent went away."); |
| 2276 | |
| 2277 | |
| 2278 | /* |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2279 | * MemoryError extends Exception |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2280 | */ |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2281 | |
| 2282 | #define MEMERRORS_SAVE 16 |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2283 | |
| 2284 | static PyObject * |
| 2285 | MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2286 | { |
| 2287 | PyBaseExceptionObject *self; |
| 2288 | |
Pablo Galindo | 9b648a9 | 2020-09-01 19:39:46 +0100 | [diff] [blame] | 2289 | /* If this is a subclass of MemoryError, don't use the freelist |
| 2290 | * and just return a fresh object */ |
| 2291 | if (type != (PyTypeObject *) PyExc_MemoryError) { |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2292 | return BaseException_new(type, args, kwds); |
Pablo Galindo | 9b648a9 | 2020-09-01 19:39:46 +0100 | [diff] [blame] | 2293 | } |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2294 | |
| 2295 | struct _Py_exc_state *state = get_exc_state(); |
| 2296 | if (state->memerrors_freelist == NULL) { |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2297 | return BaseException_new(type, args, kwds); |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2298 | } |
| 2299 | |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2300 | /* Fetch object from freelist and revive it */ |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2301 | self = state->memerrors_freelist; |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2302 | self->args = PyTuple_New(0); |
| 2303 | /* This shouldn't happen since the empty tuple is persistent */ |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2304 | if (self->args == NULL) { |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2305 | return NULL; |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | state->memerrors_freelist = (PyBaseExceptionObject *) self->dict; |
| 2309 | state->memerrors_numfree--; |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2310 | self->dict = NULL; |
| 2311 | _Py_NewReference((PyObject *)self); |
| 2312 | _PyObject_GC_TRACK(self); |
| 2313 | return (PyObject *)self; |
| 2314 | } |
| 2315 | |
| 2316 | static void |
| 2317 | MemoryError_dealloc(PyBaseExceptionObject *self) |
| 2318 | { |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2319 | BaseException_clear(self); |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2320 | |
Pablo Galindo | 9b648a9 | 2020-09-01 19:39:46 +0100 | [diff] [blame] | 2321 | /* If this is a subclass of MemoryError, we don't need to |
| 2322 | * do anything in the free-list*/ |
| 2323 | if (!Py_IS_TYPE(self, (PyTypeObject *) PyExc_MemoryError)) { |
Victor Stinner | bbeb223 | 2020-09-23 23:25:40 +0200 | [diff] [blame^] | 2324 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 2325 | return; |
Pablo Galindo | 9b648a9 | 2020-09-01 19:39:46 +0100 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | _PyObject_GC_UNTRACK(self); |
| 2329 | |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2330 | struct _Py_exc_state *state = get_exc_state(); |
| 2331 | if (state->memerrors_numfree >= MEMERRORS_SAVE) { |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2332 | Py_TYPE(self)->tp_free((PyObject *)self); |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2333 | } |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2334 | else { |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2335 | self->dict = (PyObject *) state->memerrors_freelist; |
| 2336 | state->memerrors_freelist = self; |
| 2337 | state->memerrors_numfree++; |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2341 | static int |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2342 | preallocate_memerrors(void) |
| 2343 | { |
| 2344 | /* We create enough MemoryErrors and then decref them, which will fill |
| 2345 | up the freelist. */ |
| 2346 | int i; |
| 2347 | PyObject *errors[MEMERRORS_SAVE]; |
| 2348 | for (i = 0; i < MEMERRORS_SAVE; i++) { |
| 2349 | errors[i] = MemoryError_new((PyTypeObject *) PyExc_MemoryError, |
| 2350 | NULL, NULL); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2351 | if (!errors[i]) { |
| 2352 | return -1; |
| 2353 | } |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2354 | } |
| 2355 | for (i = 0; i < MEMERRORS_SAVE; i++) { |
| 2356 | Py_DECREF(errors[i]); |
| 2357 | } |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2358 | return 0; |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | static void |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2362 | free_preallocated_memerrors(struct _Py_exc_state *state) |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2363 | { |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2364 | while (state->memerrors_freelist != NULL) { |
| 2365 | PyObject *self = (PyObject *) state->memerrors_freelist; |
| 2366 | state->memerrors_freelist = (PyBaseExceptionObject *)state->memerrors_freelist->dict; |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 2367 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | |
| 2372 | static PyTypeObject _PyExc_MemoryError = { |
| 2373 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2374 | "MemoryError", |
| 2375 | sizeof(PyBaseExceptionObject), |
| 2376 | 0, (destructor)MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0, |
| 2377 | 0, 0, 0, 0, 0, 0, 0, |
| 2378 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 2379 | PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse, |
| 2380 | (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception, |
| 2381 | 0, 0, 0, offsetof(PyBaseExceptionObject, dict), |
| 2382 | (initproc)BaseException_init, 0, MemoryError_new |
| 2383 | }; |
| 2384 | PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError; |
| 2385 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2386 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2387 | /* |
| 2388 | * BufferError extends Exception |
| 2389 | */ |
| 2390 | SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error."); |
| 2391 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2392 | |
| 2393 | /* Warning category docstrings */ |
| 2394 | |
| 2395 | /* |
| 2396 | * Warning extends Exception |
| 2397 | */ |
| 2398 | SimpleExtendsException(PyExc_Exception, Warning, |
| 2399 | "Base class for warning categories."); |
| 2400 | |
| 2401 | |
| 2402 | /* |
| 2403 | * UserWarning extends Warning |
| 2404 | */ |
| 2405 | SimpleExtendsException(PyExc_Warning, UserWarning, |
| 2406 | "Base class for warnings generated by user code."); |
| 2407 | |
| 2408 | |
| 2409 | /* |
| 2410 | * DeprecationWarning extends Warning |
| 2411 | */ |
| 2412 | SimpleExtendsException(PyExc_Warning, DeprecationWarning, |
| 2413 | "Base class for warnings about deprecated features."); |
| 2414 | |
| 2415 | |
| 2416 | /* |
| 2417 | * PendingDeprecationWarning extends Warning |
| 2418 | */ |
| 2419 | SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning, |
| 2420 | "Base class for warnings about features which will be deprecated\n" |
| 2421 | "in the future."); |
| 2422 | |
| 2423 | |
| 2424 | /* |
| 2425 | * SyntaxWarning extends Warning |
| 2426 | */ |
| 2427 | SimpleExtendsException(PyExc_Warning, SyntaxWarning, |
| 2428 | "Base class for warnings about dubious syntax."); |
| 2429 | |
| 2430 | |
| 2431 | /* |
| 2432 | * RuntimeWarning extends Warning |
| 2433 | */ |
| 2434 | SimpleExtendsException(PyExc_Warning, RuntimeWarning, |
| 2435 | "Base class for warnings about dubious runtime behavior."); |
| 2436 | |
| 2437 | |
| 2438 | /* |
| 2439 | * FutureWarning extends Warning |
| 2440 | */ |
| 2441 | SimpleExtendsException(PyExc_Warning, FutureWarning, |
| 2442 | "Base class for warnings about constructs that will change semantically\n" |
| 2443 | "in the future."); |
| 2444 | |
| 2445 | |
| 2446 | /* |
| 2447 | * ImportWarning extends Warning |
| 2448 | */ |
| 2449 | SimpleExtendsException(PyExc_Warning, ImportWarning, |
| 2450 | "Base class for warnings about probable mistakes in module imports"); |
| 2451 | |
| 2452 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2453 | /* |
| 2454 | * UnicodeWarning extends Warning |
| 2455 | */ |
| 2456 | SimpleExtendsException(PyExc_Warning, UnicodeWarning, |
| 2457 | "Base class for warnings about Unicode related problems, mostly\n" |
| 2458 | "related to conversion problems."); |
| 2459 | |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 2460 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2461 | /* |
| 2462 | * BytesWarning extends Warning |
| 2463 | */ |
| 2464 | SimpleExtendsException(PyExc_Warning, BytesWarning, |
| 2465 | "Base class for warnings about bytes and buffer related problems, mostly\n" |
| 2466 | "related to conversion from str or comparing to str."); |
| 2467 | |
| 2468 | |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 2469 | /* |
| 2470 | * ResourceWarning extends Warning |
| 2471 | */ |
| 2472 | SimpleExtendsException(PyExc_Warning, ResourceWarning, |
| 2473 | "Base class for warnings about resource usage."); |
| 2474 | |
| 2475 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2476 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2477 | #ifdef MS_WINDOWS |
Antoine Pitrou | 7faf705 | 2013-03-31 22:48:04 +0200 | [diff] [blame] | 2478 | #include <winsock2.h> |
Brian Curtin | 401f9f3 | 2012-05-13 11:19:23 -0500 | [diff] [blame] | 2479 | /* The following constants were added to errno.h in VS2010 but have |
| 2480 | preferred WSA equivalents. */ |
| 2481 | #undef EADDRINUSE |
| 2482 | #undef EADDRNOTAVAIL |
| 2483 | #undef EAFNOSUPPORT |
| 2484 | #undef EALREADY |
| 2485 | #undef ECONNABORTED |
| 2486 | #undef ECONNREFUSED |
| 2487 | #undef ECONNRESET |
| 2488 | #undef EDESTADDRREQ |
| 2489 | #undef EHOSTUNREACH |
| 2490 | #undef EINPROGRESS |
| 2491 | #undef EISCONN |
| 2492 | #undef ELOOP |
| 2493 | #undef EMSGSIZE |
| 2494 | #undef ENETDOWN |
| 2495 | #undef ENETRESET |
| 2496 | #undef ENETUNREACH |
| 2497 | #undef ENOBUFS |
| 2498 | #undef ENOPROTOOPT |
| 2499 | #undef ENOTCONN |
| 2500 | #undef ENOTSOCK |
| 2501 | #undef EOPNOTSUPP |
| 2502 | #undef EPROTONOSUPPORT |
| 2503 | #undef EPROTOTYPE |
| 2504 | #undef ETIMEDOUT |
| 2505 | #undef EWOULDBLOCK |
| 2506 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2507 | #if defined(WSAEALREADY) && !defined(EALREADY) |
| 2508 | #define EALREADY WSAEALREADY |
| 2509 | #endif |
| 2510 | #if defined(WSAECONNABORTED) && !defined(ECONNABORTED) |
| 2511 | #define ECONNABORTED WSAECONNABORTED |
| 2512 | #endif |
| 2513 | #if defined(WSAECONNREFUSED) && !defined(ECONNREFUSED) |
| 2514 | #define ECONNREFUSED WSAECONNREFUSED |
| 2515 | #endif |
| 2516 | #if defined(WSAECONNRESET) && !defined(ECONNRESET) |
| 2517 | #define ECONNRESET WSAECONNRESET |
| 2518 | #endif |
| 2519 | #if defined(WSAEINPROGRESS) && !defined(EINPROGRESS) |
| 2520 | #define EINPROGRESS WSAEINPROGRESS |
| 2521 | #endif |
| 2522 | #if defined(WSAESHUTDOWN) && !defined(ESHUTDOWN) |
| 2523 | #define ESHUTDOWN WSAESHUTDOWN |
| 2524 | #endif |
| 2525 | #if defined(WSAETIMEDOUT) && !defined(ETIMEDOUT) |
| 2526 | #define ETIMEDOUT WSAETIMEDOUT |
| 2527 | #endif |
| 2528 | #if defined(WSAEWOULDBLOCK) && !defined(EWOULDBLOCK) |
| 2529 | #define EWOULDBLOCK WSAEWOULDBLOCK |
| 2530 | #endif |
| 2531 | #endif /* MS_WINDOWS */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2532 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2533 | PyStatus |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2534 | _PyExc_Init(PyThreadState *tstate) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2535 | { |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2536 | struct _Py_exc_state *state = &tstate->interp->exc_state; |
| 2537 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2538 | #define PRE_INIT(TYPE) \ |
| 2539 | if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \ |
| 2540 | if (PyType_Ready(&_PyExc_ ## TYPE) < 0) { \ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2541 | return _PyStatus_ERR("exceptions bootstrapping error."); \ |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2542 | } \ |
| 2543 | Py_INCREF(PyExc_ ## TYPE); \ |
| 2544 | } |
| 2545 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2546 | #define ADD_ERRNO(TYPE, CODE) \ |
| 2547 | do { \ |
| 2548 | PyObject *_code = PyLong_FromLong(CODE); \ |
| 2549 | assert(_PyObject_RealIsSubclass(PyExc_ ## TYPE, PyExc_OSError)); \ |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2550 | if (!_code || PyDict_SetItem(state->errnomap, _code, PyExc_ ## TYPE)) \ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2551 | return _PyStatus_ERR("errmap insertion problem."); \ |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2552 | Py_DECREF(_code); \ |
| 2553 | } while (0) |
| 2554 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2555 | PRE_INIT(BaseException); |
| 2556 | PRE_INIT(Exception); |
| 2557 | PRE_INIT(TypeError); |
| 2558 | PRE_INIT(StopAsyncIteration); |
| 2559 | PRE_INIT(StopIteration); |
| 2560 | PRE_INIT(GeneratorExit); |
| 2561 | PRE_INIT(SystemExit); |
| 2562 | PRE_INIT(KeyboardInterrupt); |
| 2563 | PRE_INIT(ImportError); |
| 2564 | PRE_INIT(ModuleNotFoundError); |
| 2565 | PRE_INIT(OSError); |
| 2566 | PRE_INIT(EOFError); |
| 2567 | PRE_INIT(RuntimeError); |
| 2568 | PRE_INIT(RecursionError); |
| 2569 | PRE_INIT(NotImplementedError); |
| 2570 | PRE_INIT(NameError); |
| 2571 | PRE_INIT(UnboundLocalError); |
| 2572 | PRE_INIT(AttributeError); |
| 2573 | PRE_INIT(SyntaxError); |
| 2574 | PRE_INIT(IndentationError); |
| 2575 | PRE_INIT(TabError); |
| 2576 | PRE_INIT(LookupError); |
| 2577 | PRE_INIT(IndexError); |
| 2578 | PRE_INIT(KeyError); |
| 2579 | PRE_INIT(ValueError); |
| 2580 | PRE_INIT(UnicodeError); |
| 2581 | PRE_INIT(UnicodeEncodeError); |
| 2582 | PRE_INIT(UnicodeDecodeError); |
| 2583 | PRE_INIT(UnicodeTranslateError); |
| 2584 | PRE_INIT(AssertionError); |
| 2585 | PRE_INIT(ArithmeticError); |
| 2586 | PRE_INIT(FloatingPointError); |
| 2587 | PRE_INIT(OverflowError); |
| 2588 | PRE_INIT(ZeroDivisionError); |
| 2589 | PRE_INIT(SystemError); |
| 2590 | PRE_INIT(ReferenceError); |
| 2591 | PRE_INIT(MemoryError); |
| 2592 | PRE_INIT(BufferError); |
| 2593 | PRE_INIT(Warning); |
| 2594 | PRE_INIT(UserWarning); |
| 2595 | PRE_INIT(DeprecationWarning); |
| 2596 | PRE_INIT(PendingDeprecationWarning); |
| 2597 | PRE_INIT(SyntaxWarning); |
| 2598 | PRE_INIT(RuntimeWarning); |
| 2599 | PRE_INIT(FutureWarning); |
| 2600 | PRE_INIT(ImportWarning); |
| 2601 | PRE_INIT(UnicodeWarning); |
| 2602 | PRE_INIT(BytesWarning); |
| 2603 | PRE_INIT(ResourceWarning); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2605 | /* OSError subclasses */ |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2606 | PRE_INIT(ConnectionError); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2607 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2608 | PRE_INIT(BlockingIOError); |
| 2609 | PRE_INIT(BrokenPipeError); |
| 2610 | PRE_INIT(ChildProcessError); |
| 2611 | PRE_INIT(ConnectionAbortedError); |
| 2612 | PRE_INIT(ConnectionRefusedError); |
| 2613 | PRE_INIT(ConnectionResetError); |
| 2614 | PRE_INIT(FileExistsError); |
| 2615 | PRE_INIT(FileNotFoundError); |
| 2616 | PRE_INIT(IsADirectoryError); |
| 2617 | PRE_INIT(NotADirectoryError); |
| 2618 | PRE_INIT(InterruptedError); |
| 2619 | PRE_INIT(PermissionError); |
| 2620 | PRE_INIT(ProcessLookupError); |
| 2621 | PRE_INIT(TimeoutError); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2622 | |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 2623 | if (preallocate_memerrors() < 0) { |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2624 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | /* Add exceptions to errnomap */ |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2628 | assert(state->errnomap == NULL); |
| 2629 | state->errnomap = PyDict_New(); |
| 2630 | if (!state->errnomap) { |
| 2631 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | ADD_ERRNO(BlockingIOError, EAGAIN); |
| 2635 | ADD_ERRNO(BlockingIOError, EALREADY); |
| 2636 | ADD_ERRNO(BlockingIOError, EINPROGRESS); |
| 2637 | ADD_ERRNO(BlockingIOError, EWOULDBLOCK); |
| 2638 | ADD_ERRNO(BrokenPipeError, EPIPE); |
| 2639 | #ifdef ESHUTDOWN |
| 2640 | ADD_ERRNO(BrokenPipeError, ESHUTDOWN); |
| 2641 | #endif |
| 2642 | ADD_ERRNO(ChildProcessError, ECHILD); |
| 2643 | ADD_ERRNO(ConnectionAbortedError, ECONNABORTED); |
| 2644 | ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED); |
| 2645 | ADD_ERRNO(ConnectionResetError, ECONNRESET); |
| 2646 | ADD_ERRNO(FileExistsError, EEXIST); |
| 2647 | ADD_ERRNO(FileNotFoundError, ENOENT); |
| 2648 | ADD_ERRNO(IsADirectoryError, EISDIR); |
| 2649 | ADD_ERRNO(NotADirectoryError, ENOTDIR); |
| 2650 | ADD_ERRNO(InterruptedError, EINTR); |
| 2651 | ADD_ERRNO(PermissionError, EACCES); |
| 2652 | ADD_ERRNO(PermissionError, EPERM); |
| 2653 | ADD_ERRNO(ProcessLookupError, ESRCH); |
| 2654 | ADD_ERRNO(TimeoutError, ETIMEDOUT); |
| 2655 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2656 | return _PyStatus_OK(); |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 2657 | |
| 2658 | #undef PRE_INIT |
| 2659 | #undef ADD_ERRNO |
| 2660 | } |
| 2661 | |
| 2662 | |
| 2663 | /* Add exception types to the builtins module */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2664 | PyStatus |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 2665 | _PyBuiltins_AddExceptions(PyObject *bltinmod) |
| 2666 | { |
| 2667 | #define POST_INIT(TYPE) \ |
| 2668 | if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) { \ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2669 | return _PyStatus_ERR("Module dictionary insertion problem."); \ |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | #define INIT_ALIAS(NAME, TYPE) \ |
| 2673 | do { \ |
| 2674 | Py_INCREF(PyExc_ ## TYPE); \ |
| 2675 | Py_XDECREF(PyExc_ ## NAME); \ |
| 2676 | PyExc_ ## NAME = PyExc_ ## TYPE; \ |
| 2677 | if (PyDict_SetItemString(bdict, # NAME, PyExc_ ## NAME)) { \ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2678 | return _PyStatus_ERR("Module dictionary insertion problem."); \ |
Victor Stinner | 6d43f6f | 2019-01-22 21:18:05 +0100 | [diff] [blame] | 2679 | } \ |
| 2680 | } while (0) |
| 2681 | |
| 2682 | PyObject *bdict; |
| 2683 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2684 | bdict = PyModule_GetDict(bltinmod); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2685 | if (bdict == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2686 | return _PyStatus_ERR("exceptions bootstrapping error."); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2687 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2688 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2689 | POST_INIT(BaseException); |
| 2690 | POST_INIT(Exception); |
| 2691 | POST_INIT(TypeError); |
| 2692 | POST_INIT(StopAsyncIteration); |
| 2693 | POST_INIT(StopIteration); |
| 2694 | POST_INIT(GeneratorExit); |
| 2695 | POST_INIT(SystemExit); |
| 2696 | POST_INIT(KeyboardInterrupt); |
| 2697 | POST_INIT(ImportError); |
| 2698 | POST_INIT(ModuleNotFoundError); |
| 2699 | POST_INIT(OSError); |
| 2700 | INIT_ALIAS(EnvironmentError, OSError); |
| 2701 | INIT_ALIAS(IOError, OSError); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2702 | #ifdef MS_WINDOWS |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2703 | INIT_ALIAS(WindowsError, OSError); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2704 | #endif |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2705 | POST_INIT(EOFError); |
| 2706 | POST_INIT(RuntimeError); |
| 2707 | POST_INIT(RecursionError); |
| 2708 | POST_INIT(NotImplementedError); |
| 2709 | POST_INIT(NameError); |
| 2710 | POST_INIT(UnboundLocalError); |
| 2711 | POST_INIT(AttributeError); |
| 2712 | POST_INIT(SyntaxError); |
| 2713 | POST_INIT(IndentationError); |
| 2714 | POST_INIT(TabError); |
| 2715 | POST_INIT(LookupError); |
| 2716 | POST_INIT(IndexError); |
| 2717 | POST_INIT(KeyError); |
| 2718 | POST_INIT(ValueError); |
| 2719 | POST_INIT(UnicodeError); |
| 2720 | POST_INIT(UnicodeEncodeError); |
| 2721 | POST_INIT(UnicodeDecodeError); |
| 2722 | POST_INIT(UnicodeTranslateError); |
| 2723 | POST_INIT(AssertionError); |
| 2724 | POST_INIT(ArithmeticError); |
| 2725 | POST_INIT(FloatingPointError); |
| 2726 | POST_INIT(OverflowError); |
| 2727 | POST_INIT(ZeroDivisionError); |
| 2728 | POST_INIT(SystemError); |
| 2729 | POST_INIT(ReferenceError); |
| 2730 | POST_INIT(MemoryError); |
| 2731 | POST_INIT(BufferError); |
| 2732 | POST_INIT(Warning); |
| 2733 | POST_INIT(UserWarning); |
| 2734 | POST_INIT(DeprecationWarning); |
| 2735 | POST_INIT(PendingDeprecationWarning); |
| 2736 | POST_INIT(SyntaxWarning); |
| 2737 | POST_INIT(RuntimeWarning); |
| 2738 | POST_INIT(FutureWarning); |
| 2739 | POST_INIT(ImportWarning); |
| 2740 | POST_INIT(UnicodeWarning); |
| 2741 | POST_INIT(BytesWarning); |
| 2742 | POST_INIT(ResourceWarning); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2743 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2744 | /* OSError subclasses */ |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2745 | POST_INIT(ConnectionError); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2746 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2747 | POST_INIT(BlockingIOError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2748 | POST_INIT(BrokenPipeError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2749 | POST_INIT(ChildProcessError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2750 | POST_INIT(ConnectionAbortedError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2751 | POST_INIT(ConnectionRefusedError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2752 | POST_INIT(ConnectionResetError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2753 | POST_INIT(FileExistsError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2754 | POST_INIT(FileNotFoundError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2755 | POST_INIT(IsADirectoryError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2756 | POST_INIT(NotADirectoryError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2757 | POST_INIT(InterruptedError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2758 | POST_INIT(PermissionError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2759 | POST_INIT(ProcessLookupError); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2760 | POST_INIT(TimeoutError); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2761 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 2762 | return _PyStatus_OK(); |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2763 | |
Victor Stinner | bf4ac2d | 2019-01-22 17:39:03 +0100 | [diff] [blame] | 2764 | #undef POST_INIT |
| 2765 | #undef INIT_ALIAS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | void |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2769 | _PyExc_Fini(PyThreadState *tstate) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2770 | { |
Victor Stinner | 281cce1 | 2020-06-23 22:55:46 +0200 | [diff] [blame] | 2771 | struct _Py_exc_state *state = &tstate->interp->exc_state; |
| 2772 | free_preallocated_memerrors(state); |
| 2773 | Py_CLEAR(state->errnomap); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2774 | } |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2775 | |
| 2776 | /* Helper to do the equivalent of "raise X from Y" in C, but always using |
| 2777 | * the current exception rather than passing one in. |
| 2778 | * |
| 2779 | * We currently limit this to *only* exceptions that use the BaseException |
| 2780 | * tp_init and tp_new methods, since we can be reasonably sure we can wrap |
| 2781 | * those correctly without losing data and without losing backwards |
| 2782 | * compatibility. |
| 2783 | * |
| 2784 | * We also aim to rule out *all* exceptions that might be storing additional |
| 2785 | * state, whether by having a size difference relative to BaseException, |
| 2786 | * additional arguments passed in during construction or by having a |
| 2787 | * non-empty instance dict. |
| 2788 | * |
| 2789 | * We need to be very careful with what we wrap, since changing types to |
| 2790 | * a broader exception type would be backwards incompatible for |
| 2791 | * existing codecs, and with different init or new method implementations |
| 2792 | * may either not support instantiation with PyErr_Format or lose |
| 2793 | * information when instantiated that way. |
| 2794 | * |
| 2795 | * XXX (ncoghlan): This could be made more comprehensive by exploiting the |
| 2796 | * fact that exceptions are expected to support pickling. If more builtin |
| 2797 | * exceptions (e.g. AttributeError) start to be converted to rich |
| 2798 | * exceptions with additional attributes, that's probably a better approach |
| 2799 | * to pursue over adding special cases for particular stateful subclasses. |
| 2800 | * |
| 2801 | * Returns a borrowed reference to the new exception (if any), NULL if the |
| 2802 | * existing exception was left in place. |
| 2803 | */ |
| 2804 | PyObject * |
| 2805 | _PyErr_TrySetFromCause(const char *format, ...) |
| 2806 | { |
| 2807 | PyObject* msg_prefix; |
| 2808 | PyObject *exc, *val, *tb; |
| 2809 | PyTypeObject *caught_type; |
Christian Heimes | 6a3db25 | 2013-11-14 01:47:14 +0100 | [diff] [blame] | 2810 | PyObject **dictptr; |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2811 | PyObject *instance_args; |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2812 | Py_ssize_t num_args, caught_type_size, base_exc_size; |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2813 | PyObject *new_exc, *new_val, *new_tb; |
| 2814 | va_list vargs; |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2815 | int same_basic_size; |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2816 | |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2817 | PyErr_Fetch(&exc, &val, &tb); |
Benjamin Peterson | 079c998 | 2013-11-13 23:25:01 -0500 | [diff] [blame] | 2818 | caught_type = (PyTypeObject *)exc; |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2819 | /* Ensure type info indicates no extra state is stored at the C level |
| 2820 | * and that the type can be reinstantiated using PyErr_Format |
| 2821 | */ |
| 2822 | caught_type_size = caught_type->tp_basicsize; |
| 2823 | base_exc_size = _PyExc_BaseException.tp_basicsize; |
| 2824 | same_basic_size = ( |
| 2825 | caught_type_size == base_exc_size || |
| 2826 | (PyType_SUPPORTS_WEAKREFS(caught_type) && |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 2827 | (caught_type_size == base_exc_size + (Py_ssize_t)sizeof(PyObject *)) |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2828 | ) |
| 2829 | ); |
Benjamin Peterson | 079c998 | 2013-11-13 23:25:01 -0500 | [diff] [blame] | 2830 | if (caught_type->tp_init != (initproc)BaseException_init || |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2831 | caught_type->tp_new != BaseException_new || |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2832 | !same_basic_size || |
Benjamin Peterson | 079c998 | 2013-11-13 23:25:01 -0500 | [diff] [blame] | 2833 | caught_type->tp_itemsize != _PyExc_BaseException.tp_itemsize) { |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2834 | /* We can't be sure we can wrap this safely, since it may contain |
| 2835 | * more state than just the exception type. Accordingly, we just |
| 2836 | * leave it alone. |
| 2837 | */ |
| 2838 | PyErr_Restore(exc, val, tb); |
| 2839 | return NULL; |
| 2840 | } |
| 2841 | |
| 2842 | /* Check the args are empty or contain a single string */ |
| 2843 | PyErr_NormalizeException(&exc, &val, &tb); |
Benjamin Peterson | 079c998 | 2013-11-13 23:25:01 -0500 | [diff] [blame] | 2844 | instance_args = ((PyBaseExceptionObject *)val)->args; |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2845 | num_args = PyTuple_GET_SIZE(instance_args); |
Benjamin Peterson | 079c998 | 2013-11-13 23:25:01 -0500 | [diff] [blame] | 2846 | if (num_args > 1 || |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2847 | (num_args == 1 && |
Benjamin Peterson | 079c998 | 2013-11-13 23:25:01 -0500 | [diff] [blame] | 2848 | !PyUnicode_CheckExact(PyTuple_GET_ITEM(instance_args, 0)))) { |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2849 | /* More than 1 arg, or the one arg we do have isn't a string |
| 2850 | */ |
| 2851 | PyErr_Restore(exc, val, tb); |
| 2852 | return NULL; |
| 2853 | } |
| 2854 | |
| 2855 | /* Ensure the instance dict is also empty */ |
Christian Heimes | 6a3db25 | 2013-11-14 01:47:14 +0100 | [diff] [blame] | 2856 | dictptr = _PyObject_GetDictPtr(val); |
Benjamin Peterson | 079c998 | 2013-11-13 23:25:01 -0500 | [diff] [blame] | 2857 | if (dictptr != NULL && *dictptr != NULL && |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 2858 | PyDict_GET_SIZE(*dictptr) > 0) { |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2859 | /* While we could potentially copy a non-empty instance dictionary |
| 2860 | * to the replacement exception, for now we take the more |
| 2861 | * conservative path of leaving exceptions with attributes set |
| 2862 | * alone. |
| 2863 | */ |
| 2864 | PyErr_Restore(exc, val, tb); |
| 2865 | return NULL; |
| 2866 | } |
| 2867 | |
| 2868 | /* For exceptions that we can wrap safely, we chain the original |
| 2869 | * exception to a new one of the exact same type with an |
| 2870 | * error message that mentions the additional details and the |
| 2871 | * original exception. |
| 2872 | * |
| 2873 | * It would be nice to wrap OSError and various other exception |
| 2874 | * types as well, but that's quite a bit trickier due to the extra |
| 2875 | * state potentially stored on OSError instances. |
| 2876 | */ |
Nick Coghlan | 77b286b | 2014-01-27 00:53:38 +1000 | [diff] [blame] | 2877 | /* Ensure the traceback is set correctly on the existing exception */ |
| 2878 | if (tb != NULL) { |
| 2879 | PyException_SetTraceback(val, tb); |
| 2880 | Py_DECREF(tb); |
| 2881 | } |
Benjamin Peterson | e109ee8 | 2013-11-13 23:49:49 -0500 | [diff] [blame] | 2882 | |
Christian Heimes | 507eabd | 2013-11-14 01:39:35 +0100 | [diff] [blame] | 2883 | #ifdef HAVE_STDARG_PROTOTYPES |
| 2884 | va_start(vargs, format); |
| 2885 | #else |
| 2886 | va_start(vargs); |
| 2887 | #endif |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2888 | msg_prefix = PyUnicode_FromFormatV(format, vargs); |
Christian Heimes | 507eabd | 2013-11-14 01:39:35 +0100 | [diff] [blame] | 2889 | va_end(vargs); |
Benjamin Peterson | e109ee8 | 2013-11-13 23:49:49 -0500 | [diff] [blame] | 2890 | if (msg_prefix == NULL) { |
Nick Coghlan | 4b9b936 | 2013-11-16 00:34:13 +1000 | [diff] [blame] | 2891 | Py_DECREF(exc); |
Benjamin Peterson | e109ee8 | 2013-11-13 23:49:49 -0500 | [diff] [blame] | 2892 | Py_DECREF(val); |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2893 | return NULL; |
Benjamin Peterson | e109ee8 | 2013-11-13 23:49:49 -0500 | [diff] [blame] | 2894 | } |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2895 | |
| 2896 | PyErr_Format(exc, "%U (%s: %S)", |
| 2897 | msg_prefix, Py_TYPE(val)->tp_name, val); |
Nick Coghlan | 4b9b936 | 2013-11-16 00:34:13 +1000 | [diff] [blame] | 2898 | Py_DECREF(exc); |
Benjamin Peterson | e109ee8 | 2013-11-13 23:49:49 -0500 | [diff] [blame] | 2899 | Py_DECREF(msg_prefix); |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2900 | PyErr_Fetch(&new_exc, &new_val, &new_tb); |
| 2901 | PyErr_NormalizeException(&new_exc, &new_val, &new_tb); |
| 2902 | PyException_SetCause(new_val, val); |
| 2903 | PyErr_Restore(new_exc, new_val, new_tb); |
| 2904 | return new_val; |
| 2905 | } |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 2906 | |
| 2907 | |
| 2908 | /* To help with migration from Python 2, SyntaxError.__init__ applies some |
| 2909 | * heuristics to try to report a more meaningful exception when print and |
| 2910 | * exec are used like statements. |
| 2911 | * |
| 2912 | * The heuristics are currently expected to detect the following cases: |
| 2913 | * - top level statement |
| 2914 | * - statement in a nested suite |
| 2915 | * - trailing section of a one line complex statement |
| 2916 | * |
| 2917 | * They're currently known not to trigger: |
| 2918 | * - after a semi-colon |
| 2919 | * |
| 2920 | * The error message can be a bit odd in cases where the "arguments" are |
| 2921 | * completely illegal syntactically, but that isn't worth the hassle of |
| 2922 | * fixing. |
| 2923 | * |
| 2924 | * We also can't do anything about cases that are legal Python 3 syntax |
| 2925 | * but mean something entirely different from what they did in Python 2 |
| 2926 | * (omitting the arguments entirely, printing items preceded by a unary plus |
| 2927 | * or minus, using the stream redirection syntax). |
| 2928 | */ |
| 2929 | |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 2930 | |
| 2931 | // Static helper for setting legacy print error message |
| 2932 | static int |
| 2933 | _set_legacy_print_statement_msg(PySyntaxErrorObject *self, Py_ssize_t start) |
| 2934 | { |
Nitish Chandra | 43c0f1a | 2018-01-28 16:26:02 +0530 | [diff] [blame] | 2935 | // PRINT_OFFSET is to remove the `print ` prefix from the data. |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 2936 | const int PRINT_OFFSET = 6; |
Sanyam Khurana | d57f26c | 2018-01-20 08:42:22 +0530 | [diff] [blame] | 2937 | const int STRIP_BOTH = 2; |
Nitish Chandra | 43c0f1a | 2018-01-28 16:26:02 +0530 | [diff] [blame] | 2938 | Py_ssize_t start_pos = start + PRINT_OFFSET; |
| 2939 | Py_ssize_t text_len = PyUnicode_GET_LENGTH(self->text); |
| 2940 | Py_UCS4 semicolon = ';'; |
| 2941 | Py_ssize_t end_pos = PyUnicode_FindChar(self->text, semicolon, |
| 2942 | start_pos, text_len, 1); |
| 2943 | if (end_pos < -1) { |
| 2944 | return -1; |
| 2945 | } else if (end_pos == -1) { |
| 2946 | end_pos = text_len; |
Sanyam Khurana | d57f26c | 2018-01-20 08:42:22 +0530 | [diff] [blame] | 2947 | } |
Nitish Chandra | 43c0f1a | 2018-01-28 16:26:02 +0530 | [diff] [blame] | 2948 | |
| 2949 | PyObject *data = PyUnicode_Substring(self->text, start_pos, end_pos); |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 2950 | if (data == NULL) { |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 2951 | return -1; |
| 2952 | } |
Nitish Chandra | 43c0f1a | 2018-01-28 16:26:02 +0530 | [diff] [blame] | 2953 | |
| 2954 | PyObject *strip_sep_obj = PyUnicode_FromString(" \t\r\n"); |
| 2955 | if (strip_sep_obj == NULL) { |
| 2956 | Py_DECREF(data); |
| 2957 | return -1; |
| 2958 | } |
| 2959 | |
Sanyam Khurana | d57f26c | 2018-01-20 08:42:22 +0530 | [diff] [blame] | 2960 | PyObject *new_data = _PyUnicode_XStrip(data, STRIP_BOTH, strip_sep_obj); |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 2961 | Py_DECREF(data); |
| 2962 | Py_DECREF(strip_sep_obj); |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 2963 | if (new_data == NULL) { |
| 2964 | return -1; |
| 2965 | } |
| 2966 | // gets the modified text_len after stripping `print ` |
| 2967 | text_len = PyUnicode_GET_LENGTH(new_data); |
| 2968 | const char *maybe_end_arg = ""; |
| 2969 | if (text_len > 0 && PyUnicode_READ_CHAR(new_data, text_len-1) == ',') { |
| 2970 | maybe_end_arg = " end=\" \""; |
| 2971 | } |
| 2972 | PyObject *error_msg = PyUnicode_FromFormat( |
| 2973 | "Missing parentheses in call to 'print'. Did you mean print(%U%s)?", |
| 2974 | new_data, maybe_end_arg |
| 2975 | ); |
| 2976 | Py_DECREF(new_data); |
| 2977 | if (error_msg == NULL) |
| 2978 | return -1; |
| 2979 | |
| 2980 | Py_XSETREF(self->msg, error_msg); |
| 2981 | return 1; |
| 2982 | } |
| 2983 | |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 2984 | static int |
| 2985 | _check_for_legacy_statements(PySyntaxErrorObject *self, Py_ssize_t start) |
| 2986 | { |
| 2987 | /* Return values: |
| 2988 | * -1: an error occurred |
| 2989 | * 0: nothing happened |
| 2990 | * 1: the check triggered & the error message was changed |
| 2991 | */ |
| 2992 | static PyObject *print_prefix = NULL; |
| 2993 | static PyObject *exec_prefix = NULL; |
Zackery Spytz | a4b48f1 | 2018-10-12 02:20:59 -0600 | [diff] [blame] | 2994 | Py_ssize_t text_len = PyUnicode_GET_LENGTH(self->text), match; |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 2995 | int kind = PyUnicode_KIND(self->text); |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 2996 | const void *data = PyUnicode_DATA(self->text); |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 2997 | |
| 2998 | /* Ignore leading whitespace */ |
| 2999 | while (start < text_len) { |
| 3000 | Py_UCS4 ch = PyUnicode_READ(kind, data, start); |
| 3001 | if (!Py_UNICODE_ISSPACE(ch)) |
| 3002 | break; |
| 3003 | start++; |
| 3004 | } |
| 3005 | /* Checking against an empty or whitespace-only part of the string */ |
| 3006 | if (start == text_len) { |
| 3007 | return 0; |
| 3008 | } |
| 3009 | |
| 3010 | /* Check for legacy print statements */ |
| 3011 | if (print_prefix == NULL) { |
| 3012 | print_prefix = PyUnicode_InternFromString("print "); |
| 3013 | if (print_prefix == NULL) { |
| 3014 | return -1; |
| 3015 | } |
| 3016 | } |
Zackery Spytz | a4b48f1 | 2018-10-12 02:20:59 -0600 | [diff] [blame] | 3017 | match = PyUnicode_Tailmatch(self->text, print_prefix, |
| 3018 | start, text_len, -1); |
| 3019 | if (match == -1) { |
| 3020 | return -1; |
| 3021 | } |
| 3022 | if (match) { |
Sanyam Khurana | 3a7f035 | 2017-06-20 19:01:32 +0530 | [diff] [blame] | 3023 | return _set_legacy_print_statement_msg(self, start); |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | /* Check for legacy exec statements */ |
| 3027 | if (exec_prefix == NULL) { |
| 3028 | exec_prefix = PyUnicode_InternFromString("exec "); |
| 3029 | if (exec_prefix == NULL) { |
| 3030 | return -1; |
| 3031 | } |
| 3032 | } |
Zackery Spytz | a4b48f1 | 2018-10-12 02:20:59 -0600 | [diff] [blame] | 3033 | match = PyUnicode_Tailmatch(self->text, exec_prefix, start, text_len, -1); |
| 3034 | if (match == -1) { |
| 3035 | return -1; |
| 3036 | } |
| 3037 | if (match) { |
| 3038 | PyObject *msg = PyUnicode_FromString("Missing parentheses in call " |
| 3039 | "to 'exec'"); |
| 3040 | if (msg == NULL) { |
| 3041 | return -1; |
| 3042 | } |
| 3043 | Py_XSETREF(self->msg, msg); |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 3044 | return 1; |
| 3045 | } |
| 3046 | /* Fall back to the default error message */ |
| 3047 | return 0; |
| 3048 | } |
| 3049 | |
| 3050 | static int |
| 3051 | _report_missing_parentheses(PySyntaxErrorObject *self) |
| 3052 | { |
| 3053 | Py_UCS4 left_paren = 40; |
| 3054 | Py_ssize_t left_paren_index; |
| 3055 | Py_ssize_t text_len = PyUnicode_GET_LENGTH(self->text); |
| 3056 | int legacy_check_result = 0; |
| 3057 | |
| 3058 | /* Skip entirely if there is an opening parenthesis */ |
| 3059 | left_paren_index = PyUnicode_FindChar(self->text, left_paren, |
| 3060 | 0, text_len, 1); |
| 3061 | if (left_paren_index < -1) { |
| 3062 | return -1; |
| 3063 | } |
| 3064 | if (left_paren_index != -1) { |
| 3065 | /* Use default error message for any line with an opening paren */ |
| 3066 | return 0; |
| 3067 | } |
| 3068 | /* Handle the simple statement case */ |
| 3069 | legacy_check_result = _check_for_legacy_statements(self, 0); |
| 3070 | if (legacy_check_result < 0) { |
| 3071 | return -1; |
| 3072 | |
| 3073 | } |
| 3074 | if (legacy_check_result == 0) { |
| 3075 | /* Handle the one-line complex statement case */ |
| 3076 | Py_UCS4 colon = 58; |
| 3077 | Py_ssize_t colon_index; |
| 3078 | colon_index = PyUnicode_FindChar(self->text, colon, |
| 3079 | 0, text_len, 1); |
| 3080 | if (colon_index < -1) { |
| 3081 | return -1; |
| 3082 | } |
| 3083 | if (colon_index >= 0 && colon_index < text_len) { |
| 3084 | /* Check again, starting from just after the colon */ |
| 3085 | if (_check_for_legacy_statements(self, colon_index+1) < 0) { |
| 3086 | return -1; |
| 3087 | } |
| 3088 | } |
| 3089 | } |
| 3090 | return 0; |
| 3091 | } |