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