blob: aed2bdc12d6b1734b58c8e8f86cc96ccae3b84c2 [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
55void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000056PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 PyThreadState *tstate = PyThreadState_GET();
59 PyObject *exc_value;
60 PyObject *tb = NULL;
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 if (exception != NULL &&
63 !PyExceptionClass_Check(exception)) {
64 PyErr_Format(PyExc_SystemError,
65 "exception %R not a BaseException subclass",
66 exception);
67 return;
68 }
69 Py_XINCREF(value);
70 exc_value = tstate->exc_value;
71 if (exc_value != NULL && exc_value != Py_None) {
72 /* Implicit exception chaining */
73 Py_INCREF(exc_value);
74 if (value == NULL || !PyExceptionInstance_Check(value)) {
75 /* We must normalize the value right now */
76 PyObject *args, *fixed_value;
Victor Stinnerde821be2015-03-24 12:41:23 +010077
78 /* Issue #23571: PyEval_CallObject() must not be called with an
79 exception set */
Victor Stinnerace47d72013-07-18 01:41:08 +020080 PyErr_Clear();
Victor Stinnerde821be2015-03-24 12:41:23 +010081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (value == NULL || value == Py_None)
83 args = PyTuple_New(0);
84 else if (PyTuple_Check(value)) {
85 Py_INCREF(value);
86 args = value;
87 }
88 else
89 args = PyTuple_Pack(1, value);
90 fixed_value = args ?
91 PyEval_CallObject(exception, args) : NULL;
92 Py_XDECREF(args);
93 Py_XDECREF(value);
94 if (fixed_value == NULL)
95 return;
96 value = fixed_value;
97 }
98 /* Avoid reference cycles through the context chain.
99 This is O(chain length) but context chains are
100 usually very short. Sensitive readers may try
101 to inline the call to PyException_GetContext. */
102 if (exc_value != value) {
103 PyObject *o = exc_value, *context;
104 while ((context = PyException_GetContext(o))) {
105 Py_DECREF(context);
106 if (context == value) {
107 PyException_SetContext(o, NULL);
108 break;
109 }
110 o = context;
111 }
112 PyException_SetContext(value, exc_value);
113 } else {
114 Py_DECREF(exc_value);
115 }
116 }
117 if (value != NULL && PyExceptionInstance_Check(value))
118 tb = PyException_GetTraceback(value);
119 Py_XINCREF(exception);
120 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Raymond Hettinger69492da2013-09-02 15:59:26 -0700123/* Set a key error with the specified argument, wrapping it in a
124 * tuple automatically so that tuple keys are not unpacked as the
125 * exception arguments. */
126void
127_PyErr_SetKeyError(PyObject *arg)
128{
129 PyObject *tup;
130 tup = PyTuple_Pack(1, arg);
131 if (!tup)
132 return; /* caller will expect error to be set anyway */
133 PyErr_SetObject(PyExc_KeyError, tup);
134 Py_DECREF(tup);
135}
136
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000138PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
142
143void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 PyObject *value = PyUnicode_FromString(string);
147 PyErr_SetObject(exception, value);
148 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149}
150
Guido van Rossum3a241811994-08-29 12:14:12 +0000151
Guido van Rossum373c8691997-04-29 18:22:47 +0000152PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000155 /* If there is no thread state, PyThreadState_GET calls
156 Py_FatalError, which calls PyErr_Occurred. To avoid the
157 resulting infinite loop, we inline PyThreadState_GET here and
158 treat no thread as no error. */
159 PyThreadState *tstate =
160 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current));
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000162 return tstate == NULL ? NULL : tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163}
164
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000165
166int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000167PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 if (err == NULL || exc == NULL) {
170 /* maybe caused by "import exceptions" that failed early on */
171 return 0;
172 }
173 if (PyTuple_Check(exc)) {
174 Py_ssize_t i, n;
175 n = PyTuple_Size(exc);
176 for (i = 0; i < n; i++) {
177 /* Test recursively */
178 if (PyErr_GivenExceptionMatches(
179 err, PyTuple_GET_ITEM(exc, i)))
180 {
181 return 1;
182 }
183 }
184 return 0;
185 }
186 /* err might be an instance, so check its class. */
187 if (PyExceptionInstance_Check(err))
188 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
191 int res = 0;
192 PyObject *exception, *value, *tb;
193 PyErr_Fetch(&exception, &value, &tb);
194 /* PyObject_IsSubclass() can recurse and therefore is
195 not safe (see test_bad_getattr in test.pickletester). */
196 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
197 /* This function must not fail, so print the error here */
198 if (res == -1) {
199 PyErr_WriteUnraisable(err);
200 res = 0;
201 }
202 PyErr_Restore(exception, value, tb);
203 return res;
204 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000207}
Guido van Rossum743007d1999-04-21 15:27:31 +0000208
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000209
210int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000214}
215
216
217/* Used in many places to normalize a raised exception, including in
218 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000219
220 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000222*/
223void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 PyObject *type = *exc;
227 PyObject *value = *val;
228 PyObject *inclass = NULL;
229 PyObject *initial_tb = NULL;
230 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (type == NULL) {
233 /* There was no exception, so nothing to do. */
234 return;
235 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 /* If PyErr_SetNone() was used, the value will have been actually
238 set to NULL.
239 */
240 if (!value) {
241 value = Py_None;
242 Py_INCREF(value);
243 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (PyExceptionInstance_Check(value))
246 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 /* Normalize the exception so that if the type is a class, the
249 value will be an instance.
250 */
251 if (PyExceptionClass_Check(type)) {
Victor Stinner74a7fa62013-07-17 00:44:53 +0200252 int is_subclass;
253 if (inclass) {
254 is_subclass = PyObject_IsSubclass(inclass, type);
255 if (is_subclass < 0)
256 goto finally;
257 }
258 else
259 is_subclass = 0;
260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* if the value was not an instance, or is not an instance
262 whose class is (or is derived from) type, then use the
263 value as an argument to instantiation of the type
264 class.
265 */
Victor Stinner74a7fa62013-07-17 00:44:53 +0200266 if (!inclass || !is_subclass) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 PyObject *args, *res;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (value == Py_None)
270 args = PyTuple_New(0);
271 else if (PyTuple_Check(value)) {
272 Py_INCREF(value);
273 args = value;
274 }
275 else
276 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (args == NULL)
279 goto finally;
280 res = PyEval_CallObject(type, args);
281 Py_DECREF(args);
282 if (res == NULL)
283 goto finally;
284 Py_DECREF(value);
285 value = res;
286 }
287 /* if the class of the instance doesn't exactly match the
288 class of the type, believe the instance
289 */
290 else if (inclass != type) {
291 Py_DECREF(type);
292 type = inclass;
293 Py_INCREF(type);
294 }
295 }
296 *exc = type;
297 *val = value;
298 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000299finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 Py_DECREF(type);
301 Py_DECREF(value);
302 /* If the new exception doesn't set a traceback and the old
303 exception had a traceback, use the old traceback for the
304 new exception. It's better than nothing.
305 */
306 initial_tb = *tb;
307 PyErr_Fetch(exc, val, tb);
308 if (initial_tb != NULL) {
309 if (*tb == NULL)
310 *tb = initial_tb;
311 else
312 Py_DECREF(initial_tb);
313 }
314 /* normalize recursively */
315 tstate = PyThreadState_GET();
316 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
317 --tstate->recursion_depth;
318 /* throw away the old exception... */
319 Py_DECREF(*exc);
320 Py_DECREF(*val);
321 /* ... and use the recursion error instead */
Yury Selivanovf488fb42015-07-03 01:04:23 -0400322 *exc = PyExc_RecursionError;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 *val = PyExc_RecursionErrorInst;
324 Py_INCREF(*exc);
325 Py_INCREF(*val);
326 /* just keeping the old traceback */
327 return;
328 }
329 PyErr_NormalizeException(exc, val, tb);
330 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000331}
332
333
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 *p_type = tstate->curexc_type;
340 *p_value = tstate->curexc_value;
341 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 tstate->curexc_type = NULL;
344 tstate->curexc_value = NULL;
345 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346}
347
348void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000349PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000353
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200354void
355PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
356{
357 PyThreadState *tstate = PyThreadState_GET();
358
359 *p_type = tstate->exc_type;
360 *p_value = tstate->exc_value;
361 *p_traceback = tstate->exc_traceback;
362
363 Py_XINCREF(*p_type);
364 Py_XINCREF(*p_value);
365 Py_XINCREF(*p_traceback);
366}
367
368void
369PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
370{
371 PyObject *oldtype, *oldvalue, *oldtraceback;
372 PyThreadState *tstate = PyThreadState_GET();
373
374 oldtype = tstate->exc_type;
375 oldvalue = tstate->exc_value;
376 oldtraceback = tstate->exc_traceback;
377
378 tstate->exc_type = p_type;
379 tstate->exc_value = p_value;
380 tstate->exc_traceback = p_traceback;
381
382 Py_XDECREF(oldtype);
383 Py_XDECREF(oldvalue);
384 Py_XDECREF(oldtraceback);
385}
386
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300387/* Like PyErr_Restore(), but if an exception is already set,
388 set the context associated with it.
389 */
390void
391_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
392{
393 if (exc == NULL)
394 return;
395
396 if (PyErr_Occurred()) {
397 PyObject *exc2, *val2, *tb2;
398 PyErr_Fetch(&exc2, &val2, &tb2);
399 PyErr_NormalizeException(&exc, &val, &tb);
400 Py_DECREF(exc);
401 Py_XDECREF(tb);
402 PyErr_NormalizeException(&exc2, &val2, &tb2);
403 PyException_SetContext(val2, val);
404 PyErr_Restore(exc2, val2, tb2);
405 }
406 else {
407 PyErr_Restore(exc, val, tb);
408 }
409}
410
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000411/* Convenience functions to set a type error exception and return 0 */
412
413int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000414PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 PyErr_SetString(PyExc_TypeError,
417 "bad argument type for built-in operation");
418 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000419}
420
Guido van Rossum373c8691997-04-29 18:22:47 +0000421PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000422PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000423{
Victor Stinnerf54a5742013-07-22 22:28:37 +0200424 if (Py_TYPE(PyExc_MemoryError) == NULL) {
425 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
426 initialized by _PyExc_Init() */
427 Py_FatalError("Out of memory and PyExc_MemoryError is not "
428 "initialized yet");
429 }
Antoine Pitrou07e20ef2010-10-28 22:56:58 +0000430 PyErr_SetNone(PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000432}
433
Guido van Rossum373c8691997-04-29 18:22:47 +0000434PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000435PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000436{
Larry Hastingsb0827312014-02-09 22:05:19 -0800437 return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
438}
439
440PyObject *
441PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200444 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100446#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000448#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000449
Guido van Rossume9fbc091995-02-18 14:52:19 +0000450#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (i == EINTR && PyErr_CheckSignals())
452 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000453#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000454
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000455#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100456 if (i != 0) {
457 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100458 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100459 }
460 else {
461 /* Sometimes errno didn't get set */
462 message = PyUnicode_FromString("Error");
463 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000464#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (i == 0)
466 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
467 else
468 {
469 /* Note that the Win32 errors do not lineup with the
470 errno error. So if the error is in the MSVC error
471 table, we use it, otherwise we assume it really _is_
472 a Win32 error code
473 */
474 if (i > 0 && i < _sys_nerr) {
475 message = PyUnicode_FromString(_sys_errlist[i]);
476 }
477 else {
478 int len = FormatMessageW(
479 FORMAT_MESSAGE_ALLOCATE_BUFFER |
480 FORMAT_MESSAGE_FROM_SYSTEM |
481 FORMAT_MESSAGE_IGNORE_INSERTS,
482 NULL, /* no message source */
483 i,
484 MAKELANGID(LANG_NEUTRAL,
485 SUBLANG_DEFAULT),
486 /* Default language */
487 (LPWSTR) &s_buf,
488 0, /* size not used */
489 NULL); /* no args */
490 if (len==0) {
491 /* Only ever seen this in out-of-mem
492 situations */
493 s_buf = NULL;
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300494 message = PyUnicode_FromFormat("Windows Error 0x%x", i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 } else {
496 /* remove trailing cr/lf and dots */
497 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
498 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200499 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 }
501 }
502 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000503#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (message == NULL)
506 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000507#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000509#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return NULL;
511 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000512
Larry Hastingsb0827312014-02-09 22:05:19 -0800513 if (filenameObject != NULL) {
514 if (filenameObject2 != NULL)
515 args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
516 else
517 args = Py_BuildValue("(iOO)", i, message, filenameObject);
518 } else {
519 assert(filenameObject2 == NULL);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200520 args = Py_BuildValue("(iO)", i, message);
Larry Hastingsb0827312014-02-09 22:05:19 -0800521 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000523
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200524 if (args != NULL) {
525 v = PyObject_Call(exc, args, NULL);
526 Py_DECREF(args);
527 if (v != NULL) {
528 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
529 Py_DECREF(v);
530 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000532#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000534#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000536}
Guido van Rossum743007d1999-04-21 15:27:31 +0000537
Barry Warsaw97d95151998-07-23 16:05:56 +0000538PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000539PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800542 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 Py_XDECREF(name);
544 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000545}
546
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000547#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000548PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000549PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyObject *name = filename ?
552 PyUnicode_FromUnicode(filename, wcslen(filename)) :
553 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800554 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 Py_XDECREF(name);
556 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000558#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000559
560PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000562{
Larry Hastingsb0827312014-02-09 22:05:19 -0800563 return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000564}
Guido van Rossum683a0721990-10-21 22:09:12 +0000565
Brett Cannonbf364092006-03-01 04:25:17 +0000566#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000567/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000568PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 PyObject *exc,
570 int ierr,
571 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000572{
Larry Hastingsb0827312014-02-09 22:05:19 -0800573 return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
574 filenameObject, NULL);
575}
576
577PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
578 PyObject *exc,
579 int ierr,
580 PyObject *filenameObject,
581 PyObject *filenameObject2)
582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 int len;
584 WCHAR *s_buf = NULL; /* Free via LocalFree */
585 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200586 PyObject *args, *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 DWORD err = (DWORD)ierr;
588 if (err==0) err = GetLastError();
589 len = FormatMessageW(
590 /* Error API error */
591 FORMAT_MESSAGE_ALLOCATE_BUFFER |
592 FORMAT_MESSAGE_FROM_SYSTEM |
593 FORMAT_MESSAGE_IGNORE_INSERTS,
594 NULL, /* no message source */
595 err,
596 MAKELANGID(LANG_NEUTRAL,
597 SUBLANG_DEFAULT), /* Default language */
598 (LPWSTR) &s_buf,
599 0, /* size not used */
600 NULL); /* no args */
601 if (len==0) {
602 /* Only seen this in out of mem situations */
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300603 message = PyUnicode_FromFormat("Windows Error 0x%x", err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 s_buf = NULL;
605 } else {
606 /* remove trailing cr/lf and dots */
607 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
608 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200609 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 if (message == NULL)
613 {
614 LocalFree(s_buf);
615 return NULL;
616 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000617
Larry Hastingsb0827312014-02-09 22:05:19 -0800618 if (filenameObject == NULL) {
619 assert(filenameObject2 == NULL);
620 filenameObject = filenameObject2 = Py_None;
621 }
622 else if (filenameObject2 == NULL)
623 filenameObject2 = Py_None;
624 /* This is the constructor signature for OSError.
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200625 The POSIX translation will be figured out by the constructor. */
Larry Hastingsb0827312014-02-09 22:05:19 -0800626 args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000628
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200629 if (args != NULL) {
630 v = PyObject_Call(exc, args, NULL);
631 Py_DECREF(args);
632 if (v != NULL) {
633 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
634 Py_DECREF(v);
635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 }
637 LocalFree(s_buf);
638 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000639}
640
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000641PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyObject *exc,
643 int ierr,
644 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000645{
Victor Stinner92be9392010-12-28 00:28:21 +0000646 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800647 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800649 name,
650 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 Py_XDECREF(name);
652 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000653}
654
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000655PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 PyObject *exc,
657 int ierr,
658 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 PyObject *name = filename ?
661 PyUnicode_FromUnicode(filename, wcslen(filename)) :
662 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800663 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800665 name,
666 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 Py_XDECREF(name);
668 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000669}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000670
Thomas Heller085358a2002-07-29 14:27:41 +0000671PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
672{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800673 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000674}
675
Guido van Rossum795e1892000-02-17 15:19:15 +0000676PyObject *PyErr_SetFromWindowsErr(int ierr)
677{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800678 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
679 ierr, NULL);
Larry Hastingsb0827312014-02-09 22:05:19 -0800680}
681
Thomas Heller085358a2002-07-29 14:27:41 +0000682PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 int ierr,
684 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000685{
Victor Stinner92be9392010-12-28 00:28:21 +0000686 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800687 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200688 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800689 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 Py_XDECREF(name);
691 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000692}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000693
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000694PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 int ierr,
696 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyObject *name = filename ?
699 PyUnicode_FromUnicode(filename, wcslen(filename)) :
700 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800701 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200702 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800703 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 Py_XDECREF(name);
705 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000706}
Guido van Rossum795e1892000-02-17 15:19:15 +0000707#endif /* MS_WINDOWS */
708
Brett Cannon79ec55e2012-04-12 20:24:54 -0400709PyObject *
Brett Cannon82da8882013-07-04 17:48:16 -0400710PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400711{
Brian Curtin09b86d12012-04-17 16:57:09 -0500712 PyObject *args, *kwargs, *error;
713
Brett Cannon82da8882013-07-04 17:48:16 -0400714 if (msg == NULL)
Brian Curtin94c001b2012-04-18 08:30:51 -0500715 return NULL;
716
Antoine Pitrouec9bac42012-04-18 16:57:54 +0200717 args = PyTuple_New(1);
Brian Curtin09b86d12012-04-17 16:57:09 -0500718 if (args == NULL)
719 return NULL;
720
721 kwargs = PyDict_New();
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400722 if (kwargs == NULL) {
723 Py_DECREF(args);
Brian Curtin09b86d12012-04-17 16:57:09 -0500724 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400725 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500726
Brian Curtin94c001b2012-04-18 08:30:51 -0500727 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500728 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500729 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500730
Brian Curtin94c001b2012-04-18 08:30:51 -0500731 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500732 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500733 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500734
735 Py_INCREF(msg);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400736 PyTuple_SET_ITEM(args, 0, msg);
Victor Stinner479054b2013-07-17 21:54:25 +0200737
738 if (PyDict_SetItemString(kwargs, "name", name) < 0)
739 return NULL;
740 if (PyDict_SetItemString(kwargs, "path", path) < 0)
741 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400742
Brett Cannon82da8882013-07-04 17:48:16 -0400743 error = PyObject_Call(PyExc_ImportError, args, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400744 if (error != NULL) {
745 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500746 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400747 }
748
Brett Cannon79ec55e2012-04-12 20:24:54 -0400749 Py_DECREF(args);
750 Py_DECREF(kwargs);
751
Brian Curtin09b86d12012-04-17 16:57:09 -0500752 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400753}
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;
912 if (PyFile_WriteObject(obj, f, 0) < 0)
913 goto done;
914 if (PyFile_WriteString("\n", f) < 0)
915 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200917
918 if (PyTraceBack_Print(tb, f) < 0)
919 goto done;
920
921 if (!t)
922 goto done;
923
924 assert(PyExceptionClass_Check(t));
925 className = PyExceptionClass_Name(t);
926 if (className != NULL) {
927 char *dot = strrchr(className, '.');
928 if (dot != NULL)
929 className = dot+1;
930 }
931
932 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
933 if (moduleName == NULL) {
934 PyErr_Clear();
935 if (PyFile_WriteString("<unknown>", f) < 0)
936 goto done;
937 }
938 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100939 if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0) {
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200940 if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
941 goto done;
942 if (PyFile_WriteString(".", f) < 0)
943 goto done;
944 }
945 }
946 if (className == NULL) {
947 if (PyFile_WriteString("<unknown>", f) < 0)
948 goto done;
949 }
950 else {
951 if (PyFile_WriteString(className, f) < 0)
952 goto done;
953 }
954
955 if (v && v != Py_None) {
956 if (PyFile_WriteString(": ", f) < 0)
957 goto done;
958 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
959 goto done;
960 }
961 if (PyFile_WriteString("\n", f) < 0)
962 goto done;
963
964done:
965 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_XDECREF(t);
967 Py_XDECREF(v);
968 Py_XDECREF(tb);
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200969 PyErr_Clear(); /* Just in case */
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000970}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000971
Armin Rigo092381a2003-10-25 14:29:27 +0000972extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000973
Guido van Rossum2fd45652001-02-28 21:46:24 +0000974
Benjamin Peterson2c539712010-09-20 22:42:10 +0000975void
Victor Stinner14e461d2013-08-26 22:28:21 +0200976PyErr_SyntaxLocation(const char *filename, int lineno)
977{
Benjamin Peterson2c539712010-09-20 22:42:10 +0000978 PyErr_SyntaxLocationEx(filename, lineno, -1);
979}
980
981
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000982/* Set file and line information for the current exception.
983 If the exception is not a SyntaxError, also sets additional attributes
984 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000985
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000986void
Victor Stinner14e461d2013-08-26 22:28:21 +0200987PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200990 _Py_IDENTIFIER(filename);
991 _Py_IDENTIFIER(lineno);
992 _Py_IDENTIFIER(msg);
993 _Py_IDENTIFIER(offset);
994 _Py_IDENTIFIER(print_file_and_line);
995 _Py_IDENTIFIER(text);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 /* add attributes for the line number and filename for the error */
998 PyErr_Fetch(&exc, &v, &tb);
999 PyErr_NormalizeException(&exc, &v, &tb);
1000 /* XXX check that it is, indeed, a syntax error. It might not
1001 * be, though. */
1002 tmp = PyLong_FromLong(lineno);
1003 if (tmp == NULL)
1004 PyErr_Clear();
1005 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001006 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 PyErr_Clear();
1008 Py_DECREF(tmp);
1009 }
Benjamin Peterson2c539712010-09-20 22:42:10 +00001010 if (col_offset >= 0) {
1011 tmp = PyLong_FromLong(col_offset);
1012 if (tmp == NULL)
1013 PyErr_Clear();
1014 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001015 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
Benjamin Peterson2c539712010-09-20 22:42:10 +00001016 PyErr_Clear();
1017 Py_DECREF(tmp);
1018 }
1019 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 if (filename != NULL) {
Victor Stinner14e461d2013-08-26 22:28:21 +02001021 if (_PyObject_SetAttrId(v, &PyId_filename, filename))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001023
Victor Stinner14e461d2013-08-26 22:28:21 +02001024 tmp = PyErr_ProgramTextObject(filename, lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001026 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 PyErr_Clear();
1028 Py_DECREF(tmp);
1029 }
1030 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001031 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyErr_Clear();
1033 }
1034 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001035 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 tmp = PyObject_Str(v);
1037 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001038 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyErr_Clear();
1040 Py_DECREF(tmp);
1041 } else {
1042 PyErr_Clear();
1043 }
1044 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001045 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
1046 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
1047 Py_None))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 PyErr_Clear();
1049 }
1050 }
1051 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001052}
1053
Victor Stinner14e461d2013-08-26 22:28:21 +02001054void
1055PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
1056{
1057 PyObject *fileobj;
1058 if (filename != NULL) {
1059 fileobj = PyUnicode_DecodeFSDefault(filename);
1060 if (fileobj == NULL)
1061 PyErr_Clear();
1062 }
1063 else
1064 fileobj = NULL;
1065 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
1066 Py_XDECREF(fileobj);
1067}
1068
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001069/* Attempt to load the line of text that the exception refers to. If it
1070 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001071
1072 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001073 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001074
Antoine Pitrou409b5382013-10-12 22:41:17 +02001075static PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001076err_programtext(FILE *fp, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 int i;
1079 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (fp == NULL)
1082 return NULL;
1083 for (i = 0; i < lineno; i++) {
1084 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1085 do {
1086 *pLastChar = '\0';
1087 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1088 fp, NULL) == NULL)
1089 break;
1090 /* fgets read *something*; if it didn't get as
1091 far as pLastChar, it must have found a newline
1092 or hit the end of the file; if pLastChar is \n,
1093 it obviously found a newline; else we haven't
1094 yet seen a newline, so must continue */
1095 } while (*pLastChar != '\0' && *pLastChar != '\n');
1096 }
1097 fclose(fp);
1098 if (i == lineno) {
1099 char *p = linebuf;
1100 PyObject *res;
1101 while (*p == ' ' || *p == '\t' || *p == '\014')
1102 p++;
1103 res = PyUnicode_FromString(p);
1104 if (res == NULL)
1105 PyErr_Clear();
1106 return res;
1107 }
1108 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001109}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001110
Victor Stinner14e461d2013-08-26 22:28:21 +02001111PyObject *
1112PyErr_ProgramText(const char *filename, int lineno)
1113{
1114 FILE *fp;
1115 if (filename == NULL || *filename == '\0' || lineno <= 0)
1116 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001117 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
Victor Stinner14e461d2013-08-26 22:28:21 +02001118 return err_programtext(fp, lineno);
1119}
1120
1121PyObject *
1122PyErr_ProgramTextObject(PyObject *filename, int lineno)
1123{
1124 FILE *fp;
1125 if (filename == NULL || lineno <= 0)
1126 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001127 fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
Victor Stinnere42ccd22015-03-18 01:39:23 +01001128 if (fp == NULL) {
1129 PyErr_Clear();
1130 return NULL;
1131 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001132 return err_programtext(fp, lineno);
1133}
1134
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135#ifdef __cplusplus
1136}
1137#endif