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