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