Coverage Report

Created: 2025-11-24 06:11

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