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