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