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