blob: e721f1915da407a907bff84ed34a4939966fbdad [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum373c8691997-04-29 18:22:47 +00002/* Error handling */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossum373c8691997-04-29 18:22:47 +00004#include "Python.h"
Victor Stinneref9d9b62019-05-22 11:28:22 +02005#include "pycore_coreconfig.h"
Victor Stinner438a12d2019-05-24 17:01:38 +02006#include "pycore_pyerrors.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pystate.h"
Victor Stinnerdf22c032019-05-23 01:00:58 +02008#include "pycore_traceback.h"
Guido van Rossumf22120a1990-12-20 23:05:40 +00009
Guido van Rossum53e8d441995-03-09 12:11:31 +000010#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +000011#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +000012extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +000013#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000014#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000015
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000016#ifdef MS_WINDOWS
Martin v. Löwis5d12abe2007-09-03 07:40:24 +000017#include <windows.h>
18#include <winbase.h>
Guido van Rossum743007d1999-04-21 15:27:31 +000019#endif
20
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000021#include <ctype.h>
22
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000023#ifdef __cplusplus
24extern "C" {
25#endif
26
Victor Stinnerbd303c12013-11-07 23:07:29 +010027_Py_IDENTIFIER(builtins);
28_Py_IDENTIFIER(stderr);
29
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030
Victor Stinner438a12d2019-05-24 17:01:38 +020031/* Forward declarations */
32static PyObject *
33_PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
34 const char *format, va_list vargs);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +020035
36
Victor Stinner438a12d2019-05-24 17:01:38 +020037void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +020038_PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
39 PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
44 /* XXX Should never happen -- fatal error instead? */
45 /* Well, it could be None. */
46 Py_DECREF(traceback);
47 traceback = NULL;
48 }
Guido van Rossuma027efa1997-05-05 20:56:21 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* Save these in locals to safeguard against recursive
51 invocation through Py_XDECREF */
52 oldtype = tstate->curexc_type;
53 oldvalue = tstate->curexc_value;
54 oldtraceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 tstate->curexc_type = type;
57 tstate->curexc_value = value;
58 tstate->curexc_traceback = traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 Py_XDECREF(oldtype);
61 Py_XDECREF(oldvalue);
62 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000063}
64
Victor Stinnerb4bdecd2019-05-24 13:44:24 +020065void
66PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
67{
68 PyThreadState *tstate = _PyThreadState_GET();
69 _PyErr_Restore(tstate, type, value, traceback);
70}
71
72
Mark Shannonae3087c2017-10-22 22:41:51 +010073_PyErr_StackItem *
74_PyErr_GetTopmostException(PyThreadState *tstate)
75{
76 _PyErr_StackItem *exc_info = tstate->exc_info;
77 while ((exc_info->exc_type == NULL || exc_info->exc_type == Py_None) &&
78 exc_info->previous_item != NULL)
79 {
80 exc_info = exc_info->previous_item;
81 }
82 return exc_info;
83}
84
Victor Stinner3a840972016-08-22 23:59:08 +020085static PyObject*
86_PyErr_CreateException(PyObject *exception, PyObject *value)
87{
88 if (value == NULL || value == Py_None) {
89 return _PyObject_CallNoArg(exception);
90 }
91 else if (PyTuple_Check(value)) {
92 return PyObject_Call(exception, value, NULL);
93 }
94 else {
Victor Stinner7bfb42d2016-12-05 17:04:32 +010095 return PyObject_CallFunctionObjArgs(exception, value, NULL);
Victor Stinner3a840972016-08-22 23:59:08 +020096 }
97}
98
Victor Stinner438a12d2019-05-24 17:01:38 +020099void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200100_PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 PyObject *exc_value;
103 PyObject *tb = NULL;
Guido van Rossumb4fb6e42008-06-14 20:20:24 +0000104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (exception != NULL &&
106 !PyExceptionClass_Check(exception)) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200107 _PyErr_Format(tstate, PyExc_SystemError,
108 "exception %R not a BaseException subclass",
109 exception);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return;
111 }
Victor Stinner3a840972016-08-22 23:59:08 +0200112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 Py_XINCREF(value);
Mark Shannonae3087c2017-10-22 22:41:51 +0100114 exc_value = _PyErr_GetTopmostException(tstate)->exc_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (exc_value != NULL && exc_value != Py_None) {
116 /* Implicit exception chaining */
117 Py_INCREF(exc_value);
118 if (value == NULL || !PyExceptionInstance_Check(value)) {
119 /* We must normalize the value right now */
Victor Stinner3a840972016-08-22 23:59:08 +0200120 PyObject *fixed_value;
Victor Stinnerde821be2015-03-24 12:41:23 +0100121
Victor Stinner3a840972016-08-22 23:59:08 +0200122 /* Issue #23571: functions must not be called with an
Victor Stinnerde821be2015-03-24 12:41:23 +0100123 exception set */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200124 _PyErr_Clear(tstate);
Victor Stinnerde821be2015-03-24 12:41:23 +0100125
Victor Stinner3a840972016-08-22 23:59:08 +0200126 fixed_value = _PyErr_CreateException(exception, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 Py_XDECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +0200128 if (fixed_value == NULL) {
Alexey Izbysheva2eefa62018-08-26 19:31:25 +0300129 Py_DECREF(exc_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 return;
Victor Stinner3a840972016-08-22 23:59:08 +0200131 }
132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 value = fixed_value;
134 }
Victor Stinner3a840972016-08-22 23:59:08 +0200135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 /* Avoid reference cycles through the context chain.
137 This is O(chain length) but context chains are
138 usually very short. Sensitive readers may try
139 to inline the call to PyException_GetContext. */
140 if (exc_value != value) {
141 PyObject *o = exc_value, *context;
142 while ((context = PyException_GetContext(o))) {
143 Py_DECREF(context);
144 if (context == value) {
145 PyException_SetContext(o, NULL);
146 break;
147 }
148 o = context;
149 }
150 PyException_SetContext(value, exc_value);
Victor Stinner3a840972016-08-22 23:59:08 +0200151 }
152 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 Py_DECREF(exc_value);
154 }
155 }
156 if (value != NULL && PyExceptionInstance_Check(value))
157 tb = PyException_GetTraceback(value);
158 Py_XINCREF(exception);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200159 _PyErr_Restore(tstate, exception, value, tb);
160}
161
162void
163PyErr_SetObject(PyObject *exception, PyObject *value)
164{
165 PyThreadState *tstate = _PyThreadState_GET();
166 _PyErr_SetObject(tstate, exception, value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167}
168
Raymond Hettinger69492da2013-09-02 15:59:26 -0700169/* Set a key error with the specified argument, wrapping it in a
170 * tuple automatically so that tuple keys are not unpacked as the
171 * exception arguments. */
172void
173_PyErr_SetKeyError(PyObject *arg)
174{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200175 PyThreadState *tstate = _PyThreadState_GET();
176 PyObject *tup = PyTuple_Pack(1, arg);
177 if (!tup) {
178 /* caller will expect error to be set anyway */
179 return;
180 }
181 _PyErr_SetObject(tstate, PyExc_KeyError, tup);
Raymond Hettinger69492da2013-09-02 15:59:26 -0700182 Py_DECREF(tup);
183}
184
Victor Stinner438a12d2019-05-24 17:01:38 +0200185void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200186_PyErr_SetNone(PyThreadState *tstate, PyObject *exception)
187{
188 _PyErr_SetObject(tstate, exception, (PyObject *)NULL);
189}
190
191
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200195 PyThreadState *tstate = _PyThreadState_GET();
196 _PyErr_SetNone(tstate, exception);
197}
198
199
Victor Stinner438a12d2019-05-24 17:01:38 +0200200void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200201_PyErr_SetString(PyThreadState *tstate, PyObject *exception,
202 const char *string)
203{
204 PyObject *value = PyUnicode_FromString(string);
205 _PyErr_SetObject(tstate, exception, value);
206 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207}
208
209void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000210PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200212 PyThreadState *tstate = _PyThreadState_GET();
213 _PyErr_SetString(tstate, exception, string);
214}
215
216
Victor Stinnerc6944e72016-11-11 02:13:35 +0100217PyObject* _Py_HOT_FUNCTION
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219{
Victor Stinner50b48572018-11-01 01:51:40 +0100220 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200221 return _PyErr_Occurred(tstate);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222}
223
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000224
225int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (err == NULL || exc == NULL) {
229 /* maybe caused by "import exceptions" that failed early on */
230 return 0;
231 }
232 if (PyTuple_Check(exc)) {
233 Py_ssize_t i, n;
234 n = PyTuple_Size(exc);
235 for (i = 0; i < n; i++) {
236 /* Test recursively */
237 if (PyErr_GivenExceptionMatches(
238 err, PyTuple_GET_ITEM(exc, i)))
239 {
240 return 1;
241 }
242 }
243 return 0;
244 }
245 /* err might be an instance, so check its class. */
246 if (PyExceptionInstance_Check(err))
247 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
scodere4c06bc2017-07-31 22:27:46 +0200250 return PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000254}
Guido van Rossum743007d1999-04-21 15:27:31 +0000255
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000256
257int
Victor Stinner438a12d2019-05-24 17:01:38 +0200258_PyErr_ExceptionMatches(PyThreadState *tstate, PyObject *exc)
259{
260 return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc);
261}
262
263
264int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000265PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000266{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200267 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner438a12d2019-05-24 17:01:38 +0200268 return _PyErr_ExceptionMatches(tstate, exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000269}
270
271
xdegaye56d1f5c2017-10-26 15:09:06 +0200272#ifndef Py_NORMALIZE_RECURSION_LIMIT
273#define Py_NORMALIZE_RECURSION_LIMIT 32
274#endif
275
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000276/* Used in many places to normalize a raised exception, including in
277 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000278
279 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000281*/
Victor Stinner438a12d2019-05-24 17:01:38 +0200282void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200283_PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
284 PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000285{
Serhiy Storchakacf296532017-11-05 11:27:48 +0200286 int recursion_depth = 0;
287 PyObject *type, *value, *initial_tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000288
Serhiy Storchakacf296532017-11-05 11:27:48 +0200289 restart:
290 type = *exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 if (type == NULL) {
292 /* There was no exception, so nothing to do. */
293 return;
294 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000295
Serhiy Storchakacf296532017-11-05 11:27:48 +0200296 value = *val;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* If PyErr_SetNone() was used, the value will have been actually
298 set to NULL.
299 */
300 if (!value) {
301 value = Py_None;
302 Py_INCREF(value);
303 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* Normalize the exception so that if the type is a class, the
306 value will be an instance.
307 */
308 if (PyExceptionClass_Check(type)) {
Serhiy Storchakacf296532017-11-05 11:27:48 +0200309 PyObject *inclass = NULL;
310 int is_subclass = 0;
Victor Stinner74a7fa62013-07-17 00:44:53 +0200311
Serhiy Storchakacf296532017-11-05 11:27:48 +0200312 if (PyExceptionInstance_Check(value)) {
313 inclass = PyExceptionInstance_Class(value);
314 is_subclass = PyObject_IsSubclass(inclass, type);
315 if (is_subclass < 0) {
316 goto error;
317 }
318 }
319
320 /* If the value was not an instance, or is not an instance
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 whose class is (or is derived from) type, then use the
322 value as an argument to instantiation of the type
323 class.
324 */
Serhiy Storchakacf296532017-11-05 11:27:48 +0200325 if (!is_subclass) {
326 PyObject *fixed_value = _PyErr_CreateException(type, value);
Victor Stinner3a840972016-08-22 23:59:08 +0200327 if (fixed_value == NULL) {
Serhiy Storchakacf296532017-11-05 11:27:48 +0200328 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_DECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +0200331 value = fixed_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 }
Serhiy Storchakacf296532017-11-05 11:27:48 +0200333 /* If the class of the instance doesn't exactly match the
334 class of the type, believe the instance.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 */
336 else if (inclass != type) {
Serhiy Storchakacf296532017-11-05 11:27:48 +0200337 Py_INCREF(inclass);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 Py_DECREF(type);
339 type = inclass;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 }
341 }
342 *exc = type;
343 *val = value;
344 return;
Serhiy Storchakacf296532017-11-05 11:27:48 +0200345
346 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_DECREF(type);
348 Py_DECREF(value);
Serhiy Storchakacf296532017-11-05 11:27:48 +0200349 recursion_depth++;
350 if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200351 _PyErr_SetString(tstate, PyExc_RecursionError,
352 "maximum recursion depth exceeded "
353 "while normalizing an exception");
xdegaye56d1f5c2017-10-26 15:09:06 +0200354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 /* If the new exception doesn't set a traceback and the old
356 exception had a traceback, use the old traceback for the
357 new exception. It's better than nothing.
358 */
359 initial_tb = *tb;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200360 _PyErr_Fetch(tstate, exc, val, tb);
Serhiy Storchakacf296532017-11-05 11:27:48 +0200361 assert(*exc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (initial_tb != NULL) {
363 if (*tb == NULL)
364 *tb = initial_tb;
365 else
366 Py_DECREF(initial_tb);
367 }
Serhiy Storchakacf296532017-11-05 11:27:48 +0200368 /* Abort when Py_NORMALIZE_RECURSION_LIMIT has been exceeded, and the
369 corresponding RecursionError could not be normalized, and the
370 MemoryError raised when normalize this RecursionError could not be
371 normalized. */
372 if (recursion_depth >= Py_NORMALIZE_RECURSION_LIMIT + 2) {
xdegaye56d1f5c2017-10-26 15:09:06 +0200373 if (PyErr_GivenExceptionMatches(*exc, PyExc_MemoryError)) {
374 Py_FatalError("Cannot recover from MemoryErrors "
375 "while normalizing exceptions.");
376 }
377 else {
378 Py_FatalError("Cannot recover from the recursive normalization "
379 "of an exception.");
380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 }
Serhiy Storchakacf296532017-11-05 11:27:48 +0200382 goto restart;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000383}
384
385
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200387PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Victor Stinner50b48572018-11-01 01:51:40 +0100389 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200390 _PyErr_NormalizeException(tstate, exc, val, tb);
391}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000392
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200393
Victor Stinner438a12d2019-05-24 17:01:38 +0200394void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200395_PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value,
396 PyObject **p_traceback)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 *p_type = tstate->curexc_type;
399 *p_value = tstate->curexc_value;
400 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 tstate->curexc_type = NULL;
403 tstate->curexc_value = NULL;
404 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405}
406
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200407
408void
409PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
410{
411 PyThreadState *tstate = _PyThreadState_GET();
412 _PyErr_Fetch(tstate, p_type, p_value, p_traceback);
413}
414
415
Victor Stinner438a12d2019-05-24 17:01:38 +0200416void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200417_PyErr_Clear(PyThreadState *tstate)
418{
419 _PyErr_Restore(tstate, NULL, NULL, NULL);
420}
421
422
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000424PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200426 PyThreadState *tstate = _PyThreadState_GET();
427 _PyErr_Clear(tstate);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000429
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200430
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200431void
432PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
433{
Victor Stinner50b48572018-11-01 01:51:40 +0100434 PyThreadState *tstate = _PyThreadState_GET();
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200435
Mark Shannonae3087c2017-10-22 22:41:51 +0100436 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
437 *p_type = exc_info->exc_type;
438 *p_value = exc_info->exc_value;
439 *p_traceback = exc_info->exc_traceback;
440
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200441
442 Py_XINCREF(*p_type);
443 Py_XINCREF(*p_value);
444 Py_XINCREF(*p_traceback);
445}
446
447void
448PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
449{
450 PyObject *oldtype, *oldvalue, *oldtraceback;
Victor Stinner50b48572018-11-01 01:51:40 +0100451 PyThreadState *tstate = _PyThreadState_GET();
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200452
Mark Shannonae3087c2017-10-22 22:41:51 +0100453 oldtype = tstate->exc_info->exc_type;
454 oldvalue = tstate->exc_info->exc_value;
455 oldtraceback = tstate->exc_info->exc_traceback;
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200456
Mark Shannonae3087c2017-10-22 22:41:51 +0100457 tstate->exc_info->exc_type = p_type;
458 tstate->exc_info->exc_value = p_value;
459 tstate->exc_info->exc_traceback = p_traceback;
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200460
461 Py_XDECREF(oldtype);
462 Py_XDECREF(oldvalue);
463 Py_XDECREF(oldtraceback);
464}
465
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300466/* Like PyErr_Restore(), but if an exception is already set,
467 set the context associated with it.
468 */
469void
470_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
471{
472 if (exc == NULL)
473 return;
474
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200475 PyThreadState *tstate = _PyThreadState_GET();
476 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300477 PyObject *exc2, *val2, *tb2;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200478 _PyErr_Fetch(tstate, &exc2, &val2, &tb2);
479 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Serhiy Storchaka9e373be2016-10-21 16:19:59 +0300480 if (tb != NULL) {
481 PyException_SetTraceback(val, tb);
482 Py_DECREF(tb);
483 }
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300484 Py_DECREF(exc);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200485 _PyErr_NormalizeException(tstate, &exc2, &val2, &tb2);
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300486 PyException_SetContext(val2, val);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200487 _PyErr_Restore(tstate, exc2, val2, tb2);
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300488 }
489 else {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200490 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300491 }
492}
493
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300494static PyObject *
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200495_PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
496 const char *format, va_list vargs)
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300497{
498 PyObject *exc, *val, *val2, *tb;
499
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200500 assert(_PyErr_Occurred(tstate));
501 _PyErr_Fetch(tstate, &exc, &val, &tb);
502 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300503 if (tb != NULL) {
504 PyException_SetTraceback(val, tb);
505 Py_DECREF(tb);
506 }
507 Py_DECREF(exc);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200508 assert(!_PyErr_Occurred(tstate));
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300509
Victor Stinner438a12d2019-05-24 17:01:38 +0200510 _PyErr_FormatV(tstate, exception, format, vargs);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300511
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200512 _PyErr_Fetch(tstate, &exc, &val2, &tb);
513 _PyErr_NormalizeException(tstate, &exc, &val2, &tb);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300514 Py_INCREF(val);
515 PyException_SetCause(val2, val);
516 PyException_SetContext(val2, val);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200517 _PyErr_Restore(tstate, exc, val2, tb);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300518
519 return NULL;
520}
521
522PyObject *
523_PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
524{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200525 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300526 va_list vargs;
527#ifdef HAVE_STDARG_PROTOTYPES
528 va_start(vargs, format);
529#else
530 va_start(vargs);
531#endif
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200532 _PyErr_FormatVFromCause(tstate, exception, format, vargs);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300533 va_end(vargs);
534 return NULL;
535}
536
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000537/* Convenience functions to set a type error exception and return 0 */
538
539int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000540PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000541{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200542 PyThreadState *tstate = _PyThreadState_GET();
543 _PyErr_SetString(tstate, PyExc_TypeError,
544 "bad argument type for built-in operation");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000546}
547
Guido van Rossum373c8691997-04-29 18:22:47 +0000548PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000549PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000550{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200551 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf54a5742013-07-22 22:28:37 +0200552 if (Py_TYPE(PyExc_MemoryError) == NULL) {
553 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
554 initialized by _PyExc_Init() */
555 Py_FatalError("Out of memory and PyExc_MemoryError is not "
556 "initialized yet");
557 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200558 _PyErr_SetNone(tstate, PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000560}
561
Guido van Rossum373c8691997-04-29 18:22:47 +0000562PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000563PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000564{
Larry Hastingsb0827312014-02-09 22:05:19 -0800565 return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
566}
567
568PyObject *
569PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
570{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200571 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200573 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100575#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000577#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000578
Guido van Rossume9fbc091995-02-18 14:52:19 +0000579#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (i == EINTR && PyErr_CheckSignals())
581 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000582#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000583
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000584#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100585 if (i != 0) {
586 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100587 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100588 }
589 else {
590 /* Sometimes errno didn't get set */
591 message = PyUnicode_FromString("Error");
592 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000593#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 if (i == 0)
595 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
596 else
597 {
598 /* Note that the Win32 errors do not lineup with the
599 errno error. So if the error is in the MSVC error
600 table, we use it, otherwise we assume it really _is_
601 a Win32 error code
602 */
603 if (i > 0 && i < _sys_nerr) {
604 message = PyUnicode_FromString(_sys_errlist[i]);
605 }
606 else {
607 int len = FormatMessageW(
608 FORMAT_MESSAGE_ALLOCATE_BUFFER |
609 FORMAT_MESSAGE_FROM_SYSTEM |
610 FORMAT_MESSAGE_IGNORE_INSERTS,
611 NULL, /* no message source */
612 i,
613 MAKELANGID(LANG_NEUTRAL,
614 SUBLANG_DEFAULT),
615 /* Default language */
616 (LPWSTR) &s_buf,
617 0, /* size not used */
618 NULL); /* no args */
619 if (len==0) {
620 /* Only ever seen this in out-of-mem
621 situations */
622 s_buf = NULL;
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300623 message = PyUnicode_FromFormat("Windows Error 0x%x", i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 } else {
625 /* remove trailing cr/lf and dots */
626 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
627 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200628 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 }
630 }
631 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000632#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (message == NULL)
635 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000636#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000638#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 return NULL;
640 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000641
Larry Hastingsb0827312014-02-09 22:05:19 -0800642 if (filenameObject != NULL) {
643 if (filenameObject2 != NULL)
644 args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
645 else
646 args = Py_BuildValue("(iOO)", i, message, filenameObject);
647 } else {
648 assert(filenameObject2 == NULL);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200649 args = Py_BuildValue("(iO)", i, message);
Larry Hastingsb0827312014-02-09 22:05:19 -0800650 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000652
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200653 if (args != NULL) {
654 v = PyObject_Call(exc, args, NULL);
655 Py_DECREF(args);
656 if (v != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200657 _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200658 Py_DECREF(v);
659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000661#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000663#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000665}
Guido van Rossum743007d1999-04-21 15:27:31 +0000666
Barry Warsaw97d95151998-07-23 16:05:56 +0000667PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000668PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800671 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 Py_XDECREF(name);
673 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000674}
675
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000676#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000677PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000678PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000679{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200680 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800681 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 Py_XDECREF(name);
683 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000684}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000685#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686
687PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000688PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000689{
Larry Hastingsb0827312014-02-09 22:05:19 -0800690 return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000691}
Guido van Rossum683a0721990-10-21 22:09:12 +0000692
Brett Cannonbf364092006-03-01 04:25:17 +0000693#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000694/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000695PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyObject *exc,
697 int ierr,
698 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000699{
Larry Hastingsb0827312014-02-09 22:05:19 -0800700 return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
701 filenameObject, NULL);
702}
703
704PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
705 PyObject *exc,
706 int ierr,
707 PyObject *filenameObject,
708 PyObject *filenameObject2)
709{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200710 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 int len;
712 WCHAR *s_buf = NULL; /* Free via LocalFree */
713 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200714 PyObject *args, *v;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 DWORD err = (DWORD)ierr;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200717 if (err==0) {
718 err = GetLastError();
719 }
720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 len = FormatMessageW(
722 /* Error API error */
723 FORMAT_MESSAGE_ALLOCATE_BUFFER |
724 FORMAT_MESSAGE_FROM_SYSTEM |
725 FORMAT_MESSAGE_IGNORE_INSERTS,
726 NULL, /* no message source */
727 err,
728 MAKELANGID(LANG_NEUTRAL,
729 SUBLANG_DEFAULT), /* Default language */
730 (LPWSTR) &s_buf,
731 0, /* size not used */
732 NULL); /* no args */
733 if (len==0) {
734 /* Only seen this in out of mem situations */
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300735 message = PyUnicode_FromFormat("Windows Error 0x%x", err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 s_buf = NULL;
737 } else {
738 /* remove trailing cr/lf and dots */
739 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
740 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200741 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 if (message == NULL)
745 {
746 LocalFree(s_buf);
747 return NULL;
748 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000749
Larry Hastingsb0827312014-02-09 22:05:19 -0800750 if (filenameObject == NULL) {
751 assert(filenameObject2 == NULL);
752 filenameObject = filenameObject2 = Py_None;
753 }
754 else if (filenameObject2 == NULL)
755 filenameObject2 = Py_None;
756 /* This is the constructor signature for OSError.
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200757 The POSIX translation will be figured out by the constructor. */
Larry Hastingsb0827312014-02-09 22:05:19 -0800758 args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000760
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200761 if (args != NULL) {
762 v = PyObject_Call(exc, args, NULL);
763 Py_DECREF(args);
764 if (v != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200765 _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200766 Py_DECREF(v);
767 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 }
769 LocalFree(s_buf);
770 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000771}
772
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000773PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 PyObject *exc,
775 int ierr,
776 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000777{
Victor Stinner92be9392010-12-28 00:28:21 +0000778 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800779 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800781 name,
782 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 Py_XDECREF(name);
784 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000785}
786
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000787PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject *exc,
789 int ierr,
790 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000791{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200792 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800793 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800795 name,
796 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_XDECREF(name);
798 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000799}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000800
Thomas Heller085358a2002-07-29 14:27:41 +0000801PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
802{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800803 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000804}
805
Guido van Rossum795e1892000-02-17 15:19:15 +0000806PyObject *PyErr_SetFromWindowsErr(int ierr)
807{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800808 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
809 ierr, NULL);
Larry Hastingsb0827312014-02-09 22:05:19 -0800810}
811
Thomas Heller085358a2002-07-29 14:27:41 +0000812PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 int ierr,
814 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000815{
Victor Stinner92be9392010-12-28 00:28:21 +0000816 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800817 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200818 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800819 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Py_XDECREF(name);
821 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000822}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000823
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000824PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 int ierr,
826 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000827{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200828 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800829 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200830 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800831 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 Py_XDECREF(name);
833 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000834}
Guido van Rossum795e1892000-02-17 15:19:15 +0000835#endif /* MS_WINDOWS */
836
Brett Cannon79ec55e2012-04-12 20:24:54 -0400837PyObject *
Eric Snow46f97b82016-09-07 16:56:15 -0700838PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
839 PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400840{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200841 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow46f97b82016-09-07 16:56:15 -0700842 int issubclass;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200843 PyObject *kwargs, *error;
Brian Curtin09b86d12012-04-17 16:57:09 -0500844
Eric Snow46f97b82016-09-07 16:56:15 -0700845 issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
846 if (issubclass < 0) {
847 return NULL;
848 }
849 else if (!issubclass) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200850 _PyErr_SetString(tstate, PyExc_TypeError,
851 "expected a subclass of ImportError");
Brian Curtin94c001b2012-04-18 08:30:51 -0500852 return NULL;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200853 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500854
Eric Snow46f97b82016-09-07 16:56:15 -0700855 if (msg == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200856 _PyErr_SetString(tstate, PyExc_TypeError,
857 "expected a message argument");
Brian Curtin09b86d12012-04-17 16:57:09 -0500858 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400859 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500860
Brian Curtin94c001b2012-04-18 08:30:51 -0500861 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500862 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500863 }
Brian Curtin94c001b2012-04-18 08:30:51 -0500864 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500865 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500866 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500867
Eric Snow46f97b82016-09-07 16:56:15 -0700868 kwargs = PyDict_New();
869 if (kwargs == NULL) {
870 return NULL;
871 }
Victor Stinnerf45a5612016-08-23 00:04:41 +0200872 if (PyDict_SetItemString(kwargs, "name", name) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300873 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200874 }
875 if (PyDict_SetItemString(kwargs, "path", path) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300876 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200877 }
Brett Cannon79ec55e2012-04-12 20:24:54 -0400878
Eric Snow46f97b82016-09-07 16:56:15 -0700879 error = _PyObject_FastCallDict(exception, &msg, 1, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400880 if (error != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200881 _PyErr_SetObject(tstate, (PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500882 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400883 }
884
Berker Peksagec766d32016-05-01 09:06:36 +0300885done:
Brett Cannon79ec55e2012-04-12 20:24:54 -0400886 Py_DECREF(kwargs);
Brian Curtin09b86d12012-04-17 16:57:09 -0500887 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400888}
889
Eric Snow46f97b82016-09-07 16:56:15 -0700890PyObject *
891PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
892{
893 return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
894}
895
Guido van Rossum683a0721990-10-21 22:09:12 +0000896void
Neal Norwitzb382b842007-08-24 20:00:37 +0000897_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000898{
Victor Stinner438a12d2019-05-24 17:01:38 +0200899 PyThreadState *tstate = _PyThreadState_GET();
900 _PyErr_Format(tstate, PyExc_SystemError,
901 "%s:%d: bad argument to internal function",
902 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000903}
904
905/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
906 export the entry point for existing object code: */
907#undef PyErr_BadInternalCall
908void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000909PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000910{
Victor Stinnerfb3a6302013-07-12 00:37:30 +0200911 assert(0 && "bad argument to internal function");
Victor Stinner438a12d2019-05-24 17:01:38 +0200912 PyThreadState *tstate = _PyThreadState_GET();
913 _PyErr_SetString(tstate, PyExc_SystemError,
914 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000915}
Fred Drake6d63adf2000-08-24 22:38:39 +0000916#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
917
Guido van Rossum1548bac1997-02-14 17:09:47 +0000918
Victor Stinner438a12d2019-05-24 17:01:38 +0200919static PyObject *
920_PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
921 const char *format, va_list vargs)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000924
Victor Stinnerde821be2015-03-24 12:41:23 +0100925 /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
926 exception set, it calls arbitrary Python code like PyObject_Repr() */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200927 _PyErr_Clear(tstate);
Victor Stinnerace47d72013-07-18 01:41:08 +0200928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 string = PyUnicode_FromFormatV(format, vargs);
Victor Stinnerde821be2015-03-24 12:41:23 +0100930
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200931 _PyErr_SetObject(tstate, exception, string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 Py_XDECREF(string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000934}
Guido van Rossum7617e051997-09-16 18:43:50 +0000935
936
Antoine Pitrou0676a402014-09-30 21:16:27 +0200937PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +0200938PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
939{
940 PyThreadState *tstate = _PyThreadState_GET();
941 return _PyErr_FormatV(tstate, exception, format, vargs);
942}
943
944
945PyObject *
946_PyErr_Format(PyThreadState *tstate, PyObject *exception,
947 const char *format, ...)
Antoine Pitrou0676a402014-09-30 21:16:27 +0200948{
949 va_list vargs;
950#ifdef HAVE_STDARG_PROTOTYPES
951 va_start(vargs, format);
952#else
953 va_start(vargs);
954#endif
Victor Stinner438a12d2019-05-24 17:01:38 +0200955 _PyErr_FormatV(tstate, exception, format, vargs);
956 va_end(vargs);
957 return NULL;
958}
959
960
961PyObject *
962PyErr_Format(PyObject *exception, const char *format, ...)
963{
964 PyThreadState *tstate = _PyThreadState_GET();
965 va_list vargs;
966#ifdef HAVE_STDARG_PROTOTYPES
967 va_start(vargs, format);
968#else
969 va_start(vargs);
970#endif
971 _PyErr_FormatV(tstate, exception, format, vargs);
Antoine Pitrou0676a402014-09-30 21:16:27 +0200972 va_end(vargs);
973 return NULL;
974}
975
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976
Guido van Rossum7617e051997-09-16 18:43:50 +0000977PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000978PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000979{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200980 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200981 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 PyObject *modulename = NULL;
983 PyObject *classname = NULL;
984 PyObject *mydict = NULL;
985 PyObject *bases = NULL;
986 PyObject *result = NULL;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200987
988 const char *dot = strrchr(name, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 if (dot == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200990 _PyErr_SetString(tstate, PyExc_SystemError,
991 "PyErr_NewException: name must be module.class");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 return NULL;
993 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200994 if (base == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 base = PyExc_Exception;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (dict == NULL) {
998 dict = mydict = PyDict_New();
999 if (dict == NULL)
1000 goto failure;
1001 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001002
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001003 if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001004 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001005 goto failure;
1006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 modulename = PyUnicode_FromStringAndSize(name,
1008 (Py_ssize_t)(dot-name));
1009 if (modulename == NULL)
1010 goto failure;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001011 if (_PyDict_SetItemId(dict, &PyId___module__, modulename) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 goto failure;
1013 }
1014 if (PyTuple_Check(base)) {
1015 bases = base;
1016 /* INCREF as we create a new ref in the else branch */
1017 Py_INCREF(bases);
1018 } else {
1019 bases = PyTuple_Pack(1, base);
1020 if (bases == NULL)
1021 goto failure;
1022 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +01001023 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001024 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +00001026 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 Py_XDECREF(bases);
1028 Py_XDECREF(mydict);
1029 Py_XDECREF(classname);
1030 Py_XDECREF(modulename);
1031 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +00001032}
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001033
Georg Brandl1e28a272009-12-28 08:41:01 +00001034
1035/* Create an exception with docstring */
1036PyObject *
1037PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +00001039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 int result;
1041 PyObject *ret = NULL;
1042 PyObject *mydict = NULL; /* points to the dict only if we create it */
1043 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (dict == NULL) {
1046 dict = mydict = PyDict_New();
1047 if (dict == NULL) {
1048 return NULL;
1049 }
1050 }
Georg Brandl1e28a272009-12-28 08:41:01 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (doc != NULL) {
1053 docobj = PyUnicode_FromString(doc);
1054 if (docobj == NULL)
1055 goto failure;
1056 result = PyDict_SetItemString(dict, "__doc__", docobj);
1057 Py_DECREF(docobj);
1058 if (result < 0)
1059 goto failure;
1060 }
Georg Brandl1e28a272009-12-28 08:41:01 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +00001063 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 Py_XDECREF(mydict);
1065 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +00001066}
1067
1068
Victor Stinneref9d9b62019-05-22 11:28:22 +02001069PyDoc_STRVAR(UnraisableHookArgs__doc__,
1070"UnraisableHookArgs\n\
1071\n\
1072Type used to pass arguments to sys.unraisablehook.");
1073
1074static PyTypeObject UnraisableHookArgsType;
1075
1076static PyStructSequence_Field UnraisableHookArgs_fields[] = {
1077 {"exc_type", "Exception type"},
1078 {"exc_value", "Exception value"},
1079 {"exc_traceback", "Exception traceback"},
1080 {"object", "Object causing the exception"},
1081 {0}
1082};
1083
1084static PyStructSequence_Desc UnraisableHookArgs_desc = {
1085 .name = "UnraisableHookArgs",
1086 .doc = UnraisableHookArgs__doc__,
1087 .fields = UnraisableHookArgs_fields,
1088 .n_in_sequence = 4
1089};
1090
1091
1092_PyInitError
1093_PyErr_Init(void)
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001094{
Victor Stinneref9d9b62019-05-22 11:28:22 +02001095 if (UnraisableHookArgsType.tp_name == NULL) {
1096 if (PyStructSequence_InitType2(&UnraisableHookArgsType,
1097 &UnraisableHookArgs_desc) < 0) {
1098 return _Py_INIT_ERR("failed to initialize UnraisableHookArgs type");
Martin Panter3263f682016-02-28 03:16:11 +00001099 }
Victor Stinneref9d9b62019-05-22 11:28:22 +02001100 }
1101 return _Py_INIT_OK();
1102}
1103
1104
1105static PyObject *
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001106make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type,
1107 PyObject *exc_value, PyObject *exc_tb, PyObject *obj)
Victor Stinneref9d9b62019-05-22 11:28:22 +02001108{
1109 PyObject *args = PyStructSequence_New(&UnraisableHookArgsType);
1110 if (args == NULL) {
1111 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001113
Victor Stinneref9d9b62019-05-22 11:28:22 +02001114 Py_ssize_t pos = 0;
1115#define ADD_ITEM(exc_type) \
1116 do { \
1117 if (exc_type == NULL) { \
1118 exc_type = Py_None; \
1119 } \
1120 Py_INCREF(exc_type); \
1121 PyStructSequence_SET_ITEM(args, pos++, exc_type); \
1122 } while (0)
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001123
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001124
Victor Stinneref9d9b62019-05-22 11:28:22 +02001125 ADD_ITEM(exc_type);
1126 ADD_ITEM(exc_value);
1127 ADD_ITEM(exc_tb);
1128 ADD_ITEM(obj);
1129#undef ADD_ITEM
1130
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001131 if (_PyErr_Occurred(tstate)) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001132 Py_DECREF(args);
1133 return NULL;
1134 }
1135 return args;
1136}
1137
1138
1139
1140/* Default implementation of sys.unraisablehook.
1141
1142 It can be called to log the exception of a custom sys.unraisablehook.
1143
1144 Do nothing if sys.stderr attribute doesn't exist or is set to None. */
1145static int
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001146write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
1147 PyObject *exc_value, PyObject *exc_tb,
1148 PyObject *obj, PyObject *file)
Victor Stinneref9d9b62019-05-22 11:28:22 +02001149{
1150 if (obj != NULL && obj != Py_None) {
1151 if (PyFile_WriteString("Exception ignored in: ", file) < 0) {
1152 return -1;
1153 }
1154
1155 if (PyFile_WriteObject(obj, file, 0) < 0) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001156 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001157 if (PyFile_WriteString("<object repr() failed>", file) < 0) {
1158 return -1;
1159 }
1160 }
1161 if (PyFile_WriteString("\n", file) < 0) {
1162 return -1;
1163 }
1164 }
1165
1166 if (exc_tb != NULL && exc_tb != Py_None) {
1167 if (PyTraceBack_Print(exc_tb, file) < 0) {
1168 /* continue even if writing the traceback failed */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001169 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001170 }
1171 }
1172
Victor Stinnerdf22c032019-05-23 01:00:58 +02001173 if (exc_type == NULL || exc_type == Py_None) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001174 return -1;
1175 }
1176
1177 assert(PyExceptionClass_Check(exc_type));
1178 const char *className = PyExceptionClass_Name(exc_type);
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001179 if (className != NULL) {
Serhiy Storchakaceeef102018-06-15 11:09:43 +03001180 const char *dot = strrchr(className, '.');
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001181 if (dot != NULL)
1182 className = dot+1;
1183 }
1184
Victor Stinneref9d9b62019-05-22 11:28:22 +02001185 _Py_IDENTIFIER(__module__);
1186 PyObject *moduleName = _PyObject_GetAttrId(exc_type, &PyId___module__);
Oren Milmanf6e61df2017-09-14 01:30:05 +03001187 if (moduleName == NULL || !PyUnicode_Check(moduleName)) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001188 Py_XDECREF(moduleName);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001189 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001190 if (PyFile_WriteString("<unknown>", file) < 0) {
1191 return -1;
1192 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001193 }
1194 else {
Serhiy Storchakaf5894dd2016-11-16 15:40:39 +02001195 if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001196 if (PyFile_WriteObject(moduleName, file, Py_PRINT_RAW) < 0) {
1197 Py_DECREF(moduleName);
1198 return -1;
1199 }
1200 Py_DECREF(moduleName);
1201 if (PyFile_WriteString(".", file) < 0) {
1202 return -1;
1203 }
1204 }
1205 else {
1206 Py_DECREF(moduleName);
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001207 }
1208 }
1209 if (className == NULL) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001210 if (PyFile_WriteString("<unknown>", file) < 0) {
1211 return -1;
1212 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001213 }
1214 else {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001215 if (PyFile_WriteString(className, file) < 0) {
1216 return -1;
1217 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001218 }
1219
Victor Stinneref9d9b62019-05-22 11:28:22 +02001220 if (exc_value && exc_value != Py_None) {
1221 if (PyFile_WriteString(": ", file) < 0) {
1222 return -1;
1223 }
1224 if (PyFile_WriteObject(exc_value, file, Py_PRINT_RAW) < 0) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001225 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001226 if (PyFile_WriteString("<exception str() failed>", file) < 0) {
1227 return -1;
Martin Panter3263f682016-02-28 03:16:11 +00001228 }
1229 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001230 }
Victor Stinnerdf22c032019-05-23 01:00:58 +02001231
Victor Stinneref9d9b62019-05-22 11:28:22 +02001232 if (PyFile_WriteString("\n", file) < 0) {
1233 return -1;
1234 }
1235 return 0;
1236}
1237
1238
1239static int
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001240write_unraisable_exc(PyThreadState *tstate, PyObject *exc_type,
1241 PyObject *exc_value, PyObject *exc_tb, PyObject *obj)
Victor Stinneref9d9b62019-05-22 11:28:22 +02001242{
1243 PyObject *file = _PySys_GetObjectId(&PyId_stderr);
1244 if (file == NULL || file == Py_None) {
1245 return 0;
1246 }
1247
1248 /* Hold a strong reference to ensure that sys.stderr doesn't go away
1249 while we use it */
1250 Py_INCREF(file);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001251 int res = write_unraisable_exc_file(tstate, exc_type, exc_value, exc_tb,
Victor Stinneref9d9b62019-05-22 11:28:22 +02001252 obj, file);
1253 Py_DECREF(file);
1254
1255 return res;
1256}
1257
1258
1259PyObject*
1260_PyErr_WriteUnraisableDefaultHook(PyObject *args)
1261{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001262 PyThreadState *tstate = _PyThreadState_GET();
1263
Victor Stinneref9d9b62019-05-22 11:28:22 +02001264 if (Py_TYPE(args) != &UnraisableHookArgsType) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001265 _PyErr_SetString(tstate, PyExc_TypeError,
1266 "sys.unraisablehook argument type "
1267 "must be UnraisableHookArgs");
Victor Stinneref9d9b62019-05-22 11:28:22 +02001268 return NULL;
1269 }
1270
1271 /* Borrowed references */
1272 PyObject *exc_type = PyStructSequence_GET_ITEM(args, 0);
1273 PyObject *exc_value = PyStructSequence_GET_ITEM(args, 1);
1274 PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
1275 PyObject *obj = PyStructSequence_GET_ITEM(args, 3);
1276
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001277 if (write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, obj) < 0) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001278 return NULL;
1279 }
1280 Py_RETURN_NONE;
1281}
1282
1283
1284/* Call sys.unraisablehook().
1285
1286 This function can be used when an exception has occurred but there is no way
1287 for Python to handle it. For example, when a destructor raises an exception
1288 or during garbage collection (gc.collect()).
1289
1290 An exception must be set when calling this function. */
1291void
1292PyErr_WriteUnraisable(PyObject *obj)
1293{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001294 PyThreadState *tstate = _PyThreadState_GET();
1295 assert(tstate != NULL);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001296
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001297 PyObject *exc_type, *exc_value, *exc_tb;
1298 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001299
1300 assert(exc_type != NULL);
1301
1302 if (exc_type == NULL) {
1303 /* sys.unraisablehook requires that at least exc_type is set */
1304 goto default_hook;
1305 }
1306
Victor Stinnerdf22c032019-05-23 01:00:58 +02001307 if (exc_tb == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001308 struct _frame *frame = tstate->frame;
Victor Stinnerdf22c032019-05-23 01:00:58 +02001309 if (frame != NULL) {
1310 exc_tb = _PyTraceBack_FromFrame(NULL, frame);
1311 if (exc_tb == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001312 _PyErr_Clear(tstate);
Victor Stinnerdf22c032019-05-23 01:00:58 +02001313 }
1314 }
1315 }
1316
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001317 _PyErr_NormalizeException(tstate, &exc_type, &exc_value, &exc_tb);
Victor Stinnerdf22c032019-05-23 01:00:58 +02001318
1319 if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) {
1320 if (PyException_SetTraceback(exc_value, exc_tb) < 0) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001321 _PyErr_Clear(tstate);
Victor Stinnerdf22c032019-05-23 01:00:58 +02001322 }
1323 }
1324
Victor Stinneref9d9b62019-05-22 11:28:22 +02001325 _Py_IDENTIFIER(unraisablehook);
1326 PyObject *hook = _PySys_GetObjectId(&PyId_unraisablehook);
1327 if (hook != NULL && hook != Py_None) {
1328 PyObject *hook_args;
1329
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001330 hook_args = make_unraisable_hook_args(tstate, exc_type, exc_value,
1331 exc_tb, obj);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001332 if (hook_args != NULL) {
1333 PyObject *args[1] = {hook_args};
1334 PyObject *res = _PyObject_FastCall(hook, args, 1);
1335 Py_DECREF(hook_args);
1336 if (res != NULL) {
1337 Py_DECREF(res);
1338 goto done;
1339 }
1340 }
1341
1342 /* sys.unraisablehook failed: log its error using default hook */
1343 Py_XDECREF(exc_type);
1344 Py_XDECREF(exc_value);
1345 Py_XDECREF(exc_tb);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001346 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001347
1348 obj = hook;
1349 }
1350
1351default_hook:
1352 /* Call the default unraisable hook (ignore failure) */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001353 (void)write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, obj);
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001354
1355done:
Victor Stinneref9d9b62019-05-22 11:28:22 +02001356 Py_XDECREF(exc_type);
1357 Py_XDECREF(exc_value);
1358 Py_XDECREF(exc_tb);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001359 _PyErr_Clear(tstate); /* Just in case */
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001360}
Guido van Rossumcfd42b52000-12-15 21:58:52 +00001361
Armin Rigo092381a2003-10-25 14:29:27 +00001362extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +00001363
Guido van Rossum2fd45652001-02-28 21:46:24 +00001364
Benjamin Peterson2c539712010-09-20 22:42:10 +00001365void
Victor Stinner14e461d2013-08-26 22:28:21 +02001366PyErr_SyntaxLocation(const char *filename, int lineno)
1367{
Benjamin Peterson2c539712010-09-20 22:42:10 +00001368 PyErr_SyntaxLocationEx(filename, lineno, -1);
1369}
1370
1371
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001372/* Set file and line information for the current exception.
1373 If the exception is not a SyntaxError, also sets additional attributes
1374 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +00001375
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001376void
Victor Stinner14e461d2013-08-26 22:28:21 +02001377PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001380 _Py_IDENTIFIER(filename);
1381 _Py_IDENTIFIER(lineno);
1382 _Py_IDENTIFIER(msg);
1383 _Py_IDENTIFIER(offset);
1384 _Py_IDENTIFIER(print_file_and_line);
1385 _Py_IDENTIFIER(text);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001386 PyThreadState *tstate = _PyThreadState_GET();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* add attributes for the line number and filename for the error */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001389 _PyErr_Fetch(tstate, &exc, &v, &tb);
1390 _PyErr_NormalizeException(tstate, &exc, &v, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 /* XXX check that it is, indeed, a syntax error. It might not
1392 * be, though. */
1393 tmp = PyLong_FromLong(lineno);
1394 if (tmp == NULL)
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001395 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 else {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001397 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) {
1398 _PyErr_Clear(tstate);
1399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 Py_DECREF(tmp);
1401 }
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001402 tmp = NULL;
Benjamin Peterson2c539712010-09-20 22:42:10 +00001403 if (col_offset >= 0) {
1404 tmp = PyLong_FromLong(col_offset);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001405 if (tmp == NULL) {
1406 _PyErr_Clear(tstate);
1407 }
Benjamin Peterson2c539712010-09-20 22:42:10 +00001408 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001409 if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None)) {
1410 _PyErr_Clear(tstate);
1411 }
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001412 Py_XDECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (filename != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001414 if (_PyObject_SetAttrId(v, &PyId_filename, filename)) {
1415 _PyErr_Clear(tstate);
1416 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001417
Victor Stinner14e461d2013-08-26 22:28:21 +02001418 tmp = PyErr_ProgramTextObject(filename, lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (tmp) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001420 if (_PyObject_SetAttrId(v, &PyId_text, tmp)) {
1421 _PyErr_Clear(tstate);
1422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 Py_DECREF(tmp);
1424 }
1425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001427 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 tmp = PyObject_Str(v);
1429 if (tmp) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001430 if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) {
1431 _PyErr_Clear(tstate);
1432 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 Py_DECREF(tmp);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001434 }
1435 else {
1436 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 }
1438 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001439 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
1440 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001441 Py_None)) {
1442 _PyErr_Clear(tstate);
1443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 }
1445 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001446 _PyErr_Restore(tstate, exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001447}
1448
Victor Stinner14e461d2013-08-26 22:28:21 +02001449void
1450PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
1451{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001452 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner14e461d2013-08-26 22:28:21 +02001453 PyObject *fileobj;
1454 if (filename != NULL) {
1455 fileobj = PyUnicode_DecodeFSDefault(filename);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001456 if (fileobj == NULL) {
1457 _PyErr_Clear(tstate);
1458 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001459 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001460 else {
Victor Stinner14e461d2013-08-26 22:28:21 +02001461 fileobj = NULL;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001462 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001463 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
1464 Py_XDECREF(fileobj);
1465}
1466
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001467/* Attempt to load the line of text that the exception refers to. If it
1468 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001469
1470 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001471 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001472
Antoine Pitrou409b5382013-10-12 22:41:17 +02001473static PyObject *
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001474err_programtext(PyThreadState *tstate, FILE *fp, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 int i;
1477 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (fp == NULL)
1480 return NULL;
1481 for (i = 0; i < lineno; i++) {
1482 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1483 do {
1484 *pLastChar = '\0';
1485 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1486 fp, NULL) == NULL)
1487 break;
1488 /* fgets read *something*; if it didn't get as
1489 far as pLastChar, it must have found a newline
1490 or hit the end of the file; if pLastChar is \n,
1491 it obviously found a newline; else we haven't
1492 yet seen a newline, so must continue */
1493 } while (*pLastChar != '\0' && *pLastChar != '\n');
1494 }
1495 fclose(fp);
1496 if (i == lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 PyObject *res;
Martin Panterca3263c2016-12-11 00:18:36 +00001498 res = PyUnicode_FromString(linebuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (res == NULL)
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001500 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 return res;
1502 }
1503 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001504}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001505
Victor Stinner14e461d2013-08-26 22:28:21 +02001506PyObject *
1507PyErr_ProgramText(const char *filename, int lineno)
1508{
1509 FILE *fp;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001510 if (filename == NULL || *filename == '\0' || lineno <= 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02001511 return NULL;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001512 }
1513 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerdaf45552013-08-28 00:53:59 +02001514 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001515 return err_programtext(tstate, fp, lineno);
Victor Stinner14e461d2013-08-26 22:28:21 +02001516}
1517
1518PyObject *
1519PyErr_ProgramTextObject(PyObject *filename, int lineno)
1520{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001521 if (filename == NULL || lineno <= 0) {
Victor Stinnere42ccd22015-03-18 01:39:23 +01001522 return NULL;
1523 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001524
1525 PyThreadState *tstate = _PyThreadState_GET();
1526 FILE *fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
1527 if (fp == NULL) {
1528 _PyErr_Clear(tstate);
1529 return NULL;
1530 }
1531 return err_programtext(tstate, fp, lineno);
Victor Stinner14e461d2013-08-26 22:28:21 +02001532}
1533
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001534#ifdef __cplusplus
1535}
1536#endif