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