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