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