blob: 13ae6b456150cdcd13078181f5f2d70218ddd5bf [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 {
65 return _PyObject_CallArg1(exception, value);
66 }
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
Guido van Rossum373c8691997-04-29 18:22:47 +0000161PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000162PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100164 PyThreadState *tstate = _PyThreadState_UncheckedGet();
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)) {
194 int res = 0;
195 PyObject *exception, *value, *tb;
196 PyErr_Fetch(&exception, &value, &tb);
197 /* PyObject_IsSubclass() can recurse and therefore is
198 not safe (see test_bad_getattr in test.pickletester). */
199 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
200 /* This function must not fail, so print the error here */
201 if (res == -1) {
202 PyErr_WriteUnraisable(err);
203 res = 0;
204 }
205 PyErr_Restore(exception, value, tb);
206 return res;
207 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000210}
Guido van Rossum743007d1999-04-21 15:27:31 +0000211
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000212
213int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000214PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000217}
218
219
220/* Used in many places to normalize a raised exception, including in
221 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000222
223 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000225*/
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 PyObject *type = *exc;
230 PyObject *value = *val;
231 PyObject *inclass = NULL;
232 PyObject *initial_tb = NULL;
233 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if (type == NULL) {
236 /* There was no exception, so nothing to do. */
237 return;
238 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 /* If PyErr_SetNone() was used, the value will have been actually
241 set to NULL.
242 */
243 if (!value) {
244 value = Py_None;
245 Py_INCREF(value);
246 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (PyExceptionInstance_Check(value))
249 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 /* Normalize the exception so that if the type is a class, the
252 value will be an instance.
253 */
254 if (PyExceptionClass_Check(type)) {
Victor Stinner74a7fa62013-07-17 00:44:53 +0200255 int is_subclass;
256 if (inclass) {
257 is_subclass = PyObject_IsSubclass(inclass, type);
258 if (is_subclass < 0)
259 goto finally;
260 }
261 else
262 is_subclass = 0;
263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* if the value was not an instance, or is not an instance
265 whose class is (or is derived from) type, then use the
266 value as an argument to instantiation of the type
267 class.
268 */
Victor Stinner74a7fa62013-07-17 00:44:53 +0200269 if (!inclass || !is_subclass) {
Victor Stinner3a840972016-08-22 23:59:08 +0200270 PyObject *fixed_value;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000271
Victor Stinner3a840972016-08-22 23:59:08 +0200272 fixed_value = _PyErr_CreateException(type, value);
273 if (fixed_value == NULL) {
274 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_DECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +0200278 value = fixed_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 }
280 /* if the class of the instance doesn't exactly match the
281 class of the type, believe the instance
282 */
283 else if (inclass != type) {
284 Py_DECREF(type);
285 type = inclass;
286 Py_INCREF(type);
287 }
288 }
289 *exc = type;
290 *val = value;
291 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000292finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 Py_DECREF(type);
294 Py_DECREF(value);
295 /* If the new exception doesn't set a traceback and the old
296 exception had a traceback, use the old traceback for the
297 new exception. It's better than nothing.
298 */
299 initial_tb = *tb;
300 PyErr_Fetch(exc, val, tb);
301 if (initial_tb != NULL) {
302 if (*tb == NULL)
303 *tb = initial_tb;
304 else
305 Py_DECREF(initial_tb);
306 }
307 /* normalize recursively */
308 tstate = PyThreadState_GET();
309 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
310 --tstate->recursion_depth;
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200311 /* throw away the old exception and use the recursion error instead */
312 Py_INCREF(PyExc_RecursionError);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300313 Py_SETREF(*exc, PyExc_RecursionError);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200314 Py_INCREF(PyExc_RecursionErrorInst);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300315 Py_SETREF(*val, PyExc_RecursionErrorInst);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 /* just keeping the old traceback */
317 return;
318 }
319 PyErr_NormalizeException(exc, val, tb);
320 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000321}
322
323
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 *p_type = tstate->curexc_type;
330 *p_value = tstate->curexc_value;
331 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 tstate->curexc_type = NULL;
334 tstate->curexc_value = NULL;
335 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336}
337
338void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000343
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200344void
345PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
346{
347 PyThreadState *tstate = PyThreadState_GET();
348
349 *p_type = tstate->exc_type;
350 *p_value = tstate->exc_value;
351 *p_traceback = tstate->exc_traceback;
352
353 Py_XINCREF(*p_type);
354 Py_XINCREF(*p_value);
355 Py_XINCREF(*p_traceback);
356}
357
358void
359PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
360{
361 PyObject *oldtype, *oldvalue, *oldtraceback;
362 PyThreadState *tstate = PyThreadState_GET();
363
364 oldtype = tstate->exc_type;
365 oldvalue = tstate->exc_value;
366 oldtraceback = tstate->exc_traceback;
367
368 tstate->exc_type = p_type;
369 tstate->exc_value = p_value;
370 tstate->exc_traceback = p_traceback;
371
372 Py_XDECREF(oldtype);
373 Py_XDECREF(oldvalue);
374 Py_XDECREF(oldtraceback);
375}
376
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300377/* Like PyErr_Restore(), but if an exception is already set,
378 set the context associated with it.
379 */
380void
381_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
382{
383 if (exc == NULL)
384 return;
385
386 if (PyErr_Occurred()) {
387 PyObject *exc2, *val2, *tb2;
388 PyErr_Fetch(&exc2, &val2, &tb2);
389 PyErr_NormalizeException(&exc, &val, &tb);
390 Py_DECREF(exc);
391 Py_XDECREF(tb);
392 PyErr_NormalizeException(&exc2, &val2, &tb2);
393 PyException_SetContext(val2, val);
394 PyErr_Restore(exc2, val2, tb2);
395 }
396 else {
397 PyErr_Restore(exc, val, tb);
398 }
399}
400
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000401/* Convenience functions to set a type error exception and return 0 */
402
403int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000404PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 PyErr_SetString(PyExc_TypeError,
407 "bad argument type for built-in operation");
408 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000409}
410
Guido van Rossum373c8691997-04-29 18:22:47 +0000411PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000412PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000413{
Victor Stinnerf54a5742013-07-22 22:28:37 +0200414 if (Py_TYPE(PyExc_MemoryError) == NULL) {
415 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
416 initialized by _PyExc_Init() */
417 Py_FatalError("Out of memory and PyExc_MemoryError is not "
418 "initialized yet");
419 }
Antoine Pitrou07e20ef2010-10-28 22:56:58 +0000420 PyErr_SetNone(PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000422}
423
Guido van Rossum373c8691997-04-29 18:22:47 +0000424PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000425PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000426{
Larry Hastingsb0827312014-02-09 22:05:19 -0800427 return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
428}
429
430PyObject *
431PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200434 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100436#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000438#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000439
Guido van Rossume9fbc091995-02-18 14:52:19 +0000440#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (i == EINTR && PyErr_CheckSignals())
442 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000443#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000444
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000445#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100446 if (i != 0) {
447 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100448 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100449 }
450 else {
451 /* Sometimes errno didn't get set */
452 message = PyUnicode_FromString("Error");
453 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000454#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (i == 0)
456 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
457 else
458 {
459 /* Note that the Win32 errors do not lineup with the
460 errno error. So if the error is in the MSVC error
461 table, we use it, otherwise we assume it really _is_
462 a Win32 error code
463 */
464 if (i > 0 && i < _sys_nerr) {
465 message = PyUnicode_FromString(_sys_errlist[i]);
466 }
467 else {
468 int len = FormatMessageW(
469 FORMAT_MESSAGE_ALLOCATE_BUFFER |
470 FORMAT_MESSAGE_FROM_SYSTEM |
471 FORMAT_MESSAGE_IGNORE_INSERTS,
472 NULL, /* no message source */
473 i,
474 MAKELANGID(LANG_NEUTRAL,
475 SUBLANG_DEFAULT),
476 /* Default language */
477 (LPWSTR) &s_buf,
478 0, /* size not used */
479 NULL); /* no args */
480 if (len==0) {
481 /* Only ever seen this in out-of-mem
482 situations */
483 s_buf = NULL;
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300484 message = PyUnicode_FromFormat("Windows Error 0x%x", i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 } else {
486 /* remove trailing cr/lf and dots */
487 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
488 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200489 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 }
491 }
492 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000493#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (message == NULL)
496 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000497#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 return NULL;
501 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000502
Larry Hastingsb0827312014-02-09 22:05:19 -0800503 if (filenameObject != NULL) {
504 if (filenameObject2 != NULL)
505 args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
506 else
507 args = Py_BuildValue("(iOO)", i, message, filenameObject);
508 } else {
509 assert(filenameObject2 == NULL);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200510 args = Py_BuildValue("(iO)", i, message);
Larry Hastingsb0827312014-02-09 22:05:19 -0800511 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000513
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200514 if (args != NULL) {
515 v = PyObject_Call(exc, args, NULL);
516 Py_DECREF(args);
517 if (v != NULL) {
518 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
519 Py_DECREF(v);
520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000522#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000524#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000526}
Guido van Rossum743007d1999-04-21 15:27:31 +0000527
Barry Warsaw97d95151998-07-23 16:05:56 +0000528PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000529PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800532 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 Py_XDECREF(name);
534 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000535}
536
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000537#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000538PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000539PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject *name = filename ?
542 PyUnicode_FromUnicode(filename, wcslen(filename)) :
543 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800544 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_XDECREF(name);
546 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000547}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000548#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000549
550PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000551PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000552{
Larry Hastingsb0827312014-02-09 22:05:19 -0800553 return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000554}
Guido van Rossum683a0721990-10-21 22:09:12 +0000555
Brett Cannonbf364092006-03-01 04:25:17 +0000556#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000557/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000558PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyObject *exc,
560 int ierr,
561 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000562{
Larry Hastingsb0827312014-02-09 22:05:19 -0800563 return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
564 filenameObject, NULL);
565}
566
567PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
568 PyObject *exc,
569 int ierr,
570 PyObject *filenameObject,
571 PyObject *filenameObject2)
572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 int len;
574 WCHAR *s_buf = NULL; /* Free via LocalFree */
575 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200576 PyObject *args, *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 DWORD err = (DWORD)ierr;
578 if (err==0) err = GetLastError();
579 len = FormatMessageW(
580 /* Error API error */
581 FORMAT_MESSAGE_ALLOCATE_BUFFER |
582 FORMAT_MESSAGE_FROM_SYSTEM |
583 FORMAT_MESSAGE_IGNORE_INSERTS,
584 NULL, /* no message source */
585 err,
586 MAKELANGID(LANG_NEUTRAL,
587 SUBLANG_DEFAULT), /* Default language */
588 (LPWSTR) &s_buf,
589 0, /* size not used */
590 NULL); /* no args */
591 if (len==0) {
592 /* Only seen this in out of mem situations */
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300593 message = PyUnicode_FromFormat("Windows Error 0x%x", err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 s_buf = NULL;
595 } else {
596 /* remove trailing cr/lf and dots */
597 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
598 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200599 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 if (message == NULL)
603 {
604 LocalFree(s_buf);
605 return NULL;
606 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000607
Larry Hastingsb0827312014-02-09 22:05:19 -0800608 if (filenameObject == NULL) {
609 assert(filenameObject2 == NULL);
610 filenameObject = filenameObject2 = Py_None;
611 }
612 else if (filenameObject2 == NULL)
613 filenameObject2 = Py_None;
614 /* This is the constructor signature for OSError.
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200615 The POSIX translation will be figured out by the constructor. */
Larry Hastingsb0827312014-02-09 22:05:19 -0800616 args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000618
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200619 if (args != NULL) {
620 v = PyObject_Call(exc, args, NULL);
621 Py_DECREF(args);
622 if (v != NULL) {
623 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
624 Py_DECREF(v);
625 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 }
627 LocalFree(s_buf);
628 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000629}
630
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000631PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PyObject *exc,
633 int ierr,
634 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635{
Victor Stinner92be9392010-12-28 00:28:21 +0000636 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800637 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800639 name,
640 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 Py_XDECREF(name);
642 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000643}
644
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000645PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 PyObject *exc,
647 int ierr,
648 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyObject *name = filename ?
651 PyUnicode_FromUnicode(filename, wcslen(filename)) :
652 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800653 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800655 name,
656 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 Py_XDECREF(name);
658 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000659}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000660
Thomas Heller085358a2002-07-29 14:27:41 +0000661PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
662{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800663 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000664}
665
Guido van Rossum795e1892000-02-17 15:19:15 +0000666PyObject *PyErr_SetFromWindowsErr(int ierr)
667{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800668 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
669 ierr, NULL);
Larry Hastingsb0827312014-02-09 22:05:19 -0800670}
671
Thomas Heller085358a2002-07-29 14:27:41 +0000672PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 int ierr,
674 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000675{
Victor Stinner92be9392010-12-28 00:28:21 +0000676 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800677 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200678 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800679 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_XDECREF(name);
681 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000682}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000683
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000684PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 int ierr,
686 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyObject *name = filename ?
689 PyUnicode_FromUnicode(filename, wcslen(filename)) :
690 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800691 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200692 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800693 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 Py_XDECREF(name);
695 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000696}
Guido van Rossum795e1892000-02-17 15:19:15 +0000697#endif /* MS_WINDOWS */
698
Brett Cannon79ec55e2012-04-12 20:24:54 -0400699PyObject *
Eric Snow46f97b82016-09-07 16:56:15 -0700700PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
701 PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400702{
Eric Snow46f97b82016-09-07 16:56:15 -0700703 int issubclass;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200704 PyObject *kwargs, *error;
Brian Curtin09b86d12012-04-17 16:57:09 -0500705
Eric Snow46f97b82016-09-07 16:56:15 -0700706 issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
707 if (issubclass < 0) {
708 return NULL;
709 }
710 else if (!issubclass) {
711 PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError");
Brian Curtin94c001b2012-04-18 08:30:51 -0500712 return NULL;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200713 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500714
Eric Snow46f97b82016-09-07 16:56:15 -0700715 if (msg == NULL) {
716 PyErr_SetString(PyExc_TypeError, "expected a message argument");
Brian Curtin09b86d12012-04-17 16:57:09 -0500717 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400718 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500719
Brian Curtin94c001b2012-04-18 08:30:51 -0500720 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500721 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500722 }
Brian Curtin94c001b2012-04-18 08:30:51 -0500723 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500724 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500725 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500726
Eric Snow46f97b82016-09-07 16:56:15 -0700727 kwargs = PyDict_New();
728 if (kwargs == NULL) {
729 return NULL;
730 }
Victor Stinnerf45a5612016-08-23 00:04:41 +0200731 if (PyDict_SetItemString(kwargs, "name", name) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300732 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200733 }
734 if (PyDict_SetItemString(kwargs, "path", path) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300735 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200736 }
Brett Cannon79ec55e2012-04-12 20:24:54 -0400737
Eric Snow46f97b82016-09-07 16:56:15 -0700738 error = _PyObject_FastCallDict(exception, &msg, 1, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400739 if (error != NULL) {
740 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500741 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400742 }
743
Berker Peksagec766d32016-05-01 09:06:36 +0300744done:
Brett Cannon79ec55e2012-04-12 20:24:54 -0400745 Py_DECREF(kwargs);
Brian Curtin09b86d12012-04-17 16:57:09 -0500746 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400747}
748
Eric Snow46f97b82016-09-07 16:56:15 -0700749PyObject *
750PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
751{
752 return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
753}
754
Guido van Rossum683a0721990-10-21 22:09:12 +0000755void
Neal Norwitzb382b842007-08-24 20:00:37 +0000756_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PyErr_Format(PyExc_SystemError,
759 "%s:%d: bad argument to internal function",
760 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000761}
762
763/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
764 export the entry point for existing object code: */
765#undef PyErr_BadInternalCall
766void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000767PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000768{
Victor Stinnerfb3a6302013-07-12 00:37:30 +0200769 assert(0 && "bad argument to internal function");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyErr_Format(PyExc_SystemError,
771 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000772}
Fred Drake6d63adf2000-08-24 22:38:39 +0000773#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
774
Guido van Rossum1548bac1997-02-14 17:09:47 +0000775
Guido van Rossum1548bac1997-02-14 17:09:47 +0000776PyObject *
Antoine Pitrou0676a402014-09-30 21:16:27 +0200777PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000780
Victor Stinnerde821be2015-03-24 12:41:23 +0100781 /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
782 exception set, it calls arbitrary Python code like PyObject_Repr() */
Victor Stinnerace47d72013-07-18 01:41:08 +0200783 PyErr_Clear();
Victor Stinnerace47d72013-07-18 01:41:08 +0200784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 string = PyUnicode_FromFormatV(format, vargs);
Victor Stinnerde821be2015-03-24 12:41:23 +0100786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyErr_SetObject(exception, string);
788 Py_XDECREF(string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000790}
Guido van Rossum7617e051997-09-16 18:43:50 +0000791
792
Antoine Pitrou0676a402014-09-30 21:16:27 +0200793PyObject *
794PyErr_Format(PyObject *exception, const char *format, ...)
795{
796 va_list vargs;
797#ifdef HAVE_STDARG_PROTOTYPES
798 va_start(vargs, format);
799#else
800 va_start(vargs);
801#endif
802 PyErr_FormatV(exception, format, vargs);
803 va_end(vargs);
804 return NULL;
805}
806
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807
Guido van Rossum7617e051997-09-16 18:43:50 +0000808PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000809PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 const char *dot;
812 PyObject *modulename = NULL;
813 PyObject *classname = NULL;
814 PyObject *mydict = NULL;
815 PyObject *bases = NULL;
816 PyObject *result = NULL;
817 dot = strrchr(name, '.');
818 if (dot == NULL) {
819 PyErr_SetString(PyExc_SystemError,
820 "PyErr_NewException: name must be module.class");
821 return NULL;
822 }
823 if (base == NULL)
824 base = PyExc_Exception;
825 if (dict == NULL) {
826 dict = mydict = PyDict_New();
827 if (dict == NULL)
828 goto failure;
829 }
830 if (PyDict_GetItemString(dict, "__module__") == NULL) {
831 modulename = PyUnicode_FromStringAndSize(name,
832 (Py_ssize_t)(dot-name));
833 if (modulename == NULL)
834 goto failure;
835 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
836 goto failure;
837 }
838 if (PyTuple_Check(base)) {
839 bases = base;
840 /* INCREF as we create a new ref in the else branch */
841 Py_INCREF(bases);
842 } else {
843 bases = PyTuple_Pack(1, base);
844 if (bases == NULL)
845 goto failure;
846 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100847 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000848 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000850 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 Py_XDECREF(bases);
852 Py_XDECREF(mydict);
853 Py_XDECREF(classname);
854 Py_XDECREF(modulename);
855 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +0000856}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000857
Georg Brandl1e28a272009-12-28 08:41:01 +0000858
859/* Create an exception with docstring */
860PyObject *
861PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 int result;
865 PyObject *ret = NULL;
866 PyObject *mydict = NULL; /* points to the dict only if we create it */
867 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (dict == NULL) {
870 dict = mydict = PyDict_New();
871 if (dict == NULL) {
872 return NULL;
873 }
874 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (doc != NULL) {
877 docobj = PyUnicode_FromString(doc);
878 if (docobj == NULL)
879 goto failure;
880 result = PyDict_SetItemString(dict, "__doc__", docobj);
881 Py_DECREF(docobj);
882 if (result < 0)
883 goto failure;
884 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +0000887 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_XDECREF(mydict);
889 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +0000890}
891
892
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000893/* Call when an exception has occurred but there is no way for Python
894 to handle it. Examples: exception in __del__ or during GC. */
895void
896PyErr_WriteUnraisable(PyObject *obj)
897{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200898 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 PyObject *f, *t, *v, *tb;
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200900 PyObject *moduleName = NULL;
901 char* className;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000902
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200903 PyErr_Fetch(&t, &v, &tb);
904
Victor Stinnerbd303c12013-11-07 23:07:29 +0100905 f = _PySys_GetObjectId(&PyId_stderr);
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200906 if (f == NULL || f == Py_None)
907 goto done;
908
909 if (obj) {
910 if (PyFile_WriteString("Exception ignored in: ", f) < 0)
911 goto done;
Martin Panter3263f682016-02-28 03:16:11 +0000912 if (PyFile_WriteObject(obj, f, 0) < 0) {
913 PyErr_Clear();
914 if (PyFile_WriteString("<object repr() failed>", f) < 0) {
915 goto done;
916 }
917 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200918 if (PyFile_WriteString("\n", f) < 0)
919 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200921
922 if (PyTraceBack_Print(tb, f) < 0)
923 goto done;
924
925 if (!t)
926 goto done;
927
928 assert(PyExceptionClass_Check(t));
929 className = PyExceptionClass_Name(t);
930 if (className != NULL) {
931 char *dot = strrchr(className, '.');
932 if (dot != NULL)
933 className = dot+1;
934 }
935
936 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
937 if (moduleName == NULL) {
938 PyErr_Clear();
939 if (PyFile_WriteString("<unknown>", f) < 0)
940 goto done;
941 }
942 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100943 if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0) {
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200944 if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
945 goto done;
946 if (PyFile_WriteString(".", f) < 0)
947 goto done;
948 }
949 }
950 if (className == NULL) {
951 if (PyFile_WriteString("<unknown>", f) < 0)
952 goto done;
953 }
954 else {
955 if (PyFile_WriteString(className, f) < 0)
956 goto done;
957 }
958
959 if (v && v != Py_None) {
960 if (PyFile_WriteString(": ", f) < 0)
961 goto done;
Martin Panter3263f682016-02-28 03:16:11 +0000962 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) {
963 PyErr_Clear();
964 if (PyFile_WriteString("<exception str() failed>", f) < 0) {
965 goto done;
966 }
967 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200968 }
969 if (PyFile_WriteString("\n", f) < 0)
970 goto done;
971
972done:
973 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_XDECREF(t);
975 Py_XDECREF(v);
976 Py_XDECREF(tb);
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200977 PyErr_Clear(); /* Just in case */
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000978}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000979
Armin Rigo092381a2003-10-25 14:29:27 +0000980extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000981
Guido van Rossum2fd45652001-02-28 21:46:24 +0000982
Benjamin Peterson2c539712010-09-20 22:42:10 +0000983void
Victor Stinner14e461d2013-08-26 22:28:21 +0200984PyErr_SyntaxLocation(const char *filename, int lineno)
985{
Benjamin Peterson2c539712010-09-20 22:42:10 +0000986 PyErr_SyntaxLocationEx(filename, lineno, -1);
987}
988
989
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000990/* Set file and line information for the current exception.
991 If the exception is not a SyntaxError, also sets additional attributes
992 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000993
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000994void
Victor Stinner14e461d2013-08-26 22:28:21 +0200995PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200998 _Py_IDENTIFIER(filename);
999 _Py_IDENTIFIER(lineno);
1000 _Py_IDENTIFIER(msg);
1001 _Py_IDENTIFIER(offset);
1002 _Py_IDENTIFIER(print_file_and_line);
1003 _Py_IDENTIFIER(text);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* add attributes for the line number and filename for the error */
1006 PyErr_Fetch(&exc, &v, &tb);
1007 PyErr_NormalizeException(&exc, &v, &tb);
1008 /* XXX check that it is, indeed, a syntax error. It might not
1009 * be, though. */
1010 tmp = PyLong_FromLong(lineno);
1011 if (tmp == NULL)
1012 PyErr_Clear();
1013 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001014 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyErr_Clear();
1016 Py_DECREF(tmp);
1017 }
Benjamin Peterson2c539712010-09-20 22:42:10 +00001018 if (col_offset >= 0) {
1019 tmp = PyLong_FromLong(col_offset);
1020 if (tmp == NULL)
1021 PyErr_Clear();
1022 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001023 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
Benjamin Peterson2c539712010-09-20 22:42:10 +00001024 PyErr_Clear();
1025 Py_DECREF(tmp);
1026 }
1027 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (filename != NULL) {
Victor Stinner14e461d2013-08-26 22:28:21 +02001029 if (_PyObject_SetAttrId(v, &PyId_filename, filename))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001031
Victor Stinner14e461d2013-08-26 22:28:21 +02001032 tmp = PyErr_ProgramTextObject(filename, lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001034 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyErr_Clear();
1036 Py_DECREF(tmp);
1037 }
1038 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001039 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyErr_Clear();
1041 }
1042 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001043 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 tmp = PyObject_Str(v);
1045 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001046 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 PyErr_Clear();
1048 Py_DECREF(tmp);
1049 } else {
1050 PyErr_Clear();
1051 }
1052 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001053 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
1054 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
1055 Py_None))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 PyErr_Clear();
1057 }
1058 }
1059 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001060}
1061
Victor Stinner14e461d2013-08-26 22:28:21 +02001062void
1063PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
1064{
1065 PyObject *fileobj;
1066 if (filename != NULL) {
1067 fileobj = PyUnicode_DecodeFSDefault(filename);
1068 if (fileobj == NULL)
1069 PyErr_Clear();
1070 }
1071 else
1072 fileobj = NULL;
1073 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
1074 Py_XDECREF(fileobj);
1075}
1076
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001077/* Attempt to load the line of text that the exception refers to. If it
1078 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001079
1080 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001081 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001082
Antoine Pitrou409b5382013-10-12 22:41:17 +02001083static PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001084err_programtext(FILE *fp, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 int i;
1087 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (fp == NULL)
1090 return NULL;
1091 for (i = 0; i < lineno; i++) {
1092 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1093 do {
1094 *pLastChar = '\0';
1095 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1096 fp, NULL) == NULL)
1097 break;
1098 /* fgets read *something*; if it didn't get as
1099 far as pLastChar, it must have found a newline
1100 or hit the end of the file; if pLastChar is \n,
1101 it obviously found a newline; else we haven't
1102 yet seen a newline, so must continue */
1103 } while (*pLastChar != '\0' && *pLastChar != '\n');
1104 }
1105 fclose(fp);
1106 if (i == lineno) {
1107 char *p = linebuf;
1108 PyObject *res;
1109 while (*p == ' ' || *p == '\t' || *p == '\014')
1110 p++;
1111 res = PyUnicode_FromString(p);
1112 if (res == NULL)
1113 PyErr_Clear();
1114 return res;
1115 }
1116 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001117}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118
Victor Stinner14e461d2013-08-26 22:28:21 +02001119PyObject *
1120PyErr_ProgramText(const char *filename, int lineno)
1121{
1122 FILE *fp;
1123 if (filename == NULL || *filename == '\0' || lineno <= 0)
1124 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001125 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
Victor Stinner14e461d2013-08-26 22:28:21 +02001126 return err_programtext(fp, lineno);
1127}
1128
1129PyObject *
1130PyErr_ProgramTextObject(PyObject *filename, int lineno)
1131{
1132 FILE *fp;
1133 if (filename == NULL || lineno <= 0)
1134 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001135 fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
Victor Stinnere42ccd22015-03-18 01:39:23 +01001136 if (fp == NULL) {
1137 PyErr_Clear();
1138 return NULL;
1139 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001140 return err_programtext(fp, lineno);
1141}
1142
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143#ifdef __cplusplus
1144}
1145#endif