1
2 /* Error handling */
3
4 #include "Python.h"
5 #include "pycore_call.h" // _PyObject_CallNoArgs()
6 #include "pycore_initconfig.h" // _PyStatus_ERR()
7 #include "pycore_pyerrors.h" // _PyErr_Format()
8 #include "pycore_pystate.h" // _PyThreadState_GET()
9 #include "pycore_sysmodule.h" // _PySys_Audit()
10 #include "pycore_traceback.h" // _PyTraceBack_FromFrame()
11
12 #ifndef __STDC__
13 #ifndef MS_WINDOWS
14 extern char *strerror(int);
15 #endif
16 #endif
17
18 #include <ctype.h>
19 #ifdef MS_WINDOWS
20 # include <windows.h>
21 # include <winbase.h>
22 # include <stdlib.h> // _sys_nerr
23 #endif
24
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 _Py_IDENTIFIER(__main__);
31 _Py_IDENTIFIER(__module__);
32 _Py_IDENTIFIER(builtins);
33 _Py_IDENTIFIER(stderr);
34 _Py_IDENTIFIER(flush);
35
36 /* Forward declarations */
37 static PyObject *
38 _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
39 const char *format, va_list vargs);
40
41
42 void
_PyErr_Restore(PyThreadState * tstate,PyObject * type,PyObject * value,PyObject * traceback)43 _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
44 PyObject *traceback)
45 {
46 PyObject *oldtype, *oldvalue, *oldtraceback;
47
48 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
49 /* XXX Should never happen -- fatal error instead? */
50 /* Well, it could be None. */
51 Py_DECREF(traceback);
52 traceback = NULL;
53 }
54
55 /* Save these in locals to safeguard against recursive
56 invocation through Py_XDECREF */
57 oldtype = tstate->curexc_type;
58 oldvalue = tstate->curexc_value;
59 oldtraceback = tstate->curexc_traceback;
60
61 tstate->curexc_type = type;
62 tstate->curexc_value = value;
63 tstate->curexc_traceback = traceback;
64
65 Py_XDECREF(oldtype);
66 Py_XDECREF(oldvalue);
67 Py_XDECREF(oldtraceback);
68 }
69
70 void
PyErr_Restore(PyObject * type,PyObject * value,PyObject * traceback)71 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
72 {
73 PyThreadState *tstate = _PyThreadState_GET();
74 _PyErr_Restore(tstate, type, value, traceback);
75 }
76
77
78 _PyErr_StackItem *
_PyErr_GetTopmostException(PyThreadState * tstate)79 _PyErr_GetTopmostException(PyThreadState *tstate)
80 {
81 _PyErr_StackItem *exc_info = tstate->exc_info;
82 assert(exc_info);
83
84 while ((exc_info->exc_value == NULL || exc_info->exc_value == Py_None) &&
85 exc_info->previous_item != NULL)
86 {
87 assert(exc_info->exc_type == NULL || exc_info->exc_type == Py_None);
88 exc_info = exc_info->previous_item;
89 }
90 assert(exc_info->previous_item == NULL ||
91 (exc_info->exc_type != NULL && exc_info->exc_type != Py_None));
92 return exc_info;
93 }
94
95 static PyObject*
_PyErr_CreateException(PyObject * exception_type,PyObject * value)96 _PyErr_CreateException(PyObject *exception_type, PyObject *value)
97 {
98 PyObject *exc;
99
100 if (value == NULL || value == Py_None) {
101 exc = _PyObject_CallNoArgs(exception_type);
102 }
103 else if (PyTuple_Check(value)) {
104 exc = PyObject_Call(exception_type, value, NULL);
105 }
106 else {
107 exc = PyObject_CallOneArg(exception_type, value);
108 }
109
110 if (exc != NULL && !PyExceptionInstance_Check(exc)) {
111 PyErr_Format(PyExc_TypeError,
112 "calling %R should have returned an instance of "
113 "BaseException, not %s",
114 exception_type, Py_TYPE(exc)->tp_name);
115 Py_CLEAR(exc);
116 }
117
118 return exc;
119 }
120
121 void
_PyErr_SetObject(PyThreadState * tstate,PyObject * exception,PyObject * value)122 _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
123 {
124 PyObject *exc_value;
125 PyObject *tb = NULL;
126
127 if (exception != NULL &&
128 !PyExceptionClass_Check(exception)) {
129 _PyErr_Format(tstate, PyExc_SystemError,
130 "_PyErr_SetObject: "
131 "exception %R is not a BaseException subclass",
132 exception);
133 return;
134 }
135
136 Py_XINCREF(value);
137 exc_value = _PyErr_GetTopmostException(tstate)->exc_value;
138 if (exc_value != NULL && exc_value != Py_None) {
139 /* Implicit exception chaining */
140 Py_INCREF(exc_value);
141 if (value == NULL || !PyExceptionInstance_Check(value)) {
142 /* We must normalize the value right now */
143 PyObject *fixed_value;
144
145 /* Issue #23571: functions must not be called with an
146 exception set */
147 _PyErr_Clear(tstate);
148
149 fixed_value = _PyErr_CreateException(exception, value);
150 Py_XDECREF(value);
151 if (fixed_value == NULL) {
152 Py_DECREF(exc_value);
153 return;
154 }
155
156 value = fixed_value;
157 }
158
159 /* Avoid creating new reference cycles through the
160 context chain, while taking care not to hang on
161 pre-existing ones.
162 This is O(chain length) but context chains are
163 usually very short. Sensitive readers may try
164 to inline the call to PyException_GetContext. */
165 if (exc_value != value) {
166 PyObject *o = exc_value, *context;
167 PyObject *slow_o = o; /* Floyd's cycle detection algo */
168 int slow_update_toggle = 0;
169 while ((context = PyException_GetContext(o))) {
170 Py_DECREF(context);
171 if (context == value) {
172 PyException_SetContext(o, NULL);
173 break;
174 }
175 o = context;
176 if (o == slow_o) {
177 /* pre-existing cycle - all exceptions on the
178 path were visited and checked. */
179 break;
180 }
181 if (slow_update_toggle) {
182 slow_o = PyException_GetContext(slow_o);
183 Py_DECREF(slow_o);
184 }
185 slow_update_toggle = !slow_update_toggle;
186 }
187 PyException_SetContext(value, exc_value);
188 }
189 else {
190 Py_DECREF(exc_value);
191 }
192 }
193 if (value != NULL && PyExceptionInstance_Check(value))
194 tb = PyException_GetTraceback(value);
195 Py_XINCREF(exception);
196 _PyErr_Restore(tstate, exception, value, tb);
197 }
198
199 void
PyErr_SetObject(PyObject * exception,PyObject * value)200 PyErr_SetObject(PyObject *exception, PyObject *value)
201 {
202 PyThreadState *tstate = _PyThreadState_GET();
203 _PyErr_SetObject(tstate, exception, value);
204 }
205
206 /* Set a key error with the specified argument, wrapping it in a
207 * tuple automatically so that tuple keys are not unpacked as the
208 * exception arguments. */
209 void
_PyErr_SetKeyError(PyObject * arg)210 _PyErr_SetKeyError(PyObject *arg)
211 {
212 PyThreadState *tstate = _PyThreadState_GET();
213 PyObject *tup = PyTuple_Pack(1, arg);
214 if (!tup) {
215 /* caller will expect error to be set anyway */
216 return;
217 }
218 _PyErr_SetObject(tstate, PyExc_KeyError, tup);
219 Py_DECREF(tup);
220 }
221
222 void
_PyErr_SetNone(PyThreadState * tstate,PyObject * exception)223 _PyErr_SetNone(PyThreadState *tstate, PyObject *exception)
224 {
225 _PyErr_SetObject(tstate, exception, (PyObject *)NULL);
226 }
227
228
229 void
PyErr_SetNone(PyObject * exception)230 PyErr_SetNone(PyObject *exception)
231 {
232 PyThreadState *tstate = _PyThreadState_GET();
233 _PyErr_SetNone(tstate, exception);
234 }
235
236
237 void
_PyErr_SetString(PyThreadState * tstate,PyObject * exception,const char * string)238 _PyErr_SetString(PyThreadState *tstate, PyObject *exception,
239 const char *string)
240 {
241 PyObject *value = PyUnicode_FromString(string);
242 _PyErr_SetObject(tstate, exception, value);
243 Py_XDECREF(value);
244 }
245
246 void
PyErr_SetString(PyObject * exception,const char * string)247 PyErr_SetString(PyObject *exception, const char *string)
248 {
249 PyThreadState *tstate = _PyThreadState_GET();
250 _PyErr_SetString(tstate, exception, string);
251 }
252
253
254 PyObject* _Py_HOT_FUNCTION
PyErr_Occurred(void)255 PyErr_Occurred(void)
256 {
257 /* The caller must hold the GIL. */
258 assert(PyGILState_Check());
259
260 PyThreadState *tstate = _PyThreadState_GET();
261 return _PyErr_Occurred(tstate);
262 }
263
264
265 int
PyErr_GivenExceptionMatches(PyObject * err,PyObject * exc)266 PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
267 {
268 if (err == NULL || exc == NULL) {
269 /* maybe caused by "import exceptions" that failed early on */
270 return 0;
271 }
272 if (PyTuple_Check(exc)) {
273 Py_ssize_t i, n;
274 n = PyTuple_Size(exc);
275 for (i = 0; i < n; i++) {
276 /* Test recursively */
277 if (PyErr_GivenExceptionMatches(
278 err, PyTuple_GET_ITEM(exc, i)))
279 {
280 return 1;
281 }
282 }
283 return 0;
284 }
285 /* err might be an instance, so check its class. */
286 if (PyExceptionInstance_Check(err))
287 err = PyExceptionInstance_Class(err);
288
289 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
290 return PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
291 }
292
293 return err == exc;
294 }
295
296
297 int
_PyErr_ExceptionMatches(PyThreadState * tstate,PyObject * exc)298 _PyErr_ExceptionMatches(PyThreadState *tstate, PyObject *exc)
299 {
300 return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc);
301 }
302
303
304 int
PyErr_ExceptionMatches(PyObject * exc)305 PyErr_ExceptionMatches(PyObject *exc)
306 {
307 PyThreadState *tstate = _PyThreadState_GET();
308 return _PyErr_ExceptionMatches(tstate, exc);
309 }
310
311
312 #ifndef Py_NORMALIZE_RECURSION_LIMIT
313 #define Py_NORMALIZE_RECURSION_LIMIT 32
314 #endif
315
316 /* Used in many places to normalize a raised exception, including in
317 eval_code2(), do_raise(), and PyErr_Print()
318
319 XXX: should PyErr_NormalizeException() also call
320 PyException_SetTraceback() with the resulting value and tb?
321 */
322 void
_PyErr_NormalizeException(PyThreadState * tstate,PyObject ** exc,PyObject ** val,PyObject ** tb)323 _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
324 PyObject **val, PyObject **tb)
325 {
326 int recursion_depth = 0;
327 tstate->recursion_headroom++;
328 PyObject *type, *value, *initial_tb;
329
330 restart:
331 type = *exc;
332 if (type == NULL) {
333 /* There was no exception, so nothing to do. */
334 tstate->recursion_headroom--;
335 return;
336 }
337
338 value = *val;
339 /* If PyErr_SetNone() was used, the value will have been actually
340 set to NULL.
341 */
342 if (!value) {
343 value = Py_None;
344 Py_INCREF(value);
345 }
346
347 /* Normalize the exception so that if the type is a class, the
348 value will be an instance.
349 */
350 if (PyExceptionClass_Check(type)) {
351 PyObject *inclass = NULL;
352 int is_subclass = 0;
353
354 if (PyExceptionInstance_Check(value)) {
355 inclass = PyExceptionInstance_Class(value);
356 is_subclass = PyObject_IsSubclass(inclass, type);
357 if (is_subclass < 0) {
358 goto error;
359 }
360 }
361
362 /* If the value was not an instance, or is not an instance
363 whose class is (or is derived from) type, then use the
364 value as an argument to instantiation of the type
365 class.
366 */
367 if (!is_subclass) {
368 PyObject *fixed_value = _PyErr_CreateException(type, value);
369 if (fixed_value == NULL) {
370 goto error;
371 }
372 Py_DECREF(value);
373 value = fixed_value;
374 }
375 /* If the class of the instance doesn't exactly match the
376 class of the type, believe the instance.
377 */
378 else if (inclass != type) {
379 Py_INCREF(inclass);
380 Py_DECREF(type);
381 type = inclass;
382 }
383 }
384 *exc = type;
385 *val = value;
386 tstate->recursion_headroom--;
387 return;
388
389 error:
390 Py_DECREF(type);
391 Py_DECREF(value);
392 recursion_depth++;
393 if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) {
394 _PyErr_SetString(tstate, PyExc_RecursionError,
395 "maximum recursion depth exceeded "
396 "while normalizing an exception");
397 }
398 /* If the new exception doesn't set a traceback and the old
399 exception had a traceback, use the old traceback for the
400 new exception. It's better than nothing.
401 */
402 initial_tb = *tb;
403 _PyErr_Fetch(tstate, exc, val, tb);
404 assert(*exc != NULL);
405 if (initial_tb != NULL) {
406 if (*tb == NULL)
407 *tb = initial_tb;
408 else
409 Py_DECREF(initial_tb);
410 }
411 /* Abort when Py_NORMALIZE_RECURSION_LIMIT has been exceeded, and the
412 corresponding RecursionError could not be normalized, and the
413 MemoryError raised when normalize this RecursionError could not be
414 normalized. */
415 if (recursion_depth >= Py_NORMALIZE_RECURSION_LIMIT + 2) {
416 if (PyErr_GivenExceptionMatches(*exc, PyExc_MemoryError)) {
417 Py_FatalError("Cannot recover from MemoryErrors "
418 "while normalizing exceptions.");
419 }
420 else {
421 Py_FatalError("Cannot recover from the recursive normalization "
422 "of an exception.");
423 }
424 }
425 goto restart;
426 }
427
428
429 void
PyErr_NormalizeException(PyObject ** exc,PyObject ** val,PyObject ** tb)430 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
431 {
432 PyThreadState *tstate = _PyThreadState_GET();
433 _PyErr_NormalizeException(tstate, exc, val, tb);
434 }
435
436
437 void
_PyErr_Fetch(PyThreadState * tstate,PyObject ** p_type,PyObject ** p_value,PyObject ** p_traceback)438 _PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value,
439 PyObject **p_traceback)
440 {
441 *p_type = tstate->curexc_type;
442 *p_value = tstate->curexc_value;
443 *p_traceback = tstate->curexc_traceback;
444
445 tstate->curexc_type = NULL;
446 tstate->curexc_value = NULL;
447 tstate->curexc_traceback = NULL;
448 }
449
450
451 void
PyErr_Fetch(PyObject ** p_type,PyObject ** p_value,PyObject ** p_traceback)452 PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
453 {
454 PyThreadState *tstate = _PyThreadState_GET();
455 _PyErr_Fetch(tstate, p_type, p_value, p_traceback);
456 }
457
458
459 void
_PyErr_Clear(PyThreadState * tstate)460 _PyErr_Clear(PyThreadState *tstate)
461 {
462 _PyErr_Restore(tstate, NULL, NULL, NULL);
463 }
464
465
466 void
PyErr_Clear(void)467 PyErr_Clear(void)
468 {
469 PyThreadState *tstate = _PyThreadState_GET();
470 _PyErr_Clear(tstate);
471 }
472
473 static PyObject*
get_exc_type(PyObject * exc_value)474 get_exc_type(PyObject *exc_value) /* returns a borrowed ref */
475 {
476 if (exc_value == NULL || exc_value == Py_None) {
477 return Py_None;
478 }
479 else {
480 assert(PyExceptionInstance_Check(exc_value));
481 PyObject *type = PyExceptionInstance_Class(exc_value);
482 assert(type != NULL);
483 return type;
484 }
485 }
486
487 static PyObject*
get_exc_traceback(PyObject * exc_value)488 get_exc_traceback(PyObject *exc_value) /* returns a borrowed ref */
489 {
490 if (exc_value == NULL || exc_value == Py_None) {
491 return Py_None;
492 }
493 else {
494 assert(PyExceptionInstance_Check(exc_value));
495 PyObject *tb = PyException_GetTraceback(exc_value);
496 Py_XDECREF(tb);
497 return tb ? tb : Py_None;
498 }
499 }
500
501 void
_PyErr_GetExcInfo(PyThreadState * tstate,PyObject ** p_type,PyObject ** p_value,PyObject ** p_traceback)502 _PyErr_GetExcInfo(PyThreadState *tstate,
503 PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
504 {
505 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
506
507 *p_type = get_exc_type(exc_info->exc_value);
508 *p_value = exc_info->exc_value;
509 *p_traceback = get_exc_traceback(exc_info->exc_value);
510
511 Py_XINCREF(*p_type);
512 Py_XINCREF(*p_value);
513 Py_XINCREF(*p_traceback);
514 }
515
516
517 void
PyErr_GetExcInfo(PyObject ** p_type,PyObject ** p_value,PyObject ** p_traceback)518 PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
519 {
520 PyThreadState *tstate = _PyThreadState_GET();
521 _PyErr_GetExcInfo(tstate, p_type, p_value, p_traceback);
522 }
523
524 void
PyErr_SetExcInfo(PyObject * type,PyObject * value,PyObject * traceback)525 PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
526 {
527 PyObject *oldtype, *oldvalue, *oldtraceback;
528 PyThreadState *tstate = _PyThreadState_GET();
529
530 oldtype = tstate->exc_info->exc_type;
531 oldvalue = tstate->exc_info->exc_value;
532 oldtraceback = tstate->exc_info->exc_traceback;
533
534
535 tstate->exc_info->exc_type = get_exc_type(value);
536 Py_XINCREF(tstate->exc_info->exc_type);
537 tstate->exc_info->exc_value = value;
538 tstate->exc_info->exc_traceback = get_exc_traceback(value);
539 Py_XINCREF(tstate->exc_info->exc_traceback);
540
541 /* These args are no longer used, but we still need to steal a ref */
542 Py_XDECREF(type);
543 Py_XDECREF(traceback);
544
545 Py_XDECREF(oldtype);
546 Py_XDECREF(oldvalue);
547 Py_XDECREF(oldtraceback);
548 }
549
550
551 PyObject*
_PyErr_StackItemToExcInfoTuple(_PyErr_StackItem * err_info)552 _PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info)
553 {
554 PyObject *exc_value = err_info->exc_value;
555
556 assert(exc_value == NULL ||
557 exc_value == Py_None ||
558 PyExceptionInstance_Check(exc_value));
559
560 PyObject *exc_type = get_exc_type(exc_value);
561 PyObject *exc_traceback = get_exc_traceback(exc_value);
562
563 return Py_BuildValue(
564 "(OOO)",
565 exc_type ? exc_type : Py_None,
566 exc_value ? exc_value : Py_None,
567 exc_traceback ? exc_traceback : Py_None);
568 }
569
570
571 /* Like PyErr_Restore(), but if an exception is already set,
572 set the context associated with it.
573
574 The caller is responsible for ensuring that this call won't create
575 any cycles in the exception context chain. */
576 void
_PyErr_ChainExceptions(PyObject * typ,PyObject * val,PyObject * tb)577 _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb)
578 {
579 if (typ == NULL)
580 return;
581
582 PyThreadState *tstate = _PyThreadState_GET();
583
584 if (!PyExceptionClass_Check(typ)) {
585 _PyErr_Format(tstate, PyExc_SystemError,
586 "_PyErr_ChainExceptions: "
587 "exception %R is not a BaseException subclass",
588 typ);
589 return;
590 }
591
592 if (_PyErr_Occurred(tstate)) {
593 PyObject *typ2, *val2, *tb2;
594 _PyErr_Fetch(tstate, &typ2, &val2, &tb2);
595 _PyErr_NormalizeException(tstate, &typ, &val, &tb);
596 if (tb != NULL) {
597 PyException_SetTraceback(val, tb);
598 Py_DECREF(tb);
599 }
600 Py_DECREF(typ);
601 _PyErr_NormalizeException(tstate, &typ2, &val2, &tb2);
602 PyException_SetContext(val2, val);
603 _PyErr_Restore(tstate, typ2, val2, tb2);
604 }
605 else {
606 _PyErr_Restore(tstate, typ, val, tb);
607 }
608 }
609
610 /* Set the currently set exception's context to the given exception.
611
612 If the provided exc_info is NULL, then the current Python thread state's
613 exc_info will be used for the context instead.
614
615 This function can only be called when _PyErr_Occurred() is true.
616 Also, this function won't create any cycles in the exception context
617 chain to the extent that _PyErr_SetObject ensures this. */
618 void
_PyErr_ChainStackItem(_PyErr_StackItem * exc_info)619 _PyErr_ChainStackItem(_PyErr_StackItem *exc_info)
620 {
621 PyThreadState *tstate = _PyThreadState_GET();
622 assert(_PyErr_Occurred(tstate));
623
624 int exc_info_given;
625 if (exc_info == NULL) {
626 exc_info_given = 0;
627 exc_info = tstate->exc_info;
628 } else {
629 exc_info_given = 1;
630 }
631
632 assert( (exc_info->exc_type == NULL || exc_info->exc_type == Py_None) ==
633 (exc_info->exc_value == NULL || exc_info->exc_value == Py_None) );
634
635 if (exc_info->exc_value == NULL || exc_info->exc_value == Py_None) {
636 return;
637 }
638
639 _PyErr_StackItem *saved_exc_info;
640 if (exc_info_given) {
641 /* Temporarily set the thread state's exc_info since this is what
642 _PyErr_SetObject uses for implicit exception chaining. */
643 saved_exc_info = tstate->exc_info;
644 tstate->exc_info = exc_info;
645 }
646
647 PyObject *typ, *val, *tb;
648 _PyErr_Fetch(tstate, &typ, &val, &tb);
649
650 /* _PyErr_SetObject sets the context from PyThreadState. */
651 _PyErr_SetObject(tstate, typ, val);
652 Py_DECREF(typ); // since _PyErr_Occurred was true
653 Py_XDECREF(val);
654 Py_XDECREF(tb);
655
656 if (exc_info_given) {
657 tstate->exc_info = saved_exc_info;
658 }
659 }
660
661 static PyObject *
_PyErr_FormatVFromCause(PyThreadState * tstate,PyObject * exception,const char * format,va_list vargs)662 _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
663 const char *format, va_list vargs)
664 {
665 PyObject *exc, *val, *val2, *tb;
666
667 assert(_PyErr_Occurred(tstate));
668 _PyErr_Fetch(tstate, &exc, &val, &tb);
669 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
670 if (tb != NULL) {
671 PyException_SetTraceback(val, tb);
672 Py_DECREF(tb);
673 }
674 Py_DECREF(exc);
675 assert(!_PyErr_Occurred(tstate));
676
677 _PyErr_FormatV(tstate, exception, format, vargs);
678
679 _PyErr_Fetch(tstate, &exc, &val2, &tb);
680 _PyErr_NormalizeException(tstate, &exc, &val2, &tb);
681 Py_INCREF(val);
682 PyException_SetCause(val2, val);
683 PyException_SetContext(val2, val);
684 _PyErr_Restore(tstate, exc, val2, tb);
685
686 return NULL;
687 }
688
689 PyObject *
_PyErr_FormatFromCauseTstate(PyThreadState * tstate,PyObject * exception,const char * format,...)690 _PyErr_FormatFromCauseTstate(PyThreadState *tstate, PyObject *exception,
691 const char *format, ...)
692 {
693 va_list vargs;
694 #ifdef HAVE_STDARG_PROTOTYPES
695 va_start(vargs, format);
696 #else
697 va_start(vargs);
698 #endif
699 _PyErr_FormatVFromCause(tstate, exception, format, vargs);
700 va_end(vargs);
701 return NULL;
702 }
703
704 PyObject *
_PyErr_FormatFromCause(PyObject * exception,const char * format,...)705 _PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
706 {
707 PyThreadState *tstate = _PyThreadState_GET();
708 va_list vargs;
709 #ifdef HAVE_STDARG_PROTOTYPES
710 va_start(vargs, format);
711 #else
712 va_start(vargs);
713 #endif
714 _PyErr_FormatVFromCause(tstate, exception, format, vargs);
715 va_end(vargs);
716 return NULL;
717 }
718
719 /* Convenience functions to set a type error exception and return 0 */
720
721 int
PyErr_BadArgument(void)722 PyErr_BadArgument(void)
723 {
724 PyThreadState *tstate = _PyThreadState_GET();
725 _PyErr_SetString(tstate, PyExc_TypeError,
726 "bad argument type for built-in operation");
727 return 0;
728 }
729
730 PyObject *
_PyErr_NoMemory(PyThreadState * tstate)731 _PyErr_NoMemory(PyThreadState *tstate)
732 {
733 if (Py_IS_TYPE(PyExc_MemoryError, NULL)) {
734 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
735 initialized by _PyExc_Init() */
736 Py_FatalError("Out of memory and PyExc_MemoryError is not "
737 "initialized yet");
738 }
739 _PyErr_SetNone(tstate, PyExc_MemoryError);
740 return NULL;
741 }
742
743 PyObject *
PyErr_NoMemory(void)744 PyErr_NoMemory(void)
745 {
746 PyThreadState *tstate = _PyThreadState_GET();
747 return _PyErr_NoMemory(tstate);
748 }
749
750 PyObject *
PyErr_SetFromErrnoWithFilenameObject(PyObject * exc,PyObject * filenameObject)751 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
752 {
753 return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
754 }
755
756 PyObject *
PyErr_SetFromErrnoWithFilenameObjects(PyObject * exc,PyObject * filenameObject,PyObject * filenameObject2)757 PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
758 {
759 PyThreadState *tstate = _PyThreadState_GET();
760 PyObject *message;
761 PyObject *v, *args;
762 int i = errno;
763 #ifdef MS_WINDOWS
764 WCHAR *s_buf = NULL;
765 #endif /* Unix/Windows */
766
767 #ifdef EINTR
768 if (i == EINTR && PyErr_CheckSignals())
769 return NULL;
770 #endif
771
772 #ifndef MS_WINDOWS
773 if (i != 0) {
774 const char *s = strerror(i);
775 message = PyUnicode_DecodeLocale(s, "surrogateescape");
776 }
777 else {
778 /* Sometimes errno didn't get set */
779 message = PyUnicode_FromString("Error");
780 }
781 #else
782 if (i == 0)
783 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
784 else
785 {
786 /* Note that the Win32 errors do not lineup with the
787 errno error. So if the error is in the MSVC error
788 table, we use it, otherwise we assume it really _is_
789 a Win32 error code
790 */
791 if (i > 0 && i < _sys_nerr) {
792 message = PyUnicode_FromString(_sys_errlist[i]);
793 }
794 else {
795 int len = FormatMessageW(
796 FORMAT_MESSAGE_ALLOCATE_BUFFER |
797 FORMAT_MESSAGE_FROM_SYSTEM |
798 FORMAT_MESSAGE_IGNORE_INSERTS,
799 NULL, /* no message source */
800 i,
801 MAKELANGID(LANG_NEUTRAL,
802 SUBLANG_DEFAULT),
803 /* Default language */
804 (LPWSTR) &s_buf,
805 0, /* size not used */
806 NULL); /* no args */
807 if (len==0) {
808 /* Only ever seen this in out-of-mem
809 situations */
810 s_buf = NULL;
811 message = PyUnicode_FromFormat("Windows Error 0x%x", i);
812 } else {
813 /* remove trailing cr/lf and dots */
814 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
815 s_buf[--len] = L'\0';
816 message = PyUnicode_FromWideChar(s_buf, len);
817 }
818 }
819 }
820 #endif /* Unix/Windows */
821
822 if (message == NULL)
823 {
824 #ifdef MS_WINDOWS
825 LocalFree(s_buf);
826 #endif
827 return NULL;
828 }
829
830 if (filenameObject != NULL) {
831 if (filenameObject2 != NULL)
832 args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
833 else
834 args = Py_BuildValue("(iOO)", i, message, filenameObject);
835 } else {
836 assert(filenameObject2 == NULL);
837 args = Py_BuildValue("(iO)", i, message);
838 }
839 Py_DECREF(message);
840
841 if (args != NULL) {
842 v = PyObject_Call(exc, args, NULL);
843 Py_DECREF(args);
844 if (v != NULL) {
845 _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
846 Py_DECREF(v);
847 }
848 }
849 #ifdef MS_WINDOWS
850 LocalFree(s_buf);
851 #endif
852 return NULL;
853 }
854
855 PyObject *
PyErr_SetFromErrnoWithFilename(PyObject * exc,const char * filename)856 PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
857 {
858 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
859 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
860 Py_XDECREF(name);
861 return result;
862 }
863
864 PyObject *
PyErr_SetFromErrno(PyObject * exc)865 PyErr_SetFromErrno(PyObject *exc)
866 {
867 return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
868 }
869
870 #ifdef MS_WINDOWS
871 /* Windows specific error code handling */
PyErr_SetExcFromWindowsErrWithFilenameObject(PyObject * exc,int ierr,PyObject * filenameObject)872 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
873 PyObject *exc,
874 int ierr,
875 PyObject *filenameObject)
876 {
877 return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
878 filenameObject, NULL);
879 }
880
PyErr_SetExcFromWindowsErrWithFilenameObjects(PyObject * exc,int ierr,PyObject * filenameObject,PyObject * filenameObject2)881 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
882 PyObject *exc,
883 int ierr,
884 PyObject *filenameObject,
885 PyObject *filenameObject2)
886 {
887 PyThreadState *tstate = _PyThreadState_GET();
888 int len;
889 WCHAR *s_buf = NULL; /* Free via LocalFree */
890 PyObject *message;
891 PyObject *args, *v;
892
893 DWORD err = (DWORD)ierr;
894 if (err==0) {
895 err = GetLastError();
896 }
897
898 len = FormatMessageW(
899 /* Error API error */
900 FORMAT_MESSAGE_ALLOCATE_BUFFER |
901 FORMAT_MESSAGE_FROM_SYSTEM |
902 FORMAT_MESSAGE_IGNORE_INSERTS,
903 NULL, /* no message source */
904 err,
905 MAKELANGID(LANG_NEUTRAL,
906 SUBLANG_DEFAULT), /* Default language */
907 (LPWSTR) &s_buf,
908 0, /* size not used */
909 NULL); /* no args */
910 if (len==0) {
911 /* Only seen this in out of mem situations */
912 message = PyUnicode_FromFormat("Windows Error 0x%x", err);
913 s_buf = NULL;
914 } else {
915 /* remove trailing cr/lf and dots */
916 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
917 s_buf[--len] = L'\0';
918 message = PyUnicode_FromWideChar(s_buf, len);
919 }
920
921 if (message == NULL)
922 {
923 LocalFree(s_buf);
924 return NULL;
925 }
926
927 if (filenameObject == NULL) {
928 assert(filenameObject2 == NULL);
929 filenameObject = filenameObject2 = Py_None;
930 }
931 else if (filenameObject2 == NULL)
932 filenameObject2 = Py_None;
933 /* This is the constructor signature for OSError.
934 The POSIX translation will be figured out by the constructor. */
935 args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
936 Py_DECREF(message);
937
938 if (args != NULL) {
939 v = PyObject_Call(exc, args, NULL);
940 Py_DECREF(args);
941 if (v != NULL) {
942 _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
943 Py_DECREF(v);
944 }
945 }
946 LocalFree(s_buf);
947 return NULL;
948 }
949
PyErr_SetExcFromWindowsErrWithFilename(PyObject * exc,int ierr,const char * filename)950 PyObject *PyErr_SetExcFromWindowsErrWithFilename(
951 PyObject *exc,
952 int ierr,
953 const char *filename)
954 {
955 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
956 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
957 ierr,
958 name,
959 NULL);
960 Py_XDECREF(name);
961 return ret;
962 }
963
PyErr_SetExcFromWindowsErr(PyObject * exc,int ierr)964 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
965 {
966 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
967 }
968
PyErr_SetFromWindowsErr(int ierr)969 PyObject *PyErr_SetFromWindowsErr(int ierr)
970 {
971 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
972 ierr, NULL);
973 }
974
PyErr_SetFromWindowsErrWithFilename(int ierr,const char * filename)975 PyObject *PyErr_SetFromWindowsErrWithFilename(
976 int ierr,
977 const char *filename)
978 {
979 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
980 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
981 PyExc_OSError,
982 ierr, name, NULL);
983 Py_XDECREF(name);
984 return result;
985 }
986
987 #endif /* MS_WINDOWS */
988
989 PyObject *
PyErr_SetImportErrorSubclass(PyObject * exception,PyObject * msg,PyObject * name,PyObject * path)990 PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
991 PyObject *name, PyObject *path)
992 {
993 PyThreadState *tstate = _PyThreadState_GET();
994 int issubclass;
995 PyObject *kwargs, *error;
996
997 issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
998 if (issubclass < 0) {
999 return NULL;
1000 }
1001 else if (!issubclass) {
1002 _PyErr_SetString(tstate, PyExc_TypeError,
1003 "expected a subclass of ImportError");
1004 return NULL;
1005 }
1006
1007 if (msg == NULL) {
1008 _PyErr_SetString(tstate, PyExc_TypeError,
1009 "expected a message argument");
1010 return NULL;
1011 }
1012
1013 if (name == NULL) {
1014 name = Py_None;
1015 }
1016 if (path == NULL) {
1017 path = Py_None;
1018 }
1019
1020 kwargs = PyDict_New();
1021 if (kwargs == NULL) {
1022 return NULL;
1023 }
1024 if (PyDict_SetItemString(kwargs, "name", name) < 0) {
1025 goto done;
1026 }
1027 if (PyDict_SetItemString(kwargs, "path", path) < 0) {
1028 goto done;
1029 }
1030
1031 error = PyObject_VectorcallDict(exception, &msg, 1, kwargs);
1032 if (error != NULL) {
1033 _PyErr_SetObject(tstate, (PyObject *)Py_TYPE(error), error);
1034 Py_DECREF(error);
1035 }
1036
1037 done:
1038 Py_DECREF(kwargs);
1039 return NULL;
1040 }
1041
1042 PyObject *
PyErr_SetImportError(PyObject * msg,PyObject * name,PyObject * path)1043 PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
1044 {
1045 return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
1046 }
1047
1048 void
_PyErr_BadInternalCall(const char * filename,int lineno)1049 _PyErr_BadInternalCall(const char *filename, int lineno)
1050 {
1051 PyThreadState *tstate = _PyThreadState_GET();
1052 _PyErr_Format(tstate, PyExc_SystemError,
1053 "%s:%d: bad argument to internal function",
1054 filename, lineno);
1055 }
1056
1057 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
1058 export the entry point for existing object code: */
1059 #undef PyErr_BadInternalCall
1060 void
PyErr_BadInternalCall(void)1061 PyErr_BadInternalCall(void)
1062 {
1063 assert(0 && "bad argument to internal function");
1064 PyThreadState *tstate = _PyThreadState_GET();
1065 _PyErr_SetString(tstate, PyExc_SystemError,
1066 "bad argument to internal function");
1067 }
1068 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
1069
1070
1071 static PyObject *
_PyErr_FormatV(PyThreadState * tstate,PyObject * exception,const char * format,va_list vargs)1072 _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
1073 const char *format, va_list vargs)
1074 {
1075 PyObject* string;
1076
1077 /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
1078 exception set, it calls arbitrary Python code like PyObject_Repr() */
1079 _PyErr_Clear(tstate);
1080
1081 string = PyUnicode_FromFormatV(format, vargs);
1082
1083 _PyErr_SetObject(tstate, exception, string);
1084 Py_XDECREF(string);
1085 return NULL;
1086 }
1087
1088
1089 PyObject *
PyErr_FormatV(PyObject * exception,const char * format,va_list vargs)1090 PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
1091 {
1092 PyThreadState *tstate = _PyThreadState_GET();
1093 return _PyErr_FormatV(tstate, exception, format, vargs);
1094 }
1095
1096
1097 PyObject *
_PyErr_Format(PyThreadState * tstate,PyObject * exception,const char * format,...)1098 _PyErr_Format(PyThreadState *tstate, PyObject *exception,
1099 const char *format, ...)
1100 {
1101 va_list vargs;
1102 #ifdef HAVE_STDARG_PROTOTYPES
1103 va_start(vargs, format);
1104 #else
1105 va_start(vargs);
1106 #endif
1107 _PyErr_FormatV(tstate, exception, format, vargs);
1108 va_end(vargs);
1109 return NULL;
1110 }
1111
1112
1113 PyObject *
PyErr_Format(PyObject * exception,const char * format,...)1114 PyErr_Format(PyObject *exception, const char *format, ...)
1115 {
1116 PyThreadState *tstate = _PyThreadState_GET();
1117 va_list vargs;
1118 #ifdef HAVE_STDARG_PROTOTYPES
1119 va_start(vargs, format);
1120 #else
1121 va_start(vargs);
1122 #endif
1123 _PyErr_FormatV(tstate, exception, format, vargs);
1124 va_end(vargs);
1125 return NULL;
1126 }
1127
1128
1129 PyObject *
PyErr_NewException(const char * name,PyObject * base,PyObject * dict)1130 PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
1131 {
1132 PyThreadState *tstate = _PyThreadState_GET();
1133 PyObject *modulename = NULL;
1134 PyObject *mydict = NULL;
1135 PyObject *bases = NULL;
1136 PyObject *result = NULL;
1137
1138 const char *dot = strrchr(name, '.');
1139 if (dot == NULL) {
1140 _PyErr_SetString(tstate, PyExc_SystemError,
1141 "PyErr_NewException: name must be module.class");
1142 return NULL;
1143 }
1144 if (base == NULL) {
1145 base = PyExc_Exception;
1146 }
1147 if (dict == NULL) {
1148 dict = mydict = PyDict_New();
1149 if (dict == NULL)
1150 goto failure;
1151 }
1152
1153 int r = _PyDict_ContainsId(dict, &PyId___module__);
1154 if (r < 0) {
1155 goto failure;
1156 }
1157 if (r == 0) {
1158 modulename = PyUnicode_FromStringAndSize(name,
1159 (Py_ssize_t)(dot-name));
1160 if (modulename == NULL)
1161 goto failure;
1162 if (_PyDict_SetItemId(dict, &PyId___module__, modulename) != 0)
1163 goto failure;
1164 }
1165 if (PyTuple_Check(base)) {
1166 bases = base;
1167 /* INCREF as we create a new ref in the else branch */
1168 Py_INCREF(bases);
1169 } else {
1170 bases = PyTuple_Pack(1, base);
1171 if (bases == NULL)
1172 goto failure;
1173 }
1174 /* Create a real class. */
1175 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
1176 dot+1, bases, dict);
1177 failure:
1178 Py_XDECREF(bases);
1179 Py_XDECREF(mydict);
1180 Py_XDECREF(modulename);
1181 return result;
1182 }
1183
1184
1185 /* Create an exception with docstring */
1186 PyObject *
PyErr_NewExceptionWithDoc(const char * name,const char * doc,PyObject * base,PyObject * dict)1187 PyErr_NewExceptionWithDoc(const char *name, const char *doc,
1188 PyObject *base, PyObject *dict)
1189 {
1190 int result;
1191 PyObject *ret = NULL;
1192 PyObject *mydict = NULL; /* points to the dict only if we create it */
1193 PyObject *docobj;
1194
1195 if (dict == NULL) {
1196 dict = mydict = PyDict_New();
1197 if (dict == NULL) {
1198 return NULL;
1199 }
1200 }
1201
1202 if (doc != NULL) {
1203 docobj = PyUnicode_FromString(doc);
1204 if (docobj == NULL)
1205 goto failure;
1206 result = PyDict_SetItemString(dict, "__doc__", docobj);
1207 Py_DECREF(docobj);
1208 if (result < 0)
1209 goto failure;
1210 }
1211
1212 ret = PyErr_NewException(name, base, dict);
1213 failure:
1214 Py_XDECREF(mydict);
1215 return ret;
1216 }
1217
1218
1219 PyDoc_STRVAR(UnraisableHookArgs__doc__,
1220 "UnraisableHookArgs\n\
1221 \n\
1222 Type used to pass arguments to sys.unraisablehook.");
1223
1224 static PyTypeObject UnraisableHookArgsType;
1225
1226 static PyStructSequence_Field UnraisableHookArgs_fields[] = {
1227 {"exc_type", "Exception type"},
1228 {"exc_value", "Exception value"},
1229 {"exc_traceback", "Exception traceback"},
1230 {"err_msg", "Error message"},
1231 {"object", "Object causing the exception"},
1232 {0}
1233 };
1234
1235 static PyStructSequence_Desc UnraisableHookArgs_desc = {
1236 .name = "UnraisableHookArgs",
1237 .doc = UnraisableHookArgs__doc__,
1238 .fields = UnraisableHookArgs_fields,
1239 .n_in_sequence = 5
1240 };
1241
1242
1243 PyStatus
_PyErr_InitTypes(void)1244 _PyErr_InitTypes(void)
1245 {
1246 if (UnraisableHookArgsType.tp_name == NULL) {
1247 if (PyStructSequence_InitType2(&UnraisableHookArgsType,
1248 &UnraisableHookArgs_desc) < 0) {
1249 return _PyStatus_ERR("failed to initialize UnraisableHookArgs type");
1250 }
1251 }
1252 return _PyStatus_OK();
1253 }
1254
1255
1256 static PyObject *
make_unraisable_hook_args(PyThreadState * tstate,PyObject * exc_type,PyObject * exc_value,PyObject * exc_tb,PyObject * err_msg,PyObject * obj)1257 make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type,
1258 PyObject *exc_value, PyObject *exc_tb,
1259 PyObject *err_msg, PyObject *obj)
1260 {
1261 PyObject *args = PyStructSequence_New(&UnraisableHookArgsType);
1262 if (args == NULL) {
1263 return NULL;
1264 }
1265
1266 Py_ssize_t pos = 0;
1267 #define ADD_ITEM(exc_type) \
1268 do { \
1269 if (exc_type == NULL) { \
1270 exc_type = Py_None; \
1271 } \
1272 Py_INCREF(exc_type); \
1273 PyStructSequence_SET_ITEM(args, pos++, exc_type); \
1274 } while (0)
1275
1276
1277 ADD_ITEM(exc_type);
1278 ADD_ITEM(exc_value);
1279 ADD_ITEM(exc_tb);
1280 ADD_ITEM(err_msg);
1281 ADD_ITEM(obj);
1282 #undef ADD_ITEM
1283
1284 if (_PyErr_Occurred(tstate)) {
1285 Py_DECREF(args);
1286 return NULL;
1287 }
1288 return args;
1289 }
1290
1291
1292
1293 /* Default implementation of sys.unraisablehook.
1294
1295 It can be called to log the exception of a custom sys.unraisablehook.
1296
1297 Do nothing if sys.stderr attribute doesn't exist or is set to None. */
1298 static int
write_unraisable_exc_file(PyThreadState * tstate,PyObject * exc_type,PyObject * exc_value,PyObject * exc_tb,PyObject * err_msg,PyObject * obj,PyObject * file)1299 write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
1300 PyObject *exc_value, PyObject *exc_tb,
1301 PyObject *err_msg, PyObject *obj, PyObject *file)
1302 {
1303 if (obj != NULL && obj != Py_None) {
1304 if (err_msg != NULL && err_msg != Py_None) {
1305 if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) {
1306 return -1;
1307 }
1308 if (PyFile_WriteString(": ", file) < 0) {
1309 return -1;
1310 }
1311 }
1312 else {
1313 if (PyFile_WriteString("Exception ignored in: ", file) < 0) {
1314 return -1;
1315 }
1316 }
1317
1318 if (PyFile_WriteObject(obj, file, 0) < 0) {
1319 _PyErr_Clear(tstate);
1320 if (PyFile_WriteString("<object repr() failed>", file) < 0) {
1321 return -1;
1322 }
1323 }
1324 if (PyFile_WriteString("\n", file) < 0) {
1325 return -1;
1326 }
1327 }
1328 else if (err_msg != NULL && err_msg != Py_None) {
1329 if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) {
1330 return -1;
1331 }
1332 if (PyFile_WriteString(":\n", file) < 0) {
1333 return -1;
1334 }
1335 }
1336
1337 if (exc_tb != NULL && exc_tb != Py_None) {
1338 if (PyTraceBack_Print(exc_tb, file) < 0) {
1339 /* continue even if writing the traceback failed */
1340 _PyErr_Clear(tstate);
1341 }
1342 }
1343
1344 if (exc_type == NULL || exc_type == Py_None) {
1345 return -1;
1346 }
1347
1348 assert(PyExceptionClass_Check(exc_type));
1349
1350 PyObject *modulename = _PyObject_GetAttrId(exc_type, &PyId___module__);
1351 if (modulename == NULL || !PyUnicode_Check(modulename)) {
1352 Py_XDECREF(modulename);
1353 _PyErr_Clear(tstate);
1354 if (PyFile_WriteString("<unknown>", file) < 0) {
1355 return -1;
1356 }
1357 }
1358 else {
1359 if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins) &&
1360 !_PyUnicode_EqualToASCIIId(modulename, &PyId___main__)) {
1361 if (PyFile_WriteObject(modulename, file, Py_PRINT_RAW) < 0) {
1362 Py_DECREF(modulename);
1363 return -1;
1364 }
1365 Py_DECREF(modulename);
1366 if (PyFile_WriteString(".", file) < 0) {
1367 return -1;
1368 }
1369 }
1370 else {
1371 Py_DECREF(modulename);
1372 }
1373 }
1374
1375 PyObject *qualname = PyType_GetQualName((PyTypeObject *)exc_type);
1376 if (qualname == NULL || !PyUnicode_Check(qualname)) {
1377 Py_XDECREF(qualname);
1378 _PyErr_Clear(tstate);
1379 if (PyFile_WriteString("<unknown>", file) < 0) {
1380 return -1;
1381 }
1382 }
1383 else {
1384 if (PyFile_WriteObject(qualname, file, Py_PRINT_RAW) < 0) {
1385 Py_DECREF(qualname);
1386 return -1;
1387 }
1388 Py_DECREF(qualname);
1389 }
1390
1391 if (exc_value && exc_value != Py_None) {
1392 if (PyFile_WriteString(": ", file) < 0) {
1393 return -1;
1394 }
1395 if (PyFile_WriteObject(exc_value, file, Py_PRINT_RAW) < 0) {
1396 _PyErr_Clear(tstate);
1397 if (PyFile_WriteString("<exception str() failed>", file) < 0) {
1398 return -1;
1399 }
1400 }
1401 }
1402
1403 if (PyFile_WriteString("\n", file) < 0) {
1404 return -1;
1405 }
1406
1407 /* Explicitly call file.flush() */
1408 PyObject *res = _PyObject_CallMethodIdNoArgs(file, &PyId_flush);
1409 if (!res) {
1410 return -1;
1411 }
1412 Py_DECREF(res);
1413
1414 return 0;
1415 }
1416
1417
1418 static int
write_unraisable_exc(PyThreadState * tstate,PyObject * exc_type,PyObject * exc_value,PyObject * exc_tb,PyObject * err_msg,PyObject * obj)1419 write_unraisable_exc(PyThreadState *tstate, PyObject *exc_type,
1420 PyObject *exc_value, PyObject *exc_tb, PyObject *err_msg,
1421 PyObject *obj)
1422 {
1423 PyObject *file = _PySys_GetObjectId(&PyId_stderr);
1424 if (file == NULL || file == Py_None) {
1425 return 0;
1426 }
1427
1428 /* Hold a strong reference to ensure that sys.stderr doesn't go away
1429 while we use it */
1430 Py_INCREF(file);
1431 int res = write_unraisable_exc_file(tstate, exc_type, exc_value, exc_tb,
1432 err_msg, obj, file);
1433 Py_DECREF(file);
1434
1435 return res;
1436 }
1437
1438
1439 PyObject*
_PyErr_WriteUnraisableDefaultHook(PyObject * args)1440 _PyErr_WriteUnraisableDefaultHook(PyObject *args)
1441 {
1442 PyThreadState *tstate = _PyThreadState_GET();
1443
1444 if (!Py_IS_TYPE(args, &UnraisableHookArgsType)) {
1445 _PyErr_SetString(tstate, PyExc_TypeError,
1446 "sys.unraisablehook argument type "
1447 "must be UnraisableHookArgs");
1448 return NULL;
1449 }
1450
1451 /* Borrowed references */
1452 PyObject *exc_type = PyStructSequence_GET_ITEM(args, 0);
1453 PyObject *exc_value = PyStructSequence_GET_ITEM(args, 1);
1454 PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
1455 PyObject *err_msg = PyStructSequence_GET_ITEM(args, 3);
1456 PyObject *obj = PyStructSequence_GET_ITEM(args, 4);
1457
1458 if (write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, err_msg, obj) < 0) {
1459 return NULL;
1460 }
1461 Py_RETURN_NONE;
1462 }
1463
1464
1465 /* Call sys.unraisablehook().
1466
1467 This function can be used when an exception has occurred but there is no way
1468 for Python to handle it. For example, when a destructor raises an exception
1469 or during garbage collection (gc.collect()).
1470
1471 If err_msg_str is non-NULL, the error message is formatted as:
1472 "Exception ignored %s" % err_msg_str. Otherwise, use "Exception ignored in"
1473 error message.
1474
1475 An exception must be set when calling this function. */
1476 void
_PyErr_WriteUnraisableMsg(const char * err_msg_str,PyObject * obj)1477 _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
1478 {
1479 PyThreadState *tstate = _PyThreadState_GET();
1480 _Py_EnsureTstateNotNULL(tstate);
1481
1482 PyObject *err_msg = NULL;
1483 PyObject *exc_type, *exc_value, *exc_tb;
1484 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
1485
1486 assert(exc_type != NULL);
1487
1488 if (exc_type == NULL) {
1489 /* sys.unraisablehook requires that at least exc_type is set */
1490 goto default_hook;
1491 }
1492
1493 if (exc_tb == NULL) {
1494 PyFrameObject *frame = PyThreadState_GetFrame(tstate);
1495 if (frame != NULL) {
1496 exc_tb = _PyTraceBack_FromFrame(NULL, frame);
1497 if (exc_tb == NULL) {
1498 _PyErr_Clear(tstate);
1499 }
1500 Py_DECREF(frame);
1501 }
1502 }
1503
1504 _PyErr_NormalizeException(tstate, &exc_type, &exc_value, &exc_tb);
1505
1506 if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) {
1507 if (PyException_SetTraceback(exc_value, exc_tb) < 0) {
1508 _PyErr_Clear(tstate);
1509 }
1510 }
1511
1512 if (err_msg_str != NULL) {
1513 err_msg = PyUnicode_FromFormat("Exception ignored %s", err_msg_str);
1514 if (err_msg == NULL) {
1515 PyErr_Clear();
1516 }
1517 }
1518
1519 PyObject *hook_args = make_unraisable_hook_args(
1520 tstate, exc_type, exc_value, exc_tb, err_msg, obj);
1521 if (hook_args == NULL) {
1522 err_msg_str = ("Exception ignored on building "
1523 "sys.unraisablehook arguments");
1524 goto error;
1525 }
1526
1527 _Py_IDENTIFIER(unraisablehook);
1528 PyObject *hook = _PySys_GetObjectId(&PyId_unraisablehook);
1529 if (hook == NULL) {
1530 Py_DECREF(hook_args);
1531 goto default_hook;
1532 }
1533
1534 if (_PySys_Audit(tstate, "sys.unraisablehook", "OO", hook, hook_args) < 0) {
1535 Py_DECREF(hook_args);
1536 err_msg_str = "Exception ignored in audit hook";
1537 obj = NULL;
1538 goto error;
1539 }
1540
1541 if (hook == Py_None) {
1542 Py_DECREF(hook_args);
1543 goto default_hook;
1544 }
1545
1546 PyObject *res = PyObject_CallOneArg(hook, hook_args);
1547 Py_DECREF(hook_args);
1548 if (res != NULL) {
1549 Py_DECREF(res);
1550 goto done;
1551 }
1552
1553 /* sys.unraisablehook failed: log its error using default hook */
1554 obj = hook;
1555 err_msg_str = NULL;
1556
1557 error:
1558 /* err_msg_str and obj have been updated and we have a new exception */
1559 Py_XSETREF(err_msg, PyUnicode_FromString(err_msg_str ?
1560 err_msg_str : "Exception ignored in sys.unraisablehook"));
1561 Py_XDECREF(exc_type);
1562 Py_XDECREF(exc_value);
1563 Py_XDECREF(exc_tb);
1564 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
1565
1566 default_hook:
1567 /* Call the default unraisable hook (ignore failure) */
1568 (void)write_unraisable_exc(tstate, exc_type, exc_value, exc_tb,
1569 err_msg, obj);
1570
1571 done:
1572 Py_XDECREF(exc_type);
1573 Py_XDECREF(exc_value);
1574 Py_XDECREF(exc_tb);
1575 Py_XDECREF(err_msg);
1576 _PyErr_Clear(tstate); /* Just in case */
1577 }
1578
1579
1580 void
PyErr_WriteUnraisable(PyObject * obj)1581 PyErr_WriteUnraisable(PyObject *obj)
1582 {
1583 _PyErr_WriteUnraisableMsg(NULL, obj);
1584 }
1585
1586
1587 void
PyErr_SyntaxLocation(const char * filename,int lineno)1588 PyErr_SyntaxLocation(const char *filename, int lineno)
1589 {
1590 PyErr_SyntaxLocationEx(filename, lineno, -1);
1591 }
1592
1593
1594 /* Set file and line information for the current exception.
1595 If the exception is not a SyntaxError, also sets additional attributes
1596 to make printing of exceptions believe it is a syntax error. */
1597
1598 static void
PyErr_SyntaxLocationObjectEx(PyObject * filename,int lineno,int col_offset,int end_lineno,int end_col_offset)1599 PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
1600 int end_lineno, int end_col_offset)
1601 {
1602 PyObject *exc, *v, *tb, *tmp;
1603 _Py_IDENTIFIER(filename);
1604 _Py_IDENTIFIER(lineno);
1605 _Py_IDENTIFIER(end_lineno);
1606 _Py_IDENTIFIER(msg);
1607 _Py_IDENTIFIER(offset);
1608 _Py_IDENTIFIER(end_offset);
1609 _Py_IDENTIFIER(print_file_and_line);
1610 _Py_IDENTIFIER(text);
1611 PyThreadState *tstate = _PyThreadState_GET();
1612
1613 /* add attributes for the line number and filename for the error */
1614 _PyErr_Fetch(tstate, &exc, &v, &tb);
1615 _PyErr_NormalizeException(tstate, &exc, &v, &tb);
1616 /* XXX check that it is, indeed, a syntax error. It might not
1617 * be, though. */
1618 tmp = PyLong_FromLong(lineno);
1619 if (tmp == NULL)
1620 _PyErr_Clear(tstate);
1621 else {
1622 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) {
1623 _PyErr_Clear(tstate);
1624 }
1625 Py_DECREF(tmp);
1626 }
1627 tmp = NULL;
1628 if (col_offset >= 0) {
1629 tmp = PyLong_FromLong(col_offset);
1630 if (tmp == NULL) {
1631 _PyErr_Clear(tstate);
1632 }
1633 }
1634 if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None)) {
1635 _PyErr_Clear(tstate);
1636 }
1637 Py_XDECREF(tmp);
1638
1639 tmp = NULL;
1640 if (end_lineno >= 0) {
1641 tmp = PyLong_FromLong(end_lineno);
1642 if (tmp == NULL) {
1643 _PyErr_Clear(tstate);
1644 }
1645 }
1646 if (_PyObject_SetAttrId(v, &PyId_end_lineno, tmp ? tmp : Py_None)) {
1647 _PyErr_Clear(tstate);
1648 }
1649 Py_XDECREF(tmp);
1650
1651 tmp = NULL;
1652 if (end_col_offset >= 0) {
1653 tmp = PyLong_FromLong(end_col_offset);
1654 if (tmp == NULL) {
1655 _PyErr_Clear(tstate);
1656 }
1657 }
1658 if (_PyObject_SetAttrId(v, &PyId_end_offset, tmp ? tmp : Py_None)) {
1659 _PyErr_Clear(tstate);
1660 }
1661 Py_XDECREF(tmp);
1662
1663 tmp = NULL;
1664 if (filename != NULL) {
1665 if (_PyObject_SetAttrId(v, &PyId_filename, filename)) {
1666 _PyErr_Clear(tstate);
1667 }
1668
1669 tmp = PyErr_ProgramTextObject(filename, lineno);
1670 if (tmp) {
1671 if (_PyObject_SetAttrId(v, &PyId_text, tmp)) {
1672 _PyErr_Clear(tstate);
1673 }
1674 Py_DECREF(tmp);
1675 }
1676 else {
1677 _PyErr_Clear(tstate);
1678 }
1679 }
1680 if (exc != PyExc_SyntaxError) {
1681 if (_PyObject_LookupAttrId(v, &PyId_msg, &tmp) < 0) {
1682 _PyErr_Clear(tstate);
1683 }
1684 else if (tmp) {
1685 Py_DECREF(tmp);
1686 }
1687 else {
1688 tmp = PyObject_Str(v);
1689 if (tmp) {
1690 if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) {
1691 _PyErr_Clear(tstate);
1692 }
1693 Py_DECREF(tmp);
1694 }
1695 else {
1696 _PyErr_Clear(tstate);
1697 }
1698 }
1699 if (_PyObject_LookupAttrId(v, &PyId_print_file_and_line, &tmp) < 0) {
1700 _PyErr_Clear(tstate);
1701 }
1702 else if (tmp) {
1703 Py_DECREF(tmp);
1704 }
1705 else {
1706 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
1707 Py_None)) {
1708 _PyErr_Clear(tstate);
1709 }
1710 }
1711 }
1712 _PyErr_Restore(tstate, exc, v, tb);
1713 }
1714
1715 void
PyErr_SyntaxLocationObject(PyObject * filename,int lineno,int col_offset)1716 PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset) {
1717 PyErr_SyntaxLocationObjectEx(filename, lineno, col_offset, lineno, -1);
1718 }
1719
1720 void
PyErr_RangedSyntaxLocationObject(PyObject * filename,int lineno,int col_offset,int end_lineno,int end_col_offset)1721 PyErr_RangedSyntaxLocationObject(PyObject *filename, int lineno, int col_offset,
1722 int end_lineno, int end_col_offset) {
1723 PyErr_SyntaxLocationObjectEx(filename, lineno, col_offset, end_lineno, end_col_offset);
1724 }
1725
1726 void
PyErr_SyntaxLocationEx(const char * filename,int lineno,int col_offset)1727 PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
1728 {
1729 PyThreadState *tstate = _PyThreadState_GET();
1730 PyObject *fileobj;
1731 if (filename != NULL) {
1732 fileobj = PyUnicode_DecodeFSDefault(filename);
1733 if (fileobj == NULL) {
1734 _PyErr_Clear(tstate);
1735 }
1736 }
1737 else {
1738 fileobj = NULL;
1739 }
1740 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
1741 Py_XDECREF(fileobj);
1742 }
1743
1744 /* Attempt to load the line of text that the exception refers to. If it
1745 fails, it will return NULL but will not set an exception.
1746
1747 XXX The functionality of this function is quite similar to the
1748 functionality in tb_displayline() in traceback.c. */
1749
1750 static PyObject *
err_programtext(PyThreadState * tstate,FILE * fp,int lineno,const char * encoding)1751 err_programtext(PyThreadState *tstate, FILE *fp, int lineno, const char* encoding)
1752 {
1753 int i;
1754 char linebuf[1000];
1755 if (fp == NULL) {
1756 return NULL;
1757 }
1758
1759 for (i = 0; i < lineno; i++) {
1760 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1761 do {
1762 *pLastChar = '\0';
1763 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1764 fp, NULL) == NULL) {
1765 goto after_loop;
1766 }
1767 /* fgets read *something*; if it didn't get as
1768 far as pLastChar, it must have found a newline
1769 or hit the end of the file; if pLastChar is \n,
1770 it obviously found a newline; else we haven't
1771 yet seen a newline, so must continue */
1772 } while (*pLastChar != '\0' && *pLastChar != '\n');
1773 }
1774
1775 after_loop:
1776 fclose(fp);
1777 if (i == lineno) {
1778 PyObject *res;
1779 if (encoding != NULL) {
1780 res = PyUnicode_Decode(linebuf, strlen(linebuf), encoding, "replace");
1781 } else {
1782 res = PyUnicode_FromString(linebuf);
1783 }
1784 if (res == NULL)
1785 _PyErr_Clear(tstate);
1786 return res;
1787 }
1788 return NULL;
1789 }
1790
1791 PyObject *
PyErr_ProgramText(const char * filename,int lineno)1792 PyErr_ProgramText(const char *filename, int lineno)
1793 {
1794 if (filename == NULL) {
1795 return NULL;
1796 }
1797
1798 PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
1799 if (filename_obj == NULL) {
1800 PyErr_Clear();
1801 return NULL;
1802 }
1803 PyObject *res = PyErr_ProgramTextObject(filename_obj, lineno);
1804 Py_DECREF(filename_obj);
1805 return res;
1806 }
1807
1808 PyObject *
_PyErr_ProgramDecodedTextObject(PyObject * filename,int lineno,const char * encoding)1809 _PyErr_ProgramDecodedTextObject(PyObject *filename, int lineno, const char* encoding)
1810 {
1811 if (filename == NULL || lineno <= 0) {
1812 return NULL;
1813 }
1814
1815 PyThreadState *tstate = _PyThreadState_GET();
1816 FILE *fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
1817 if (fp == NULL) {
1818 _PyErr_Clear(tstate);
1819 return NULL;
1820 }
1821 return err_programtext(tstate, fp, lineno, encoding);
1822 }
1823
1824 PyObject *
PyErr_ProgramTextObject(PyObject * filename,int lineno)1825 PyErr_ProgramTextObject(PyObject *filename, int lineno)
1826 {
1827 return _PyErr_ProgramDecodedTextObject(filename, lineno, NULL);
1828 }
1829
1830 #ifdef __cplusplus
1831 }
1832 #endif
1833