blob: f856a798eed1e5d5b7d0692e5758d6ff5f7bfecb [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 Stinner331a6a52019-05-27 16:39:22 +02005#include "pycore_initconfig.h"
Victor Stinner438a12d2019-05-24 17:01:38 +02006#include "pycore_pyerrors.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02007#include "pycore_pystate.h" // _PyThreadState_GET()
Victor Stinner1c1e68c2020-03-27 15:11:45 +01008#include "pycore_sysmodule.h"
Victor Stinnerdf22c032019-05-23 01:00:58 +02009#include "pycore_traceback.h"
Guido van Rossumf22120a1990-12-20 23:05:40 +000010
Guido van Rossum53e8d441995-03-09 12:11:31 +000011#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +000012#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +000013extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +000014#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000015#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000016
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000017#ifdef MS_WINDOWS
Martin v. Löwis5d12abe2007-09-03 07:40:24 +000018#include <windows.h>
19#include <winbase.h>
Guido van Rossum743007d1999-04-21 15:27:31 +000020#endif
21
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000022#include <ctype.h>
23
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000024#ifdef __cplusplus
25extern "C" {
26#endif
27
Hai Shi46874c22020-01-30 17:20:25 -060028_Py_IDENTIFIER(__module__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010029_Py_IDENTIFIER(builtins);
30_Py_IDENTIFIER(stderr);
Victor Stinnera85a1d32019-05-28 16:01:17 +020031_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010032
Victor Stinner438a12d2019-05-24 17:01:38 +020033/* Forward declarations */
34static PyObject *
35_PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
36 const char *format, va_list vargs);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +020037
38
Victor Stinner438a12d2019-05-24 17:01:38 +020039void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +020040_PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
41 PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
46 /* XXX Should never happen -- fatal error instead? */
47 /* Well, it could be None. */
48 Py_DECREF(traceback);
49 traceback = NULL;
50 }
Guido van Rossuma027efa1997-05-05 20:56:21 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 /* Save these in locals to safeguard against recursive
53 invocation through Py_XDECREF */
54 oldtype = tstate->curexc_type;
55 oldvalue = tstate->curexc_value;
56 oldtraceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 tstate->curexc_type = type;
59 tstate->curexc_value = value;
60 tstate->curexc_traceback = traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 Py_XDECREF(oldtype);
63 Py_XDECREF(oldvalue);
64 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065}
66
Victor Stinnerb4bdecd2019-05-24 13:44:24 +020067void
68PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
69{
70 PyThreadState *tstate = _PyThreadState_GET();
71 _PyErr_Restore(tstate, type, value, traceback);
72}
73
74
Mark Shannonae3087c2017-10-22 22:41:51 +010075_PyErr_StackItem *
76_PyErr_GetTopmostException(PyThreadState *tstate)
77{
78 _PyErr_StackItem *exc_info = tstate->exc_info;
79 while ((exc_info->exc_type == NULL || exc_info->exc_type == Py_None) &&
80 exc_info->previous_item != NULL)
81 {
82 exc_info = exc_info->previous_item;
83 }
84 return exc_info;
85}
86
Victor Stinner3a840972016-08-22 23:59:08 +020087static PyObject*
88_PyErr_CreateException(PyObject *exception, PyObject *value)
89{
90 if (value == NULL || value == Py_None) {
91 return _PyObject_CallNoArg(exception);
92 }
93 else if (PyTuple_Check(value)) {
94 return PyObject_Call(exception, value, NULL);
95 }
96 else {
Petr Viktorinffd97532020-02-11 17:46:57 +010097 return PyObject_CallOneArg(exception, value);
Victor Stinner3a840972016-08-22 23:59:08 +020098 }
99}
100
Victor Stinner438a12d2019-05-24 17:01:38 +0200101void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200102_PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PyObject *exc_value;
105 PyObject *tb = NULL;
Guido van Rossumb4fb6e42008-06-14 20:20:24 +0000106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (exception != NULL &&
108 !PyExceptionClass_Check(exception)) {
Victor Stinner438a12d2019-05-24 17:01:38 +0200109 _PyErr_Format(tstate, PyExc_SystemError,
Victor Stinnerb0be6b32020-05-05 17:07:41 +0200110 "_PyErr_SetObject: "
111 "exception %R is not a BaseException subclass",
Victor Stinner438a12d2019-05-24 17:01:38 +0200112 exception);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 return;
114 }
Victor Stinner3a840972016-08-22 23:59:08 +0200115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 Py_XINCREF(value);
Mark Shannonae3087c2017-10-22 22:41:51 +0100117 exc_value = _PyErr_GetTopmostException(tstate)->exc_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 if (exc_value != NULL && exc_value != Py_None) {
119 /* Implicit exception chaining */
120 Py_INCREF(exc_value);
121 if (value == NULL || !PyExceptionInstance_Check(value)) {
122 /* We must normalize the value right now */
Victor Stinner3a840972016-08-22 23:59:08 +0200123 PyObject *fixed_value;
Victor Stinnerde821be2015-03-24 12:41:23 +0100124
Victor Stinner3a840972016-08-22 23:59:08 +0200125 /* Issue #23571: functions must not be called with an
Victor Stinnerde821be2015-03-24 12:41:23 +0100126 exception set */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200127 _PyErr_Clear(tstate);
Victor Stinnerde821be2015-03-24 12:41:23 +0100128
Victor Stinner3a840972016-08-22 23:59:08 +0200129 fixed_value = _PyErr_CreateException(exception, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 Py_XDECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +0200131 if (fixed_value == NULL) {
Alexey Izbysheva2eefa62018-08-26 19:31:25 +0300132 Py_DECREF(exc_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 return;
Victor Stinner3a840972016-08-22 23:59:08 +0200134 }
135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 value = fixed_value;
137 }
Victor Stinner3a840972016-08-22 23:59:08 +0200138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 /* Avoid reference cycles through the context chain.
140 This is O(chain length) but context chains are
141 usually very short. Sensitive readers may try
142 to inline the call to PyException_GetContext. */
143 if (exc_value != value) {
144 PyObject *o = exc_value, *context;
145 while ((context = PyException_GetContext(o))) {
146 Py_DECREF(context);
147 if (context == value) {
148 PyException_SetContext(o, NULL);
149 break;
150 }
151 o = context;
152 }
153 PyException_SetContext(value, exc_value);
Victor Stinner3a840972016-08-22 23:59:08 +0200154 }
155 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 Py_DECREF(exc_value);
157 }
158 }
159 if (value != NULL && PyExceptionInstance_Check(value))
160 tb = PyException_GetTraceback(value);
161 Py_XINCREF(exception);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200162 _PyErr_Restore(tstate, exception, value, tb);
163}
164
165void
166PyErr_SetObject(PyObject *exception, PyObject *value)
167{
168 PyThreadState *tstate = _PyThreadState_GET();
169 _PyErr_SetObject(tstate, exception, value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Raymond Hettinger69492da2013-09-02 15:59:26 -0700172/* Set a key error with the specified argument, wrapping it in a
173 * tuple automatically so that tuple keys are not unpacked as the
174 * exception arguments. */
175void
176_PyErr_SetKeyError(PyObject *arg)
177{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200178 PyThreadState *tstate = _PyThreadState_GET();
179 PyObject *tup = PyTuple_Pack(1, arg);
180 if (!tup) {
181 /* caller will expect error to be set anyway */
182 return;
183 }
184 _PyErr_SetObject(tstate, PyExc_KeyError, tup);
Raymond Hettinger69492da2013-09-02 15:59:26 -0700185 Py_DECREF(tup);
186}
187
Victor Stinner438a12d2019-05-24 17:01:38 +0200188void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200189_PyErr_SetNone(PyThreadState *tstate, PyObject *exception)
190{
191 _PyErr_SetObject(tstate, exception, (PyObject *)NULL);
192}
193
194
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200198 PyThreadState *tstate = _PyThreadState_GET();
199 _PyErr_SetNone(tstate, exception);
200}
201
202
Victor Stinner438a12d2019-05-24 17:01:38 +0200203void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200204_PyErr_SetString(PyThreadState *tstate, PyObject *exception,
205 const char *string)
206{
207 PyObject *value = PyUnicode_FromString(string);
208 _PyErr_SetObject(tstate, exception, value);
209 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210}
211
212void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200215 PyThreadState *tstate = _PyThreadState_GET();
216 _PyErr_SetString(tstate, exception, string);
217}
218
219
Victor Stinnerc6944e72016-11-11 02:13:35 +0100220PyObject* _Py_HOT_FUNCTION
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222{
Victor Stinnerd12d0e72019-11-07 12:42:07 +0100223 /* The caller must hold the GIL. */
224 assert(PyGILState_Check());
225
Victor Stinner50b48572018-11-01 01:51:40 +0100226 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200227 return _PyErr_Occurred(tstate);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228}
229
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000230
231int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 if (err == NULL || exc == NULL) {
235 /* maybe caused by "import exceptions" that failed early on */
236 return 0;
237 }
238 if (PyTuple_Check(exc)) {
239 Py_ssize_t i, n;
240 n = PyTuple_Size(exc);
241 for (i = 0; i < n; i++) {
242 /* Test recursively */
243 if (PyErr_GivenExceptionMatches(
244 err, PyTuple_GET_ITEM(exc, i)))
245 {
246 return 1;
247 }
248 }
249 return 0;
250 }
251 /* err might be an instance, so check its class. */
252 if (PyExceptionInstance_Check(err))
253 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
scodere4c06bc2017-07-31 22:27:46 +0200256 return PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000260}
Guido van Rossum743007d1999-04-21 15:27:31 +0000261
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000262
263int
Victor Stinner438a12d2019-05-24 17:01:38 +0200264_PyErr_ExceptionMatches(PyThreadState *tstate, PyObject *exc)
265{
266 return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc);
267}
268
269
270int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000272{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200273 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner438a12d2019-05-24 17:01:38 +0200274 return _PyErr_ExceptionMatches(tstate, exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000275}
276
277
xdegaye56d1f5c2017-10-26 15:09:06 +0200278#ifndef Py_NORMALIZE_RECURSION_LIMIT
279#define Py_NORMALIZE_RECURSION_LIMIT 32
280#endif
281
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000282/* Used in many places to normalize a raised exception, including in
283 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000284
285 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000287*/
Victor Stinner438a12d2019-05-24 17:01:38 +0200288void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200289_PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
290 PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000291{
Serhiy Storchakacf296532017-11-05 11:27:48 +0200292 int recursion_depth = 0;
293 PyObject *type, *value, *initial_tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000294
Serhiy Storchakacf296532017-11-05 11:27:48 +0200295 restart:
296 type = *exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (type == NULL) {
298 /* There was no exception, so nothing to do. */
299 return;
300 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000301
Serhiy Storchakacf296532017-11-05 11:27:48 +0200302 value = *val;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 /* If PyErr_SetNone() was used, the value will have been actually
304 set to NULL.
305 */
306 if (!value) {
307 value = Py_None;
308 Py_INCREF(value);
309 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* Normalize the exception so that if the type is a class, the
312 value will be an instance.
313 */
314 if (PyExceptionClass_Check(type)) {
Serhiy Storchakacf296532017-11-05 11:27:48 +0200315 PyObject *inclass = NULL;
316 int is_subclass = 0;
Victor Stinner74a7fa62013-07-17 00:44:53 +0200317
Serhiy Storchakacf296532017-11-05 11:27:48 +0200318 if (PyExceptionInstance_Check(value)) {
319 inclass = PyExceptionInstance_Class(value);
320 is_subclass = PyObject_IsSubclass(inclass, type);
321 if (is_subclass < 0) {
322 goto error;
323 }
324 }
325
326 /* If the value was not an instance, or is not an instance
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 whose class is (or is derived from) type, then use the
328 value as an argument to instantiation of the type
329 class.
330 */
Serhiy Storchakacf296532017-11-05 11:27:48 +0200331 if (!is_subclass) {
332 PyObject *fixed_value = _PyErr_CreateException(type, value);
Victor Stinner3a840972016-08-22 23:59:08 +0200333 if (fixed_value == NULL) {
Serhiy Storchakacf296532017-11-05 11:27:48 +0200334 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 Py_DECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +0200337 value = fixed_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 }
Serhiy Storchakacf296532017-11-05 11:27:48 +0200339 /* If the class of the instance doesn't exactly match the
340 class of the type, believe the instance.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 */
342 else if (inclass != type) {
Serhiy Storchakacf296532017-11-05 11:27:48 +0200343 Py_INCREF(inclass);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_DECREF(type);
345 type = inclass;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 }
347 }
348 *exc = type;
349 *val = value;
350 return;
Serhiy Storchakacf296532017-11-05 11:27:48 +0200351
352 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_DECREF(type);
354 Py_DECREF(value);
Serhiy Storchakacf296532017-11-05 11:27:48 +0200355 recursion_depth++;
356 if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200357 _PyErr_SetString(tstate, PyExc_RecursionError,
358 "maximum recursion depth exceeded "
359 "while normalizing an exception");
xdegaye56d1f5c2017-10-26 15:09:06 +0200360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* If the new exception doesn't set a traceback and the old
362 exception had a traceback, use the old traceback for the
363 new exception. It's better than nothing.
364 */
365 initial_tb = *tb;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200366 _PyErr_Fetch(tstate, exc, val, tb);
Serhiy Storchakacf296532017-11-05 11:27:48 +0200367 assert(*exc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (initial_tb != NULL) {
369 if (*tb == NULL)
370 *tb = initial_tb;
371 else
372 Py_DECREF(initial_tb);
373 }
Serhiy Storchakacf296532017-11-05 11:27:48 +0200374 /* Abort when Py_NORMALIZE_RECURSION_LIMIT has been exceeded, and the
375 corresponding RecursionError could not be normalized, and the
376 MemoryError raised when normalize this RecursionError could not be
377 normalized. */
378 if (recursion_depth >= Py_NORMALIZE_RECURSION_LIMIT + 2) {
xdegaye56d1f5c2017-10-26 15:09:06 +0200379 if (PyErr_GivenExceptionMatches(*exc, PyExc_MemoryError)) {
380 Py_FatalError("Cannot recover from MemoryErrors "
381 "while normalizing exceptions.");
382 }
383 else {
384 Py_FatalError("Cannot recover from the recursive normalization "
385 "of an exception.");
386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 }
Serhiy Storchakacf296532017-11-05 11:27:48 +0200388 goto restart;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000389}
390
391
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200393PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Victor Stinner50b48572018-11-01 01:51:40 +0100395 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200396 _PyErr_NormalizeException(tstate, exc, val, tb);
397}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000398
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200399
Victor Stinner438a12d2019-05-24 17:01:38 +0200400void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200401_PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value,
402 PyObject **p_traceback)
403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 *p_type = tstate->curexc_type;
405 *p_value = tstate->curexc_value;
406 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 tstate->curexc_type = NULL;
409 tstate->curexc_value = NULL;
410 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411}
412
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200413
414void
415PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
416{
417 PyThreadState *tstate = _PyThreadState_GET();
418 _PyErr_Fetch(tstate, p_type, p_value, p_traceback);
419}
420
421
Victor Stinner438a12d2019-05-24 17:01:38 +0200422void
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200423_PyErr_Clear(PyThreadState *tstate)
424{
425 _PyErr_Restore(tstate, NULL, NULL, NULL);
426}
427
428
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200432 PyThreadState *tstate = _PyThreadState_GET();
433 _PyErr_Clear(tstate);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000435
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200436
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200437void
Julien Danjou3430c552020-01-13 17:30:14 +0100438_PyErr_GetExcInfo(PyThreadState *tstate,
439 PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200440{
Mark Shannonae3087c2017-10-22 22:41:51 +0100441 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
442 *p_type = exc_info->exc_type;
443 *p_value = exc_info->exc_value;
444 *p_traceback = exc_info->exc_traceback;
445
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200446 Py_XINCREF(*p_type);
447 Py_XINCREF(*p_value);
448 Py_XINCREF(*p_traceback);
449}
450
Julien Danjou3430c552020-01-13 17:30:14 +0100451
452void
453PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
454{
455 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner3f12ac12020-01-15 11:23:25 +0100456 _PyErr_GetExcInfo(tstate, p_type, p_value, p_traceback);
Julien Danjou3430c552020-01-13 17:30:14 +0100457}
458
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200459void
460PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
461{
462 PyObject *oldtype, *oldvalue, *oldtraceback;
Victor Stinner50b48572018-11-01 01:51:40 +0100463 PyThreadState *tstate = _PyThreadState_GET();
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200464
Mark Shannonae3087c2017-10-22 22:41:51 +0100465 oldtype = tstate->exc_info->exc_type;
466 oldvalue = tstate->exc_info->exc_value;
467 oldtraceback = tstate->exc_info->exc_traceback;
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200468
Mark Shannonae3087c2017-10-22 22:41:51 +0100469 tstate->exc_info->exc_type = p_type;
470 tstate->exc_info->exc_value = p_value;
471 tstate->exc_info->exc_traceback = p_traceback;
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200472
473 Py_XDECREF(oldtype);
474 Py_XDECREF(oldvalue);
475 Py_XDECREF(oldtraceback);
476}
477
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300478/* Like PyErr_Restore(), but if an exception is already set,
479 set the context associated with it.
480 */
481void
482_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
483{
484 if (exc == NULL)
485 return;
486
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200487 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb0be6b32020-05-05 17:07:41 +0200488
489 if (!PyExceptionClass_Check(exc)) {
490 _PyErr_Format(tstate, PyExc_SystemError,
491 "_PyErr_ChainExceptions: "
492 "exception %R is not a BaseException subclass",
493 exc);
494 return;
495 }
496
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200497 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300498 PyObject *exc2, *val2, *tb2;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200499 _PyErr_Fetch(tstate, &exc2, &val2, &tb2);
500 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Serhiy Storchaka9e373be2016-10-21 16:19:59 +0300501 if (tb != NULL) {
502 PyException_SetTraceback(val, tb);
503 Py_DECREF(tb);
504 }
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300505 Py_DECREF(exc);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200506 _PyErr_NormalizeException(tstate, &exc2, &val2, &tb2);
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300507 PyException_SetContext(val2, val);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200508 _PyErr_Restore(tstate, exc2, val2, tb2);
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300509 }
510 else {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200511 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300512 }
513}
514
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300515static PyObject *
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200516_PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
517 const char *format, va_list vargs)
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300518{
519 PyObject *exc, *val, *val2, *tb;
520
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200521 assert(_PyErr_Occurred(tstate));
522 _PyErr_Fetch(tstate, &exc, &val, &tb);
523 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300524 if (tb != NULL) {
525 PyException_SetTraceback(val, tb);
526 Py_DECREF(tb);
527 }
528 Py_DECREF(exc);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200529 assert(!_PyErr_Occurred(tstate));
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300530
Victor Stinner438a12d2019-05-24 17:01:38 +0200531 _PyErr_FormatV(tstate, exception, format, vargs);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300532
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200533 _PyErr_Fetch(tstate, &exc, &val2, &tb);
534 _PyErr_NormalizeException(tstate, &exc, &val2, &tb);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300535 Py_INCREF(val);
536 PyException_SetCause(val2, val);
537 PyException_SetContext(val2, val);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200538 _PyErr_Restore(tstate, exc, val2, tb);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300539
540 return NULL;
541}
542
543PyObject *
Victor Stinner17269092019-11-05 01:22:12 +0100544_PyErr_FormatFromCauseTstate(PyThreadState *tstate, PyObject *exception,
545 const char *format, ...)
546{
547 va_list vargs;
548#ifdef HAVE_STDARG_PROTOTYPES
549 va_start(vargs, format);
550#else
551 va_start(vargs);
552#endif
553 _PyErr_FormatVFromCause(tstate, exception, format, vargs);
554 va_end(vargs);
555 return NULL;
556}
557
558PyObject *
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300559_PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
560{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200561 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300562 va_list vargs;
563#ifdef HAVE_STDARG_PROTOTYPES
564 va_start(vargs, format);
565#else
566 va_start(vargs);
567#endif
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200568 _PyErr_FormatVFromCause(tstate, exception, format, vargs);
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300569 va_end(vargs);
570 return NULL;
571}
572
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000573/* Convenience functions to set a type error exception and return 0 */
574
575int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000576PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000577{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200578 PyThreadState *tstate = _PyThreadState_GET();
579 _PyErr_SetString(tstate, PyExc_TypeError,
580 "bad argument type for built-in operation");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000582}
583
Guido van Rossum373c8691997-04-29 18:22:47 +0000584PyObject *
Victor Stinner838f2642019-06-13 22:41:23 +0200585_PyErr_NoMemory(PyThreadState *tstate)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000586{
Andy Lesterdffe4c02020-03-04 07:15:20 -0600587 if (Py_IS_TYPE(PyExc_MemoryError, NULL)) {
Victor Stinnerf54a5742013-07-22 22:28:37 +0200588 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
589 initialized by _PyExc_Init() */
590 Py_FatalError("Out of memory and PyExc_MemoryError is not "
591 "initialized yet");
592 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200593 _PyErr_SetNone(tstate, PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000595}
596
Guido van Rossum373c8691997-04-29 18:22:47 +0000597PyObject *
Victor Stinner838f2642019-06-13 22:41:23 +0200598PyErr_NoMemory(void)
599{
600 PyThreadState *tstate = _PyThreadState_GET();
601 return _PyErr_NoMemory(tstate);
602}
603
604PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000605PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000606{
Larry Hastingsb0827312014-02-09 22:05:19 -0800607 return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
608}
609
610PyObject *
611PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
612{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200613 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200615 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100617#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000619#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000620
Guido van Rossume9fbc091995-02-18 14:52:19 +0000621#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 if (i == EINTR && PyErr_CheckSignals())
623 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000624#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000625
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000626#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100627 if (i != 0) {
Andy Lester7386a702020-02-13 22:42:56 -0600628 const char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100629 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100630 }
631 else {
632 /* Sometimes errno didn't get set */
633 message = PyUnicode_FromString("Error");
634 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000635#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (i == 0)
637 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
638 else
639 {
640 /* Note that the Win32 errors do not lineup with the
641 errno error. So if the error is in the MSVC error
642 table, we use it, otherwise we assume it really _is_
643 a Win32 error code
644 */
645 if (i > 0 && i < _sys_nerr) {
646 message = PyUnicode_FromString(_sys_errlist[i]);
647 }
648 else {
649 int len = FormatMessageW(
650 FORMAT_MESSAGE_ALLOCATE_BUFFER |
651 FORMAT_MESSAGE_FROM_SYSTEM |
652 FORMAT_MESSAGE_IGNORE_INSERTS,
653 NULL, /* no message source */
654 i,
655 MAKELANGID(LANG_NEUTRAL,
656 SUBLANG_DEFAULT),
657 /* Default language */
658 (LPWSTR) &s_buf,
659 0, /* size not used */
660 NULL); /* no args */
661 if (len==0) {
662 /* Only ever seen this in out-of-mem
663 situations */
664 s_buf = NULL;
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300665 message = PyUnicode_FromFormat("Windows Error 0x%x", i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 } else {
667 /* remove trailing cr/lf and dots */
668 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
669 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200670 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 }
672 }
673 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000674#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (message == NULL)
677 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000678#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000680#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return NULL;
682 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000683
Larry Hastingsb0827312014-02-09 22:05:19 -0800684 if (filenameObject != NULL) {
685 if (filenameObject2 != NULL)
686 args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
687 else
688 args = Py_BuildValue("(iOO)", i, message, filenameObject);
689 } else {
690 assert(filenameObject2 == NULL);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200691 args = Py_BuildValue("(iO)", i, message);
Larry Hastingsb0827312014-02-09 22:05:19 -0800692 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000694
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200695 if (args != NULL) {
696 v = PyObject_Call(exc, args, NULL);
697 Py_DECREF(args);
698 if (v != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200699 _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200700 Py_DECREF(v);
701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000703#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000705#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000707}
Guido van Rossum743007d1999-04-21 15:27:31 +0000708
Barry Warsaw97d95151998-07-23 16:05:56 +0000709PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000710PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800713 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 Py_XDECREF(name);
715 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716}
717
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000718#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000719PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000720PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000721{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200722 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800723 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 Py_XDECREF(name);
725 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000726}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000727#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000728
729PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000730PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000731{
Larry Hastingsb0827312014-02-09 22:05:19 -0800732 return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000733}
Guido van Rossum683a0721990-10-21 22:09:12 +0000734
Brett Cannonbf364092006-03-01 04:25:17 +0000735#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000736/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000737PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 PyObject *exc,
739 int ierr,
740 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000741{
Larry Hastingsb0827312014-02-09 22:05:19 -0800742 return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
743 filenameObject, NULL);
744}
745
746PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
747 PyObject *exc,
748 int ierr,
749 PyObject *filenameObject,
750 PyObject *filenameObject2)
751{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200752 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 int len;
754 WCHAR *s_buf = NULL; /* Free via LocalFree */
755 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200756 PyObject *args, *v;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 DWORD err = (DWORD)ierr;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200759 if (err==0) {
760 err = GetLastError();
761 }
762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 len = FormatMessageW(
764 /* Error API error */
765 FORMAT_MESSAGE_ALLOCATE_BUFFER |
766 FORMAT_MESSAGE_FROM_SYSTEM |
767 FORMAT_MESSAGE_IGNORE_INSERTS,
768 NULL, /* no message source */
769 err,
770 MAKELANGID(LANG_NEUTRAL,
771 SUBLANG_DEFAULT), /* Default language */
772 (LPWSTR) &s_buf,
773 0, /* size not used */
774 NULL); /* no args */
775 if (len==0) {
776 /* Only seen this in out of mem situations */
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300777 message = PyUnicode_FromFormat("Windows Error 0x%x", err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 s_buf = NULL;
779 } else {
780 /* remove trailing cr/lf and dots */
781 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
782 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200783 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (message == NULL)
787 {
788 LocalFree(s_buf);
789 return NULL;
790 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000791
Larry Hastingsb0827312014-02-09 22:05:19 -0800792 if (filenameObject == NULL) {
793 assert(filenameObject2 == NULL);
794 filenameObject = filenameObject2 = Py_None;
795 }
796 else if (filenameObject2 == NULL)
797 filenameObject2 = Py_None;
798 /* This is the constructor signature for OSError.
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200799 The POSIX translation will be figured out by the constructor. */
Larry Hastingsb0827312014-02-09 22:05:19 -0800800 args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000802
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200803 if (args != NULL) {
804 v = PyObject_Call(exc, args, NULL);
805 Py_DECREF(args);
806 if (v != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200807 _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200808 Py_DECREF(v);
809 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 }
811 LocalFree(s_buf);
812 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000813}
814
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000815PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyObject *exc,
817 int ierr,
818 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000819{
Victor Stinner92be9392010-12-28 00:28:21 +0000820 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800821 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800823 name,
824 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 Py_XDECREF(name);
826 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000827}
828
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000829PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 PyObject *exc,
831 int ierr,
832 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000833{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200834 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800835 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800837 name,
838 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 Py_XDECREF(name);
840 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000841}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000842
Thomas Heller085358a2002-07-29 14:27:41 +0000843PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
844{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800845 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000846}
847
Guido van Rossum795e1892000-02-17 15:19:15 +0000848PyObject *PyErr_SetFromWindowsErr(int ierr)
849{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800850 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
851 ierr, NULL);
Larry Hastingsb0827312014-02-09 22:05:19 -0800852}
853
Thomas Heller085358a2002-07-29 14:27:41 +0000854PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 int ierr,
856 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000857{
Victor Stinner92be9392010-12-28 00:28:21 +0000858 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800859 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200860 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800861 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_XDECREF(name);
863 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000864}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000865
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000866PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 int ierr,
868 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000869{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200870 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800871 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200872 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800873 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 Py_XDECREF(name);
875 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000876}
Guido van Rossum795e1892000-02-17 15:19:15 +0000877#endif /* MS_WINDOWS */
878
Brett Cannon79ec55e2012-04-12 20:24:54 -0400879PyObject *
Eric Snow46f97b82016-09-07 16:56:15 -0700880PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
881 PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400882{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200883 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow46f97b82016-09-07 16:56:15 -0700884 int issubclass;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200885 PyObject *kwargs, *error;
Brian Curtin09b86d12012-04-17 16:57:09 -0500886
Eric Snow46f97b82016-09-07 16:56:15 -0700887 issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
888 if (issubclass < 0) {
889 return NULL;
890 }
891 else if (!issubclass) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200892 _PyErr_SetString(tstate, PyExc_TypeError,
893 "expected a subclass of ImportError");
Brian Curtin94c001b2012-04-18 08:30:51 -0500894 return NULL;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200895 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500896
Eric Snow46f97b82016-09-07 16:56:15 -0700897 if (msg == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200898 _PyErr_SetString(tstate, PyExc_TypeError,
899 "expected a message argument");
Brian Curtin09b86d12012-04-17 16:57:09 -0500900 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400901 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500902
Brian Curtin94c001b2012-04-18 08:30:51 -0500903 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500904 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500905 }
Brian Curtin94c001b2012-04-18 08:30:51 -0500906 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500907 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500908 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500909
Eric Snow46f97b82016-09-07 16:56:15 -0700910 kwargs = PyDict_New();
911 if (kwargs == NULL) {
912 return NULL;
913 }
Victor Stinnerf45a5612016-08-23 00:04:41 +0200914 if (PyDict_SetItemString(kwargs, "name", name) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300915 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200916 }
917 if (PyDict_SetItemString(kwargs, "path", path) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300918 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200919 }
Brett Cannon79ec55e2012-04-12 20:24:54 -0400920
Petr Viktorinffd97532020-02-11 17:46:57 +0100921 error = PyObject_VectorcallDict(exception, &msg, 1, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400922 if (error != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200923 _PyErr_SetObject(tstate, (PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500924 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400925 }
926
Berker Peksagec766d32016-05-01 09:06:36 +0300927done:
Brett Cannon79ec55e2012-04-12 20:24:54 -0400928 Py_DECREF(kwargs);
Brian Curtin09b86d12012-04-17 16:57:09 -0500929 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400930}
931
Eric Snow46f97b82016-09-07 16:56:15 -0700932PyObject *
933PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
934{
935 return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
936}
937
Guido van Rossum683a0721990-10-21 22:09:12 +0000938void
Neal Norwitzb382b842007-08-24 20:00:37 +0000939_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000940{
Victor Stinner438a12d2019-05-24 17:01:38 +0200941 PyThreadState *tstate = _PyThreadState_GET();
942 _PyErr_Format(tstate, PyExc_SystemError,
943 "%s:%d: bad argument to internal function",
944 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000945}
946
947/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
948 export the entry point for existing object code: */
949#undef PyErr_BadInternalCall
950void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000951PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000952{
Victor Stinnerfb3a6302013-07-12 00:37:30 +0200953 assert(0 && "bad argument to internal function");
Victor Stinner438a12d2019-05-24 17:01:38 +0200954 PyThreadState *tstate = _PyThreadState_GET();
955 _PyErr_SetString(tstate, PyExc_SystemError,
956 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000957}
Fred Drake6d63adf2000-08-24 22:38:39 +0000958#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
959
Guido van Rossum1548bac1997-02-14 17:09:47 +0000960
Victor Stinner438a12d2019-05-24 17:01:38 +0200961static PyObject *
962_PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
963 const char *format, va_list vargs)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000966
Victor Stinnerde821be2015-03-24 12:41:23 +0100967 /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
968 exception set, it calls arbitrary Python code like PyObject_Repr() */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200969 _PyErr_Clear(tstate);
Victor Stinnerace47d72013-07-18 01:41:08 +0200970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 string = PyUnicode_FromFormatV(format, vargs);
Victor Stinnerde821be2015-03-24 12:41:23 +0100972
Victor Stinnerb4bdecd2019-05-24 13:44:24 +0200973 _PyErr_SetObject(tstate, exception, string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_XDECREF(string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000976}
Guido van Rossum7617e051997-09-16 18:43:50 +0000977
978
Antoine Pitrou0676a402014-09-30 21:16:27 +0200979PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +0200980PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
981{
982 PyThreadState *tstate = _PyThreadState_GET();
983 return _PyErr_FormatV(tstate, exception, format, vargs);
984}
985
986
987PyObject *
988_PyErr_Format(PyThreadState *tstate, PyObject *exception,
989 const char *format, ...)
Antoine Pitrou0676a402014-09-30 21:16:27 +0200990{
991 va_list vargs;
992#ifdef HAVE_STDARG_PROTOTYPES
993 va_start(vargs, format);
994#else
995 va_start(vargs);
996#endif
Victor Stinner438a12d2019-05-24 17:01:38 +0200997 _PyErr_FormatV(tstate, exception, format, vargs);
998 va_end(vargs);
999 return NULL;
1000}
1001
1002
1003PyObject *
1004PyErr_Format(PyObject *exception, const char *format, ...)
1005{
1006 PyThreadState *tstate = _PyThreadState_GET();
1007 va_list vargs;
1008#ifdef HAVE_STDARG_PROTOTYPES
1009 va_start(vargs, format);
1010#else
1011 va_start(vargs);
1012#endif
1013 _PyErr_FormatV(tstate, exception, format, vargs);
Antoine Pitrou0676a402014-09-30 21:16:27 +02001014 va_end(vargs);
1015 return NULL;
1016}
1017
Thomas Wouters477c8d52006-05-27 19:21:47 +00001018
Guido van Rossum7617e051997-09-16 18:43:50 +00001019PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +00001020PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +00001021{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001022 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyObject *modulename = NULL;
1024 PyObject *classname = NULL;
1025 PyObject *mydict = NULL;
1026 PyObject *bases = NULL;
1027 PyObject *result = NULL;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001028
1029 const char *dot = strrchr(name, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (dot == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001031 _PyErr_SetString(tstate, PyExc_SystemError,
1032 "PyErr_NewException: name must be module.class");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 return NULL;
1034 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001035 if (base == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 base = PyExc_Exception;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001037 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (dict == NULL) {
1039 dict = mydict = PyDict_New();
1040 if (dict == NULL)
1041 goto failure;
1042 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001043
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001044 if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001045 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001046 goto failure;
1047 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 modulename = PyUnicode_FromStringAndSize(name,
1049 (Py_ssize_t)(dot-name));
1050 if (modulename == NULL)
1051 goto failure;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001052 if (_PyDict_SetItemId(dict, &PyId___module__, modulename) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 goto failure;
1054 }
1055 if (PyTuple_Check(base)) {
1056 bases = base;
1057 /* INCREF as we create a new ref in the else branch */
1058 Py_INCREF(bases);
1059 } else {
1060 bases = PyTuple_Pack(1, base);
1061 if (bases == NULL)
1062 goto failure;
1063 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +01001064 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001065 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +00001067 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 Py_XDECREF(bases);
1069 Py_XDECREF(mydict);
1070 Py_XDECREF(classname);
1071 Py_XDECREF(modulename);
1072 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +00001073}
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001074
Georg Brandl1e28a272009-12-28 08:41:01 +00001075
1076/* Create an exception with docstring */
1077PyObject *
1078PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +00001080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 int result;
1082 PyObject *ret = NULL;
1083 PyObject *mydict = NULL; /* points to the dict only if we create it */
1084 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (dict == NULL) {
1087 dict = mydict = PyDict_New();
1088 if (dict == NULL) {
1089 return NULL;
1090 }
1091 }
Georg Brandl1e28a272009-12-28 08:41:01 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (doc != NULL) {
1094 docobj = PyUnicode_FromString(doc);
1095 if (docobj == NULL)
1096 goto failure;
1097 result = PyDict_SetItemString(dict, "__doc__", docobj);
1098 Py_DECREF(docobj);
1099 if (result < 0)
1100 goto failure;
1101 }
Georg Brandl1e28a272009-12-28 08:41:01 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +00001104 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 Py_XDECREF(mydict);
1106 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +00001107}
1108
1109
Victor Stinneref9d9b62019-05-22 11:28:22 +02001110PyDoc_STRVAR(UnraisableHookArgs__doc__,
1111"UnraisableHookArgs\n\
1112\n\
1113Type used to pass arguments to sys.unraisablehook.");
1114
1115static PyTypeObject UnraisableHookArgsType;
1116
1117static PyStructSequence_Field UnraisableHookArgs_fields[] = {
1118 {"exc_type", "Exception type"},
1119 {"exc_value", "Exception value"},
1120 {"exc_traceback", "Exception traceback"},
Victor Stinner71c52e32019-05-27 08:57:14 +02001121 {"err_msg", "Error message"},
Victor Stinneref9d9b62019-05-22 11:28:22 +02001122 {"object", "Object causing the exception"},
1123 {0}
1124};
1125
1126static PyStructSequence_Desc UnraisableHookArgs_desc = {
1127 .name = "UnraisableHookArgs",
1128 .doc = UnraisableHookArgs__doc__,
1129 .fields = UnraisableHookArgs_fields,
Victor Stinner71c52e32019-05-27 08:57:14 +02001130 .n_in_sequence = 5
Victor Stinneref9d9b62019-05-22 11:28:22 +02001131};
1132
1133
Victor Stinner331a6a52019-05-27 16:39:22 +02001134PyStatus
Victor Stinneref9d9b62019-05-22 11:28:22 +02001135_PyErr_Init(void)
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001136{
Victor Stinneref9d9b62019-05-22 11:28:22 +02001137 if (UnraisableHookArgsType.tp_name == NULL) {
1138 if (PyStructSequence_InitType2(&UnraisableHookArgsType,
1139 &UnraisableHookArgs_desc) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001140 return _PyStatus_ERR("failed to initialize UnraisableHookArgs type");
Martin Panter3263f682016-02-28 03:16:11 +00001141 }
Victor Stinneref9d9b62019-05-22 11:28:22 +02001142 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001143 return _PyStatus_OK();
Victor Stinneref9d9b62019-05-22 11:28:22 +02001144}
1145
1146
1147static PyObject *
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001148make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type,
Victor Stinner71c52e32019-05-27 08:57:14 +02001149 PyObject *exc_value, PyObject *exc_tb,
1150 PyObject *err_msg, PyObject *obj)
Victor Stinneref9d9b62019-05-22 11:28:22 +02001151{
1152 PyObject *args = PyStructSequence_New(&UnraisableHookArgsType);
1153 if (args == NULL) {
1154 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001156
Victor Stinneref9d9b62019-05-22 11:28:22 +02001157 Py_ssize_t pos = 0;
1158#define ADD_ITEM(exc_type) \
1159 do { \
1160 if (exc_type == NULL) { \
1161 exc_type = Py_None; \
1162 } \
1163 Py_INCREF(exc_type); \
1164 PyStructSequence_SET_ITEM(args, pos++, exc_type); \
1165 } while (0)
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001166
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001167
Victor Stinneref9d9b62019-05-22 11:28:22 +02001168 ADD_ITEM(exc_type);
1169 ADD_ITEM(exc_value);
1170 ADD_ITEM(exc_tb);
Victor Stinner71c52e32019-05-27 08:57:14 +02001171 ADD_ITEM(err_msg);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001172 ADD_ITEM(obj);
1173#undef ADD_ITEM
1174
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001175 if (_PyErr_Occurred(tstate)) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001176 Py_DECREF(args);
1177 return NULL;
1178 }
1179 return args;
1180}
1181
1182
1183
1184/* Default implementation of sys.unraisablehook.
1185
1186 It can be called to log the exception of a custom sys.unraisablehook.
1187
1188 Do nothing if sys.stderr attribute doesn't exist or is set to None. */
1189static int
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001190write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
1191 PyObject *exc_value, PyObject *exc_tb,
Victor Stinner71c52e32019-05-27 08:57:14 +02001192 PyObject *err_msg, PyObject *obj, PyObject *file)
Victor Stinneref9d9b62019-05-22 11:28:22 +02001193{
1194 if (obj != NULL && obj != Py_None) {
Victor Stinner71c52e32019-05-27 08:57:14 +02001195 if (err_msg != NULL && err_msg != Py_None) {
1196 if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) {
1197 return -1;
1198 }
1199 if (PyFile_WriteString(": ", file) < 0) {
1200 return -1;
1201 }
1202 }
1203 else {
1204 if (PyFile_WriteString("Exception ignored in: ", file) < 0) {
1205 return -1;
1206 }
Victor Stinneref9d9b62019-05-22 11:28:22 +02001207 }
1208
1209 if (PyFile_WriteObject(obj, file, 0) < 0) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001210 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001211 if (PyFile_WriteString("<object repr() failed>", file) < 0) {
1212 return -1;
1213 }
1214 }
1215 if (PyFile_WriteString("\n", file) < 0) {
1216 return -1;
1217 }
1218 }
Victor Stinner71c52e32019-05-27 08:57:14 +02001219 else if (err_msg != NULL && err_msg != Py_None) {
1220 if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) {
1221 return -1;
1222 }
1223 if (PyFile_WriteString(":\n", file) < 0) {
1224 return -1;
1225 }
1226 }
Victor Stinneref9d9b62019-05-22 11:28:22 +02001227
1228 if (exc_tb != NULL && exc_tb != Py_None) {
1229 if (PyTraceBack_Print(exc_tb, file) < 0) {
1230 /* continue even if writing the traceback failed */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001231 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001232 }
1233 }
1234
Victor Stinnerdf22c032019-05-23 01:00:58 +02001235 if (exc_type == NULL || exc_type == Py_None) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001236 return -1;
1237 }
1238
1239 assert(PyExceptionClass_Check(exc_type));
1240 const char *className = PyExceptionClass_Name(exc_type);
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001241 if (className != NULL) {
Serhiy Storchakaceeef102018-06-15 11:09:43 +03001242 const char *dot = strrchr(className, '.');
Victor Stinner71c52e32019-05-27 08:57:14 +02001243 if (dot != NULL) {
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001244 className = dot+1;
Victor Stinner71c52e32019-05-27 08:57:14 +02001245 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001246 }
1247
Victor Stinneref9d9b62019-05-22 11:28:22 +02001248 PyObject *moduleName = _PyObject_GetAttrId(exc_type, &PyId___module__);
Oren Milmanf6e61df2017-09-14 01:30:05 +03001249 if (moduleName == NULL || !PyUnicode_Check(moduleName)) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001250 Py_XDECREF(moduleName);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001251 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001252 if (PyFile_WriteString("<unknown>", file) < 0) {
1253 return -1;
1254 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001255 }
1256 else {
Serhiy Storchakaf5894dd2016-11-16 15:40:39 +02001257 if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001258 if (PyFile_WriteObject(moduleName, file, Py_PRINT_RAW) < 0) {
1259 Py_DECREF(moduleName);
1260 return -1;
1261 }
1262 Py_DECREF(moduleName);
1263 if (PyFile_WriteString(".", file) < 0) {
1264 return -1;
1265 }
1266 }
1267 else {
1268 Py_DECREF(moduleName);
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001269 }
1270 }
1271 if (className == NULL) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001272 if (PyFile_WriteString("<unknown>", file) < 0) {
1273 return -1;
1274 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001275 }
1276 else {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001277 if (PyFile_WriteString(className, file) < 0) {
1278 return -1;
1279 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001280 }
1281
Victor Stinneref9d9b62019-05-22 11:28:22 +02001282 if (exc_value && exc_value != Py_None) {
1283 if (PyFile_WriteString(": ", file) < 0) {
1284 return -1;
1285 }
1286 if (PyFile_WriteObject(exc_value, file, Py_PRINT_RAW) < 0) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001287 _PyErr_Clear(tstate);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001288 if (PyFile_WriteString("<exception str() failed>", file) < 0) {
1289 return -1;
Martin Panter3263f682016-02-28 03:16:11 +00001290 }
1291 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001292 }
Victor Stinnerdf22c032019-05-23 01:00:58 +02001293
Victor Stinneref9d9b62019-05-22 11:28:22 +02001294 if (PyFile_WriteString("\n", file) < 0) {
1295 return -1;
1296 }
Victor Stinnera85a1d32019-05-28 16:01:17 +02001297
1298 /* Explicitly call file.flush() */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001299 PyObject *res = _PyObject_CallMethodIdNoArgs(file, &PyId_flush);
Victor Stinnera85a1d32019-05-28 16:01:17 +02001300 if (!res) {
1301 return -1;
1302 }
1303 Py_DECREF(res);
1304
Victor Stinneref9d9b62019-05-22 11:28:22 +02001305 return 0;
1306}
1307
1308
1309static int
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001310write_unraisable_exc(PyThreadState *tstate, PyObject *exc_type,
Victor Stinner71c52e32019-05-27 08:57:14 +02001311 PyObject *exc_value, PyObject *exc_tb, PyObject *err_msg,
1312 PyObject *obj)
Victor Stinneref9d9b62019-05-22 11:28:22 +02001313{
1314 PyObject *file = _PySys_GetObjectId(&PyId_stderr);
1315 if (file == NULL || file == Py_None) {
1316 return 0;
1317 }
1318
1319 /* Hold a strong reference to ensure that sys.stderr doesn't go away
1320 while we use it */
1321 Py_INCREF(file);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001322 int res = write_unraisable_exc_file(tstate, exc_type, exc_value, exc_tb,
Victor Stinner71c52e32019-05-27 08:57:14 +02001323 err_msg, obj, file);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001324 Py_DECREF(file);
1325
1326 return res;
1327}
1328
1329
1330PyObject*
1331_PyErr_WriteUnraisableDefaultHook(PyObject *args)
1332{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001333 PyThreadState *tstate = _PyThreadState_GET();
1334
Andy Lester55728702020-03-06 16:53:17 -06001335 if (!Py_IS_TYPE(args, &UnraisableHookArgsType)) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001336 _PyErr_SetString(tstate, PyExc_TypeError,
1337 "sys.unraisablehook argument type "
1338 "must be UnraisableHookArgs");
Victor Stinneref9d9b62019-05-22 11:28:22 +02001339 return NULL;
1340 }
1341
1342 /* Borrowed references */
1343 PyObject *exc_type = PyStructSequence_GET_ITEM(args, 0);
1344 PyObject *exc_value = PyStructSequence_GET_ITEM(args, 1);
1345 PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
Victor Stinner71c52e32019-05-27 08:57:14 +02001346 PyObject *err_msg = PyStructSequence_GET_ITEM(args, 3);
1347 PyObject *obj = PyStructSequence_GET_ITEM(args, 4);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001348
Victor Stinner71c52e32019-05-27 08:57:14 +02001349 if (write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, err_msg, obj) < 0) {
Victor Stinneref9d9b62019-05-22 11:28:22 +02001350 return NULL;
1351 }
1352 Py_RETURN_NONE;
1353}
1354
1355
1356/* Call sys.unraisablehook().
1357
1358 This function can be used when an exception has occurred but there is no way
1359 for Python to handle it. For example, when a destructor raises an exception
1360 or during garbage collection (gc.collect()).
1361
Victor Stinner71c52e32019-05-27 08:57:14 +02001362 If err_msg_str is non-NULL, the error message is formatted as:
1363 "Exception ignored %s" % err_msg_str. Otherwise, use "Exception ignored in"
1364 error message.
1365
Victor Stinneref9d9b62019-05-22 11:28:22 +02001366 An exception must be set when calling this function. */
1367void
Victor Stinner71c52e32019-05-27 08:57:14 +02001368_PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
Victor Stinneref9d9b62019-05-22 11:28:22 +02001369{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001370 PyThreadState *tstate = _PyThreadState_GET();
1371 assert(tstate != NULL);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001372
Victor Stinner71c52e32019-05-27 08:57:14 +02001373 PyObject *err_msg = NULL;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001374 PyObject *exc_type, *exc_value, *exc_tb;
1375 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
Victor Stinneref9d9b62019-05-22 11:28:22 +02001376
1377 assert(exc_type != NULL);
1378
1379 if (exc_type == NULL) {
1380 /* sys.unraisablehook requires that at least exc_type is set */
1381 goto default_hook;
1382 }
1383
Victor Stinnerdf22c032019-05-23 01:00:58 +02001384 if (exc_tb == NULL) {
Victor Stinner4386b902020-04-29 03:01:43 +02001385 PyFrameObject *frame = tstate->frame;
Victor Stinnerdf22c032019-05-23 01:00:58 +02001386 if (frame != NULL) {
1387 exc_tb = _PyTraceBack_FromFrame(NULL, frame);
1388 if (exc_tb == NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001389 _PyErr_Clear(tstate);
Victor Stinnerdf22c032019-05-23 01:00:58 +02001390 }
1391 }
1392 }
1393
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001394 _PyErr_NormalizeException(tstate, &exc_type, &exc_value, &exc_tb);
Victor Stinnerdf22c032019-05-23 01:00:58 +02001395
1396 if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) {
1397 if (PyException_SetTraceback(exc_value, exc_tb) < 0) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001398 _PyErr_Clear(tstate);
Victor Stinnerdf22c032019-05-23 01:00:58 +02001399 }
1400 }
1401
Victor Stinner71c52e32019-05-27 08:57:14 +02001402 if (err_msg_str != NULL) {
1403 err_msg = PyUnicode_FromFormat("Exception ignored %s", err_msg_str);
1404 if (err_msg == NULL) {
1405 PyErr_Clear();
1406 }
1407 }
1408
Steve Dowerbea33f52019-11-28 08:46:11 -08001409 PyObject *hook_args = make_unraisable_hook_args(
1410 tstate, exc_type, exc_value, exc_tb, err_msg, obj);
1411 if (hook_args == NULL) {
1412 err_msg_str = ("Exception ignored on building "
1413 "sys.unraisablehook arguments");
1414 goto error;
1415 }
1416
Victor Stinneref9d9b62019-05-22 11:28:22 +02001417 _Py_IDENTIFIER(unraisablehook);
1418 PyObject *hook = _PySys_GetObjectId(&PyId_unraisablehook);
Steve Dowerbea33f52019-11-28 08:46:11 -08001419 if (hook == NULL) {
1420 Py_DECREF(hook_args);
1421 goto default_hook;
Victor Stinneref9d9b62019-05-22 11:28:22 +02001422 }
1423
Victor Stinner1c1e68c2020-03-27 15:11:45 +01001424 if (_PySys_Audit(tstate, "sys.unraisablehook", "OO", hook, hook_args) < 0) {
Steve Dowerbea33f52019-11-28 08:46:11 -08001425 Py_DECREF(hook_args);
1426 err_msg_str = "Exception ignored in audit hook";
1427 obj = NULL;
1428 goto error;
1429 }
1430
1431 if (hook == Py_None) {
1432 Py_DECREF(hook_args);
1433 goto default_hook;
1434 }
1435
Petr Viktorinffd97532020-02-11 17:46:57 +01001436 PyObject *res = PyObject_CallOneArg(hook, hook_args);
Steve Dowerbea33f52019-11-28 08:46:11 -08001437 Py_DECREF(hook_args);
1438 if (res != NULL) {
1439 Py_DECREF(res);
1440 goto done;
1441 }
1442
1443 /* sys.unraisablehook failed: log its error using default hook */
1444 obj = hook;
1445 err_msg_str = NULL;
1446
1447error:
1448 /* err_msg_str and obj have been updated and we have a new exception */
1449 Py_XSETREF(err_msg, PyUnicode_FromString(err_msg_str ?
1450 err_msg_str : "Exception ignored in sys.unraisablehook"));
1451 Py_XDECREF(exc_type);
1452 Py_XDECREF(exc_value);
1453 Py_XDECREF(exc_tb);
1454 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
1455
Victor Stinneref9d9b62019-05-22 11:28:22 +02001456default_hook:
1457 /* Call the default unraisable hook (ignore failure) */
Victor Stinner71c52e32019-05-27 08:57:14 +02001458 (void)write_unraisable_exc(tstate, exc_type, exc_value, exc_tb,
1459 err_msg, obj);
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001460
1461done:
Victor Stinneref9d9b62019-05-22 11:28:22 +02001462 Py_XDECREF(exc_type);
1463 Py_XDECREF(exc_value);
1464 Py_XDECREF(exc_tb);
Victor Stinner71c52e32019-05-27 08:57:14 +02001465 Py_XDECREF(err_msg);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001466 _PyErr_Clear(tstate); /* Just in case */
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001467}
Guido van Rossumcfd42b52000-12-15 21:58:52 +00001468
Victor Stinner71c52e32019-05-27 08:57:14 +02001469
1470void
1471PyErr_WriteUnraisable(PyObject *obj)
1472{
1473 _PyErr_WriteUnraisableMsg(NULL, obj);
1474}
1475
1476
Armin Rigo092381a2003-10-25 14:29:27 +00001477extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +00001478
Guido van Rossum2fd45652001-02-28 21:46:24 +00001479
Benjamin Peterson2c539712010-09-20 22:42:10 +00001480void
Victor Stinner14e461d2013-08-26 22:28:21 +02001481PyErr_SyntaxLocation(const char *filename, int lineno)
1482{
Benjamin Peterson2c539712010-09-20 22:42:10 +00001483 PyErr_SyntaxLocationEx(filename, lineno, -1);
1484}
1485
1486
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001487/* Set file and line information for the current exception.
1488 If the exception is not a SyntaxError, also sets additional attributes
1489 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +00001490
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001491void
Victor Stinner14e461d2013-08-26 22:28:21 +02001492PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001495 _Py_IDENTIFIER(filename);
1496 _Py_IDENTIFIER(lineno);
1497 _Py_IDENTIFIER(msg);
1498 _Py_IDENTIFIER(offset);
1499 _Py_IDENTIFIER(print_file_and_line);
1500 _Py_IDENTIFIER(text);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001501 PyThreadState *tstate = _PyThreadState_GET();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 /* add attributes for the line number and filename for the error */
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001504 _PyErr_Fetch(tstate, &exc, &v, &tb);
1505 _PyErr_NormalizeException(tstate, &exc, &v, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 /* XXX check that it is, indeed, a syntax error. It might not
1507 * be, though. */
1508 tmp = PyLong_FromLong(lineno);
1509 if (tmp == NULL)
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001510 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 else {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001512 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) {
1513 _PyErr_Clear(tstate);
1514 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 Py_DECREF(tmp);
1516 }
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001517 tmp = NULL;
Benjamin Peterson2c539712010-09-20 22:42:10 +00001518 if (col_offset >= 0) {
1519 tmp = PyLong_FromLong(col_offset);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001520 if (tmp == NULL) {
1521 _PyErr_Clear(tstate);
1522 }
Benjamin Peterson2c539712010-09-20 22:42:10 +00001523 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001524 if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None)) {
1525 _PyErr_Clear(tstate);
1526 }
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001527 Py_XDECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 if (filename != NULL) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001529 if (_PyObject_SetAttrId(v, &PyId_filename, filename)) {
1530 _PyErr_Clear(tstate);
1531 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001532
Victor Stinner14e461d2013-08-26 22:28:21 +02001533 tmp = PyErr_ProgramTextObject(filename, lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (tmp) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001535 if (_PyObject_SetAttrId(v, &PyId_text, tmp)) {
1536 _PyErr_Clear(tstate);
1537 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 Py_DECREF(tmp);
1539 }
1540 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001542 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 tmp = PyObject_Str(v);
1544 if (tmp) {
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001545 if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) {
1546 _PyErr_Clear(tstate);
1547 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 Py_DECREF(tmp);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001549 }
1550 else {
1551 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 }
1553 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001554 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
1555 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001556 Py_None)) {
1557 _PyErr_Clear(tstate);
1558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 }
1560 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001561 _PyErr_Restore(tstate, exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001562}
1563
Victor Stinner14e461d2013-08-26 22:28:21 +02001564void
1565PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
1566{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001567 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner14e461d2013-08-26 22:28:21 +02001568 PyObject *fileobj;
1569 if (filename != NULL) {
1570 fileobj = PyUnicode_DecodeFSDefault(filename);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001571 if (fileobj == NULL) {
1572 _PyErr_Clear(tstate);
1573 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001574 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001575 else {
Victor Stinner14e461d2013-08-26 22:28:21 +02001576 fileobj = NULL;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001577 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001578 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
1579 Py_XDECREF(fileobj);
1580}
1581
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001582/* Attempt to load the line of text that the exception refers to. If it
1583 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001584
1585 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001586 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001587
Antoine Pitrou409b5382013-10-12 22:41:17 +02001588static PyObject *
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001589err_programtext(PyThreadState *tstate, FILE *fp, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 int i;
1592 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (fp == NULL)
1595 return NULL;
1596 for (i = 0; i < lineno; i++) {
1597 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1598 do {
1599 *pLastChar = '\0';
1600 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1601 fp, NULL) == NULL)
1602 break;
1603 /* fgets read *something*; if it didn't get as
1604 far as pLastChar, it must have found a newline
1605 or hit the end of the file; if pLastChar is \n,
1606 it obviously found a newline; else we haven't
1607 yet seen a newline, so must continue */
1608 } while (*pLastChar != '\0' && *pLastChar != '\n');
1609 }
1610 fclose(fp);
1611 if (i == lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 PyObject *res;
Martin Panterca3263c2016-12-11 00:18:36 +00001613 res = PyUnicode_FromString(linebuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 if (res == NULL)
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001615 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 return res;
1617 }
1618 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001619}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001620
Victor Stinner14e461d2013-08-26 22:28:21 +02001621PyObject *
1622PyErr_ProgramText(const char *filename, int lineno)
1623{
1624 FILE *fp;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001625 if (filename == NULL || *filename == '\0' || lineno <= 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02001626 return NULL;
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001627 }
1628 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerdaf45552013-08-28 00:53:59 +02001629 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001630 return err_programtext(tstate, fp, lineno);
Victor Stinner14e461d2013-08-26 22:28:21 +02001631}
1632
1633PyObject *
1634PyErr_ProgramTextObject(PyObject *filename, int lineno)
1635{
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001636 if (filename == NULL || lineno <= 0) {
Victor Stinnere42ccd22015-03-18 01:39:23 +01001637 return NULL;
1638 }
Victor Stinnerb4bdecd2019-05-24 13:44:24 +02001639
1640 PyThreadState *tstate = _PyThreadState_GET();
1641 FILE *fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
1642 if (fp == NULL) {
1643 _PyErr_Clear(tstate);
1644 return NULL;
1645 }
1646 return err_programtext(tstate, fp, lineno);
Victor Stinner14e461d2013-08-26 22:28:21 +02001647}
1648
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001649#ifdef __cplusplus
1650}
1651#endif