blob: 261dd7b27cbd1019936f90fddc2ba96ca3160e6a [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"
Guido van Rossumf22120a1990-12-20 23:05:40 +00005
Guido van Rossum53e8d441995-03-09 12:11:31 +00006#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +00007#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +00008extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +00009#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000010#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000011
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000012#ifdef MS_WINDOWS
Martin v. Löwis5d12abe2007-09-03 07:40:24 +000013#include <windows.h>
14#include <winbase.h>
Guido van Rossum743007d1999-04-21 15:27:31 +000015#endif
16
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000017#include <ctype.h>
18
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
Victor Stinnerbd303c12013-11-07 23:07:29 +010023_Py_IDENTIFIER(builtins);
24_Py_IDENTIFIER(stderr);
25
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000028PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyThreadState *tstate = PyThreadState_GET();
31 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
34 /* XXX Should never happen -- fatal error instead? */
35 /* Well, it could be None. */
36 Py_DECREF(traceback);
37 traceback = NULL;
38 }
Guido van Rossuma027efa1997-05-05 20:56:21 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 /* Save these in locals to safeguard against recursive
41 invocation through Py_XDECREF */
42 oldtype = tstate->curexc_type;
43 oldvalue = tstate->curexc_value;
44 oldtraceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 tstate->curexc_type = type;
47 tstate->curexc_value = value;
48 tstate->curexc_traceback = traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 Py_XDECREF(oldtype);
51 Py_XDECREF(oldvalue);
52 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053}
54
Victor Stinner3a840972016-08-22 23:59:08 +020055static PyObject*
56_PyErr_CreateException(PyObject *exception, PyObject *value)
57{
58 if (value == NULL || value == Py_None) {
59 return _PyObject_CallNoArg(exception);
60 }
61 else if (PyTuple_Check(value)) {
62 return PyObject_Call(exception, value, NULL);
63 }
64 else {
Victor Stinner7bfb42d2016-12-05 17:04:32 +010065 return PyObject_CallFunctionObjArgs(exception, value, NULL);
Victor Stinner3a840972016-08-22 23:59:08 +020066 }
67}
68
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000070PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 PyThreadState *tstate = PyThreadState_GET();
73 PyObject *exc_value;
74 PyObject *tb = NULL;
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (exception != NULL &&
77 !PyExceptionClass_Check(exception)) {
78 PyErr_Format(PyExc_SystemError,
79 "exception %R not a BaseException subclass",
80 exception);
81 return;
82 }
Victor Stinner3a840972016-08-22 23:59:08 +020083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 Py_XINCREF(value);
85 exc_value = tstate->exc_value;
86 if (exc_value != NULL && exc_value != Py_None) {
87 /* Implicit exception chaining */
88 Py_INCREF(exc_value);
89 if (value == NULL || !PyExceptionInstance_Check(value)) {
90 /* We must normalize the value right now */
Victor Stinner3a840972016-08-22 23:59:08 +020091 PyObject *fixed_value;
Victor Stinnerde821be2015-03-24 12:41:23 +010092
Victor Stinner3a840972016-08-22 23:59:08 +020093 /* Issue #23571: functions must not be called with an
Victor Stinnerde821be2015-03-24 12:41:23 +010094 exception set */
Victor Stinnerace47d72013-07-18 01:41:08 +020095 PyErr_Clear();
Victor Stinnerde821be2015-03-24 12:41:23 +010096
Victor Stinner3a840972016-08-22 23:59:08 +020097 fixed_value = _PyErr_CreateException(exception, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 Py_XDECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +020099 if (fixed_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return;
Victor Stinner3a840972016-08-22 23:59:08 +0200101 }
102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 value = fixed_value;
104 }
Victor Stinner3a840972016-08-22 23:59:08 +0200105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 /* Avoid reference cycles through the context chain.
107 This is O(chain length) but context chains are
108 usually very short. Sensitive readers may try
109 to inline the call to PyException_GetContext. */
110 if (exc_value != value) {
111 PyObject *o = exc_value, *context;
112 while ((context = PyException_GetContext(o))) {
113 Py_DECREF(context);
114 if (context == value) {
115 PyException_SetContext(o, NULL);
116 break;
117 }
118 o = context;
119 }
120 PyException_SetContext(value, exc_value);
Victor Stinner3a840972016-08-22 23:59:08 +0200121 }
122 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 Py_DECREF(exc_value);
124 }
125 }
126 if (value != NULL && PyExceptionInstance_Check(value))
127 tb = PyException_GetTraceback(value);
128 Py_XINCREF(exception);
129 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
Raymond Hettinger69492da2013-09-02 15:59:26 -0700132/* Set a key error with the specified argument, wrapping it in a
133 * tuple automatically so that tuple keys are not unpacked as the
134 * exception arguments. */
135void
136_PyErr_SetKeyError(PyObject *arg)
137{
138 PyObject *tup;
139 tup = PyTuple_Pack(1, arg);
140 if (!tup)
141 return; /* caller will expect error to be set anyway */
142 PyErr_SetObject(PyExc_KeyError, tup);
143 Py_DECREF(tup);
144}
145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000147PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150}
151
152void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 PyObject *value = PyUnicode_FromString(string);
156 PyErr_SetObject(exception, value);
157 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}
159
Guido van Rossum3a241811994-08-29 12:14:12 +0000160
Victor Stinnerc6944e72016-11-11 02:13:35 +0100161PyObject* _Py_HOT_FUNCTION
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000162PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163{
Victor Stinner0cae6092016-11-11 01:43:56 +0100164 PyThreadState *tstate = PyThreadState_GET();
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000165 return tstate == NULL ? NULL : tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
167
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000168
169int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (err == NULL || exc == NULL) {
173 /* maybe caused by "import exceptions" that failed early on */
174 return 0;
175 }
176 if (PyTuple_Check(exc)) {
177 Py_ssize_t i, n;
178 n = PyTuple_Size(exc);
179 for (i = 0; i < n; i++) {
180 /* Test recursively */
181 if (PyErr_GivenExceptionMatches(
182 err, PyTuple_GET_ITEM(exc, i)))
183 {
184 return 1;
185 }
186 }
187 return 0;
188 }
189 /* err might be an instance, so check its class. */
190 if (PyExceptionInstance_Check(err))
191 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
scodere4c06bc2017-07-31 22:27:46 +0200194 return PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000198}
Guido van Rossum743007d1999-04-21 15:27:31 +0000199
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000200
201int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000205}
206
207
208/* Used in many places to normalize a raised exception, including in
209 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000210
211 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000213*/
214void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000215PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PyObject *type = *exc;
218 PyObject *value = *val;
219 PyObject *inclass = NULL;
220 PyObject *initial_tb = NULL;
221 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (type == NULL) {
224 /* There was no exception, so nothing to do. */
225 return;
226 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 /* If PyErr_SetNone() was used, the value will have been actually
229 set to NULL.
230 */
231 if (!value) {
232 value = Py_None;
233 Py_INCREF(value);
234 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 if (PyExceptionInstance_Check(value))
237 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 /* Normalize the exception so that if the type is a class, the
240 value will be an instance.
241 */
242 if (PyExceptionClass_Check(type)) {
Victor Stinner74a7fa62013-07-17 00:44:53 +0200243 int is_subclass;
244 if (inclass) {
245 is_subclass = PyObject_IsSubclass(inclass, type);
246 if (is_subclass < 0)
247 goto finally;
248 }
249 else
250 is_subclass = 0;
251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 /* if the value was not an instance, or is not an instance
253 whose class is (or is derived from) type, then use the
254 value as an argument to instantiation of the type
255 class.
256 */
Victor Stinner74a7fa62013-07-17 00:44:53 +0200257 if (!inclass || !is_subclass) {
Victor Stinner3a840972016-08-22 23:59:08 +0200258 PyObject *fixed_value;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000259
Victor Stinner3a840972016-08-22 23:59:08 +0200260 fixed_value = _PyErr_CreateException(type, value);
261 if (fixed_value == NULL) {
262 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_DECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +0200266 value = fixed_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 }
268 /* if the class of the instance doesn't exactly match the
269 class of the type, believe the instance
270 */
271 else if (inclass != type) {
272 Py_DECREF(type);
273 type = inclass;
274 Py_INCREF(type);
275 }
276 }
277 *exc = type;
278 *val = value;
279 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000280finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 Py_DECREF(type);
282 Py_DECREF(value);
283 /* If the new exception doesn't set a traceback and the old
284 exception had a traceback, use the old traceback for the
285 new exception. It's better than nothing.
286 */
287 initial_tb = *tb;
288 PyErr_Fetch(exc, val, tb);
289 if (initial_tb != NULL) {
290 if (*tb == NULL)
291 *tb = initial_tb;
292 else
293 Py_DECREF(initial_tb);
294 }
295 /* normalize recursively */
296 tstate = PyThreadState_GET();
297 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
298 --tstate->recursion_depth;
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200299 /* throw away the old exception and use the recursion error instead */
300 Py_INCREF(PyExc_RecursionError);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300301 Py_SETREF(*exc, PyExc_RecursionError);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200302 Py_INCREF(PyExc_RecursionErrorInst);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300303 Py_SETREF(*val, PyExc_RecursionErrorInst);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 /* just keeping the old traceback */
305 return;
306 }
307 PyErr_NormalizeException(exc, val, tb);
308 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000309}
310
311
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000313PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 *p_type = tstate->curexc_type;
318 *p_value = tstate->curexc_value;
319 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 tstate->curexc_type = NULL;
322 tstate->curexc_value = NULL;
323 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
326void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000331
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200332void
333PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
334{
335 PyThreadState *tstate = PyThreadState_GET();
336
337 *p_type = tstate->exc_type;
338 *p_value = tstate->exc_value;
339 *p_traceback = tstate->exc_traceback;
340
341 Py_XINCREF(*p_type);
342 Py_XINCREF(*p_value);
343 Py_XINCREF(*p_traceback);
344}
345
346void
347PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
348{
349 PyObject *oldtype, *oldvalue, *oldtraceback;
350 PyThreadState *tstate = PyThreadState_GET();
351
352 oldtype = tstate->exc_type;
353 oldvalue = tstate->exc_value;
354 oldtraceback = tstate->exc_traceback;
355
356 tstate->exc_type = p_type;
357 tstate->exc_value = p_value;
358 tstate->exc_traceback = p_traceback;
359
360 Py_XDECREF(oldtype);
361 Py_XDECREF(oldvalue);
362 Py_XDECREF(oldtraceback);
363}
364
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300365/* Like PyErr_Restore(), but if an exception is already set,
366 set the context associated with it.
367 */
368void
369_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
370{
371 if (exc == NULL)
372 return;
373
374 if (PyErr_Occurred()) {
375 PyObject *exc2, *val2, *tb2;
376 PyErr_Fetch(&exc2, &val2, &tb2);
377 PyErr_NormalizeException(&exc, &val, &tb);
Serhiy Storchaka9e373be2016-10-21 16:19:59 +0300378 if (tb != NULL) {
379 PyException_SetTraceback(val, tb);
380 Py_DECREF(tb);
381 }
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300382 Py_DECREF(exc);
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300383 PyErr_NormalizeException(&exc2, &val2, &tb2);
384 PyException_SetContext(val2, val);
385 PyErr_Restore(exc2, val2, tb2);
386 }
387 else {
388 PyErr_Restore(exc, val, tb);
389 }
390}
391
Serhiy Storchaka467ab192016-10-21 17:09:17 +0300392static PyObject *
393_PyErr_FormatVFromCause(PyObject *exception, const char *format, va_list vargs)
394{
395 PyObject *exc, *val, *val2, *tb;
396
397 assert(PyErr_Occurred());
398 PyErr_Fetch(&exc, &val, &tb);
399 PyErr_NormalizeException(&exc, &val, &tb);
400 if (tb != NULL) {
401 PyException_SetTraceback(val, tb);
402 Py_DECREF(tb);
403 }
404 Py_DECREF(exc);
405 assert(!PyErr_Occurred());
406
407 PyErr_FormatV(exception, format, vargs);
408
409 PyErr_Fetch(&exc, &val2, &tb);
410 PyErr_NormalizeException(&exc, &val2, &tb);
411 Py_INCREF(val);
412 PyException_SetCause(val2, val);
413 PyException_SetContext(val2, val);
414 PyErr_Restore(exc, val2, tb);
415
416 return NULL;
417}
418
419PyObject *
420_PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
421{
422 va_list vargs;
423#ifdef HAVE_STDARG_PROTOTYPES
424 va_start(vargs, format);
425#else
426 va_start(vargs);
427#endif
428 _PyErr_FormatVFromCause(exception, format, vargs);
429 va_end(vargs);
430 return NULL;
431}
432
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000433/* Convenience functions to set a type error exception and return 0 */
434
435int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 PyErr_SetString(PyExc_TypeError,
439 "bad argument type for built-in operation");
440 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000441}
442
Guido van Rossum373c8691997-04-29 18:22:47 +0000443PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000444PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000445{
Victor Stinnerf54a5742013-07-22 22:28:37 +0200446 if (Py_TYPE(PyExc_MemoryError) == NULL) {
447 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
448 initialized by _PyExc_Init() */
449 Py_FatalError("Out of memory and PyExc_MemoryError is not "
450 "initialized yet");
451 }
Antoine Pitrou07e20ef2010-10-28 22:56:58 +0000452 PyErr_SetNone(PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000454}
455
Guido van Rossum373c8691997-04-29 18:22:47 +0000456PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000457PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000458{
Larry Hastingsb0827312014-02-09 22:05:19 -0800459 return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
460}
461
462PyObject *
463PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200466 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100468#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000470#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000471
Guido van Rossume9fbc091995-02-18 14:52:19 +0000472#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (i == EINTR && PyErr_CheckSignals())
474 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000475#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000476
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000477#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100478 if (i != 0) {
479 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100480 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100481 }
482 else {
483 /* Sometimes errno didn't get set */
484 message = PyUnicode_FromString("Error");
485 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000486#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (i == 0)
488 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
489 else
490 {
491 /* Note that the Win32 errors do not lineup with the
492 errno error. So if the error is in the MSVC error
493 table, we use it, otherwise we assume it really _is_
494 a Win32 error code
495 */
496 if (i > 0 && i < _sys_nerr) {
497 message = PyUnicode_FromString(_sys_errlist[i]);
498 }
499 else {
500 int len = FormatMessageW(
501 FORMAT_MESSAGE_ALLOCATE_BUFFER |
502 FORMAT_MESSAGE_FROM_SYSTEM |
503 FORMAT_MESSAGE_IGNORE_INSERTS,
504 NULL, /* no message source */
505 i,
506 MAKELANGID(LANG_NEUTRAL,
507 SUBLANG_DEFAULT),
508 /* Default language */
509 (LPWSTR) &s_buf,
510 0, /* size not used */
511 NULL); /* no args */
512 if (len==0) {
513 /* Only ever seen this in out-of-mem
514 situations */
515 s_buf = NULL;
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300516 message = PyUnicode_FromFormat("Windows Error 0x%x", i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 } else {
518 /* remove trailing cr/lf and dots */
519 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
520 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200521 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 }
523 }
524 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000525#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (message == NULL)
528 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000529#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000531#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return NULL;
533 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000534
Larry Hastingsb0827312014-02-09 22:05:19 -0800535 if (filenameObject != NULL) {
536 if (filenameObject2 != NULL)
537 args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
538 else
539 args = Py_BuildValue("(iOO)", i, message, filenameObject);
540 } else {
541 assert(filenameObject2 == NULL);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200542 args = Py_BuildValue("(iO)", i, message);
Larry Hastingsb0827312014-02-09 22:05:19 -0800543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000545
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200546 if (args != NULL) {
547 v = PyObject_Call(exc, args, NULL);
548 Py_DECREF(args);
549 if (v != NULL) {
550 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
551 Py_DECREF(v);
552 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000554#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000556#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000558}
Guido van Rossum743007d1999-04-21 15:27:31 +0000559
Barry Warsaw97d95151998-07-23 16:05:56 +0000560PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000561PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800564 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_XDECREF(name);
566 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000567}
568
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000569#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000570PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000571PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000572{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200573 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800574 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 Py_XDECREF(name);
576 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000577}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000578#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000579
580PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000581PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000582{
Larry Hastingsb0827312014-02-09 22:05:19 -0800583 return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000584}
Guido van Rossum683a0721990-10-21 22:09:12 +0000585
Brett Cannonbf364092006-03-01 04:25:17 +0000586#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000587/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000588PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 PyObject *exc,
590 int ierr,
591 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000592{
Larry Hastingsb0827312014-02-09 22:05:19 -0800593 return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
594 filenameObject, NULL);
595}
596
597PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
598 PyObject *exc,
599 int ierr,
600 PyObject *filenameObject,
601 PyObject *filenameObject2)
602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 int len;
604 WCHAR *s_buf = NULL; /* Free via LocalFree */
605 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200606 PyObject *args, *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 DWORD err = (DWORD)ierr;
608 if (err==0) err = GetLastError();
609 len = FormatMessageW(
610 /* Error API error */
611 FORMAT_MESSAGE_ALLOCATE_BUFFER |
612 FORMAT_MESSAGE_FROM_SYSTEM |
613 FORMAT_MESSAGE_IGNORE_INSERTS,
614 NULL, /* no message source */
615 err,
616 MAKELANGID(LANG_NEUTRAL,
617 SUBLANG_DEFAULT), /* Default language */
618 (LPWSTR) &s_buf,
619 0, /* size not used */
620 NULL); /* no args */
621 if (len==0) {
622 /* Only seen this in out of mem situations */
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300623 message = PyUnicode_FromFormat("Windows Error 0x%x", err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 s_buf = NULL;
625 } else {
626 /* remove trailing cr/lf and dots */
627 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
628 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200629 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (message == NULL)
633 {
634 LocalFree(s_buf);
635 return NULL;
636 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000637
Larry Hastingsb0827312014-02-09 22:05:19 -0800638 if (filenameObject == NULL) {
639 assert(filenameObject2 == NULL);
640 filenameObject = filenameObject2 = Py_None;
641 }
642 else if (filenameObject2 == NULL)
643 filenameObject2 = Py_None;
644 /* This is the constructor signature for OSError.
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200645 The POSIX translation will be figured out by the constructor. */
Larry Hastingsb0827312014-02-09 22:05:19 -0800646 args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000648
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200649 if (args != NULL) {
650 v = PyObject_Call(exc, args, NULL);
651 Py_DECREF(args);
652 if (v != NULL) {
653 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
654 Py_DECREF(v);
655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 }
657 LocalFree(s_buf);
658 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000659}
660
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000661PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyObject *exc,
663 int ierr,
664 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000665{
Victor Stinner92be9392010-12-28 00:28:21 +0000666 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800667 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800669 name,
670 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 Py_XDECREF(name);
672 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000673}
674
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000675PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 PyObject *exc,
677 int ierr,
678 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 *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800683 name,
684 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 Py_XDECREF(name);
686 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000688
Thomas Heller085358a2002-07-29 14:27:41 +0000689PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
690{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800691 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000692}
693
Guido van Rossum795e1892000-02-17 15:19:15 +0000694PyObject *PyErr_SetFromWindowsErr(int ierr)
695{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800696 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
697 ierr, NULL);
Larry Hastingsb0827312014-02-09 22:05:19 -0800698}
699
Thomas Heller085358a2002-07-29 14:27:41 +0000700PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 int ierr,
702 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000703{
Victor Stinner92be9392010-12-28 00:28:21 +0000704 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800705 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200706 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800707 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 Py_XDECREF(name);
709 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000710}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000711
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000712PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 int ierr,
714 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000715{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200716 PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800717 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200718 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800719 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 Py_XDECREF(name);
721 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000722}
Guido van Rossum795e1892000-02-17 15:19:15 +0000723#endif /* MS_WINDOWS */
724
Brett Cannon79ec55e2012-04-12 20:24:54 -0400725PyObject *
Eric Snow46f97b82016-09-07 16:56:15 -0700726PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
727 PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400728{
Eric Snow46f97b82016-09-07 16:56:15 -0700729 int issubclass;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200730 PyObject *kwargs, *error;
Brian Curtin09b86d12012-04-17 16:57:09 -0500731
Eric Snow46f97b82016-09-07 16:56:15 -0700732 issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
733 if (issubclass < 0) {
734 return NULL;
735 }
736 else if (!issubclass) {
737 PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError");
Brian Curtin94c001b2012-04-18 08:30:51 -0500738 return NULL;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200739 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500740
Eric Snow46f97b82016-09-07 16:56:15 -0700741 if (msg == NULL) {
742 PyErr_SetString(PyExc_TypeError, "expected a message argument");
Brian Curtin09b86d12012-04-17 16:57:09 -0500743 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400744 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500745
Brian Curtin94c001b2012-04-18 08:30:51 -0500746 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500747 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500748 }
Brian Curtin94c001b2012-04-18 08:30:51 -0500749 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500750 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500751 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500752
Eric Snow46f97b82016-09-07 16:56:15 -0700753 kwargs = PyDict_New();
754 if (kwargs == NULL) {
755 return NULL;
756 }
Victor Stinnerf45a5612016-08-23 00:04:41 +0200757 if (PyDict_SetItemString(kwargs, "name", name) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300758 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200759 }
760 if (PyDict_SetItemString(kwargs, "path", path) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300761 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200762 }
Brett Cannon79ec55e2012-04-12 20:24:54 -0400763
Eric Snow46f97b82016-09-07 16:56:15 -0700764 error = _PyObject_FastCallDict(exception, &msg, 1, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400765 if (error != NULL) {
766 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500767 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400768 }
769
Berker Peksagec766d32016-05-01 09:06:36 +0300770done:
Brett Cannon79ec55e2012-04-12 20:24:54 -0400771 Py_DECREF(kwargs);
Brian Curtin09b86d12012-04-17 16:57:09 -0500772 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400773}
774
Eric Snow46f97b82016-09-07 16:56:15 -0700775PyObject *
776PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
777{
778 return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
779}
780
Guido van Rossum683a0721990-10-21 22:09:12 +0000781void
Neal Norwitzb382b842007-08-24 20:00:37 +0000782_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 PyErr_Format(PyExc_SystemError,
785 "%s:%d: bad argument to internal function",
786 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000787}
788
789/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
790 export the entry point for existing object code: */
791#undef PyErr_BadInternalCall
792void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000793PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000794{
Victor Stinnerfb3a6302013-07-12 00:37:30 +0200795 assert(0 && "bad argument to internal function");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyErr_Format(PyExc_SystemError,
797 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000798}
Fred Drake6d63adf2000-08-24 22:38:39 +0000799#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
800
Guido van Rossum1548bac1997-02-14 17:09:47 +0000801
Guido van Rossum1548bac1997-02-14 17:09:47 +0000802PyObject *
Antoine Pitrou0676a402014-09-30 21:16:27 +0200803PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000806
Victor Stinnerde821be2015-03-24 12:41:23 +0100807 /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
808 exception set, it calls arbitrary Python code like PyObject_Repr() */
Victor Stinnerace47d72013-07-18 01:41:08 +0200809 PyErr_Clear();
Victor Stinnerace47d72013-07-18 01:41:08 +0200810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 string = PyUnicode_FromFormatV(format, vargs);
Victor Stinnerde821be2015-03-24 12:41:23 +0100812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyErr_SetObject(exception, string);
814 Py_XDECREF(string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000816}
Guido van Rossum7617e051997-09-16 18:43:50 +0000817
818
Antoine Pitrou0676a402014-09-30 21:16:27 +0200819PyObject *
820PyErr_Format(PyObject *exception, const char *format, ...)
821{
822 va_list vargs;
823#ifdef HAVE_STDARG_PROTOTYPES
824 va_start(vargs, format);
825#else
826 va_start(vargs);
827#endif
828 PyErr_FormatV(exception, format, vargs);
829 va_end(vargs);
830 return NULL;
831}
832
Thomas Wouters477c8d52006-05-27 19:21:47 +0000833
Guido van Rossum7617e051997-09-16 18:43:50 +0000834PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000835PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 const char *dot;
838 PyObject *modulename = NULL;
839 PyObject *classname = NULL;
840 PyObject *mydict = NULL;
841 PyObject *bases = NULL;
842 PyObject *result = NULL;
843 dot = strrchr(name, '.');
844 if (dot == NULL) {
845 PyErr_SetString(PyExc_SystemError,
846 "PyErr_NewException: name must be module.class");
847 return NULL;
848 }
849 if (base == NULL)
850 base = PyExc_Exception;
851 if (dict == NULL) {
852 dict = mydict = PyDict_New();
853 if (dict == NULL)
854 goto failure;
855 }
856 if (PyDict_GetItemString(dict, "__module__") == NULL) {
857 modulename = PyUnicode_FromStringAndSize(name,
858 (Py_ssize_t)(dot-name));
859 if (modulename == NULL)
860 goto failure;
861 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
862 goto failure;
863 }
864 if (PyTuple_Check(base)) {
865 bases = base;
866 /* INCREF as we create a new ref in the else branch */
867 Py_INCREF(bases);
868 } else {
869 bases = PyTuple_Pack(1, base);
870 if (bases == NULL)
871 goto failure;
872 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100873 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000874 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000876 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 Py_XDECREF(bases);
878 Py_XDECREF(mydict);
879 Py_XDECREF(classname);
880 Py_XDECREF(modulename);
881 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +0000882}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000883
Georg Brandl1e28a272009-12-28 08:41:01 +0000884
885/* Create an exception with docstring */
886PyObject *
887PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +0000889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 int result;
891 PyObject *ret = NULL;
892 PyObject *mydict = NULL; /* points to the dict only if we create it */
893 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 if (dict == NULL) {
896 dict = mydict = PyDict_New();
897 if (dict == NULL) {
898 return NULL;
899 }
900 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 if (doc != NULL) {
903 docobj = PyUnicode_FromString(doc);
904 if (docobj == NULL)
905 goto failure;
906 result = PyDict_SetItemString(dict, "__doc__", docobj);
907 Py_DECREF(docobj);
908 if (result < 0)
909 goto failure;
910 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +0000913 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 Py_XDECREF(mydict);
915 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +0000916}
917
918
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000919/* Call when an exception has occurred but there is no way for Python
920 to handle it. Examples: exception in __del__ or during GC. */
921void
922PyErr_WriteUnraisable(PyObject *obj)
923{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200924 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 PyObject *f, *t, *v, *tb;
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200926 PyObject *moduleName = NULL;
927 char* className;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000928
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200929 PyErr_Fetch(&t, &v, &tb);
930
Victor Stinnerbd303c12013-11-07 23:07:29 +0100931 f = _PySys_GetObjectId(&PyId_stderr);
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200932 if (f == NULL || f == Py_None)
933 goto done;
934
935 if (obj) {
936 if (PyFile_WriteString("Exception ignored in: ", f) < 0)
937 goto done;
Martin Panter3263f682016-02-28 03:16:11 +0000938 if (PyFile_WriteObject(obj, f, 0) < 0) {
939 PyErr_Clear();
940 if (PyFile_WriteString("<object repr() failed>", f) < 0) {
941 goto done;
942 }
943 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200944 if (PyFile_WriteString("\n", f) < 0)
945 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200947
948 if (PyTraceBack_Print(tb, f) < 0)
949 goto done;
950
951 if (!t)
952 goto done;
953
954 assert(PyExceptionClass_Check(t));
955 className = PyExceptionClass_Name(t);
956 if (className != NULL) {
957 char *dot = strrchr(className, '.');
958 if (dot != NULL)
959 className = dot+1;
960 }
961
962 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
963 if (moduleName == NULL) {
964 PyErr_Clear();
965 if (PyFile_WriteString("<unknown>", f) < 0)
966 goto done;
967 }
968 else {
Serhiy Storchakaf5894dd2016-11-16 15:40:39 +0200969 if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) {
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200970 if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
971 goto done;
972 if (PyFile_WriteString(".", f) < 0)
973 goto done;
974 }
975 }
976 if (className == NULL) {
977 if (PyFile_WriteString("<unknown>", f) < 0)
978 goto done;
979 }
980 else {
981 if (PyFile_WriteString(className, f) < 0)
982 goto done;
983 }
984
985 if (v && v != Py_None) {
986 if (PyFile_WriteString(": ", f) < 0)
987 goto done;
Martin Panter3263f682016-02-28 03:16:11 +0000988 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) {
989 PyErr_Clear();
990 if (PyFile_WriteString("<exception str() failed>", f) < 0) {
991 goto done;
992 }
993 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200994 }
995 if (PyFile_WriteString("\n", f) < 0)
996 goto done;
997
998done:
999 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 Py_XDECREF(t);
1001 Py_XDECREF(v);
1002 Py_XDECREF(tb);
Victor Stinnerc82bfd82013-08-26 14:04:10 +02001003 PyErr_Clear(); /* Just in case */
Jeremy Hyltonb709df32000-09-01 02:47:25 +00001004}
Guido van Rossumcfd42b52000-12-15 21:58:52 +00001005
Armin Rigo092381a2003-10-25 14:29:27 +00001006extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +00001007
Guido van Rossum2fd45652001-02-28 21:46:24 +00001008
Benjamin Peterson2c539712010-09-20 22:42:10 +00001009void
Victor Stinner14e461d2013-08-26 22:28:21 +02001010PyErr_SyntaxLocation(const char *filename, int lineno)
1011{
Benjamin Peterson2c539712010-09-20 22:42:10 +00001012 PyErr_SyntaxLocationEx(filename, lineno, -1);
1013}
1014
1015
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001016/* Set file and line information for the current exception.
1017 If the exception is not a SyntaxError, also sets additional attributes
1018 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +00001019
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001020void
Victor Stinner14e461d2013-08-26 22:28:21 +02001021PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001024 _Py_IDENTIFIER(filename);
1025 _Py_IDENTIFIER(lineno);
1026 _Py_IDENTIFIER(msg);
1027 _Py_IDENTIFIER(offset);
1028 _Py_IDENTIFIER(print_file_and_line);
1029 _Py_IDENTIFIER(text);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 /* add attributes for the line number and filename for the error */
1032 PyErr_Fetch(&exc, &v, &tb);
1033 PyErr_NormalizeException(&exc, &v, &tb);
1034 /* XXX check that it is, indeed, a syntax error. It might not
1035 * be, though. */
1036 tmp = PyLong_FromLong(lineno);
1037 if (tmp == NULL)
1038 PyErr_Clear();
1039 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001040 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PyErr_Clear();
1042 Py_DECREF(tmp);
1043 }
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001044 tmp = NULL;
Benjamin Peterson2c539712010-09-20 22:42:10 +00001045 if (col_offset >= 0) {
1046 tmp = PyLong_FromLong(col_offset);
1047 if (tmp == NULL)
1048 PyErr_Clear();
Benjamin Peterson2c539712010-09-20 22:42:10 +00001049 }
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001050 if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None))
1051 PyErr_Clear();
1052 Py_XDECREF(tmp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (filename != NULL) {
Victor Stinner14e461d2013-08-26 22:28:21 +02001054 if (_PyObject_SetAttrId(v, &PyId_filename, filename))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001056
Victor Stinner14e461d2013-08-26 22:28:21 +02001057 tmp = PyErr_ProgramTextObject(filename, lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001059 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyErr_Clear();
1061 Py_DECREF(tmp);
1062 }
1063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001065 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 tmp = PyObject_Str(v);
1067 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001068 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyErr_Clear();
1070 Py_DECREF(tmp);
1071 } else {
1072 PyErr_Clear();
1073 }
1074 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001075 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
1076 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
1077 Py_None))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 PyErr_Clear();
1079 }
1080 }
1081 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001082}
1083
Victor Stinner14e461d2013-08-26 22:28:21 +02001084void
1085PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
1086{
1087 PyObject *fileobj;
1088 if (filename != NULL) {
1089 fileobj = PyUnicode_DecodeFSDefault(filename);
1090 if (fileobj == NULL)
1091 PyErr_Clear();
1092 }
1093 else
1094 fileobj = NULL;
1095 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
1096 Py_XDECREF(fileobj);
1097}
1098
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001099/* Attempt to load the line of text that the exception refers to. If it
1100 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001101
1102 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001103 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001104
Antoine Pitrou409b5382013-10-12 22:41:17 +02001105static PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001106err_programtext(FILE *fp, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 int i;
1109 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (fp == NULL)
1112 return NULL;
1113 for (i = 0; i < lineno; i++) {
1114 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1115 do {
1116 *pLastChar = '\0';
1117 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1118 fp, NULL) == NULL)
1119 break;
1120 /* fgets read *something*; if it didn't get as
1121 far as pLastChar, it must have found a newline
1122 or hit the end of the file; if pLastChar is \n,
1123 it obviously found a newline; else we haven't
1124 yet seen a newline, so must continue */
1125 } while (*pLastChar != '\0' && *pLastChar != '\n');
1126 }
1127 fclose(fp);
1128 if (i == lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 PyObject *res;
Martin Panterca3263c2016-12-11 00:18:36 +00001130 res = PyUnicode_FromString(linebuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (res == NULL)
1132 PyErr_Clear();
1133 return res;
1134 }
1135 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001136}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137
Victor Stinner14e461d2013-08-26 22:28:21 +02001138PyObject *
1139PyErr_ProgramText(const char *filename, int lineno)
1140{
1141 FILE *fp;
1142 if (filename == NULL || *filename == '\0' || lineno <= 0)
1143 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001144 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
Victor Stinner14e461d2013-08-26 22:28:21 +02001145 return err_programtext(fp, lineno);
1146}
1147
1148PyObject *
1149PyErr_ProgramTextObject(PyObject *filename, int lineno)
1150{
1151 FILE *fp;
1152 if (filename == NULL || lineno <= 0)
1153 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001154 fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
Victor Stinnere42ccd22015-03-18 01:39:23 +01001155 if (fp == NULL) {
1156 PyErr_Clear();
1157 return NULL;
1158 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001159 return err_programtext(fp, lineno);
1160}
1161
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162#ifdef __cplusplus
1163}
1164#endif