blob: ac64b6a2bc1edfd68b59346ebbfb763837f75406 [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
23
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000025PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000026{
Guido van Rossum885553e1998-12-21 18:33:30 +000027 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000028 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
Guido van Rossuma027efa1997-05-05 20:56:21 +000030 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
31 /* XXX Should never happen -- fatal error instead? */
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000032 /* Well, it could be None. */
Guido van Rossuma027efa1997-05-05 20:56:21 +000033 Py_DECREF(traceback);
34 traceback = NULL;
35 }
36
37 /* Save these in locals to safeguard against recursive
38 invocation through Py_XDECREF */
39 oldtype = tstate->curexc_type;
40 oldvalue = tstate->curexc_value;
41 oldtraceback = tstate->curexc_traceback;
42
43 tstate->curexc_type = type;
44 tstate->curexc_value = value;
45 tstate->curexc_traceback = traceback;
46
47 Py_XDECREF(oldtype);
48 Py_XDECREF(oldvalue);
49 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050}
51
52void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000053PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000055 PyThreadState *tstate = PyThreadState_GET();
56 PyObject *tb = NULL;
57
Thomas Wouters303de6a2006-04-20 22:42:37 +000058 if (exception != NULL &&
59 !PyExceptionClass_Check(exception)) {
Thomas Wouters303de6a2006-04-20 22:42:37 +000060 PyErr_Format(PyExc_SystemError,
Walter Dörwald573c08c2007-05-25 15:46:59 +000061 "exception %R not a BaseException subclass",
62 exception);
Thomas Wouters303de6a2006-04-20 22:42:37 +000063 return;
64 }
Guido van Rossum373c8691997-04-29 18:22:47 +000065 Py_XINCREF(value);
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000066 if (tstate->exc_value != NULL && tstate->exc_value != Py_None) {
67 /* Implicit exception chaining */
68 if (value == NULL || !PyExceptionInstance_Check(value)) {
69 /* We must normalize the value right now */
70 PyObject *args, *fixed_value;
71 if (value == NULL || value == Py_None)
72 args = PyTuple_New(0);
73 else if (PyTuple_Check(value)) {
74 Py_INCREF(value);
75 args = value;
76 }
77 else
78 args = PyTuple_Pack(1, value);
79 fixed_value = args ?
80 PyEval_CallObject(exception, args) : NULL;
81 Py_XDECREF(args);
82 Py_XDECREF(value);
83 if (fixed_value == NULL)
84 return;
85 value = fixed_value;
86 }
87 Py_INCREF(tstate->exc_value);
88 PyException_SetContext(value, tstate->exc_value);
89 }
90 if (value != NULL && PyExceptionInstance_Check(value))
91 tb = PyException_GetTraceback(value);
92 Py_XINCREF(exception);
93 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094}
95
96void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000097PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
Guido van Rossum373c8691997-04-29 18:22:47 +000099 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
102void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000103PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104{
Walter Dörwald573c08c2007-05-25 15:46:59 +0000105 PyObject *value = PyUnicode_FromString(string);
Guido van Rossum373c8691997-04-29 18:22:47 +0000106 PyErr_SetObject(exception, value);
107 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108}
109
Guido van Rossum3a241811994-08-29 12:14:12 +0000110
Guido van Rossum373c8691997-04-29 18:22:47 +0000111PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Tim Peters024da352001-05-30 06:09:50 +0000114 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000115
116 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117}
118
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000119
120int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000121PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000122{
Barry Warsawfa5c3152000-05-02 19:27:51 +0000123 if (err == NULL || exc == NULL) {
124 /* maybe caused by "import exceptions" that failed early on */
125 return 0;
126 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000127 if (PyTuple_Check(exc)) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000128 Py_ssize_t i, n;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000129 n = PyTuple_Size(exc);
130 for (i = 0; i < n; i++) {
131 /* Test recursively */
132 if (PyErr_GivenExceptionMatches(
133 err, PyTuple_GET_ITEM(exc, i)))
134 {
135 return 1;
136 }
137 }
138 return 0;
139 }
140 /* err might be an instance, so check its class. */
Brett Cannonbf364092006-03-01 04:25:17 +0000141 if (PyExceptionInstance_Check(err))
142 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000143
Brett Cannonbf364092006-03-01 04:25:17 +0000144 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
145 /* problems here!? not sure PyObject_IsSubclass expects to
146 be called with an exception pending... */
147 return PyObject_IsSubclass(err, exc);
148 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000149
150 return err == exc;
151}
Guido van Rossum743007d1999-04-21 15:27:31 +0000152
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000153
154int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000156{
157 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
158}
159
160
161/* Used in many places to normalize a raised exception, including in
162 eval_code2(), do_raise(), and PyErr_Print()
163*/
164void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000165PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000166{
167 PyObject *type = *exc;
168 PyObject *value = *val;
169 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000170 PyObject *initial_tb = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000171 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000172
Guido van Rossumed473a42000-08-07 19:18:27 +0000173 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000174 /* There was no exception, so nothing to do. */
175 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000176 }
177
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000178 /* If PyErr_SetNone() was used, the value will have been actually
179 set to NULL.
180 */
181 if (!value) {
182 value = Py_None;
183 Py_INCREF(value);
184 }
185
Brett Cannonbf364092006-03-01 04:25:17 +0000186 if (PyExceptionInstance_Check(value))
187 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000188
189 /* Normalize the exception so that if the type is a class, the
190 value will be an instance.
191 */
Brett Cannonbf364092006-03-01 04:25:17 +0000192 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000193 /* if the value was not an instance, or is not an instance
194 whose class is (or is derived from) type, then use the
195 value as an argument to instantiation of the type
196 class.
197 */
Brett Cannonbf364092006-03-01 04:25:17 +0000198 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000199 PyObject *args, *res;
200
201 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000202 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000203 else if (PyTuple_Check(value)) {
204 Py_INCREF(value);
205 args = value;
206 }
207 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000208 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000209
210 if (args == NULL)
211 goto finally;
212 res = PyEval_CallObject(type, args);
213 Py_DECREF(args);
214 if (res == NULL)
215 goto finally;
216 Py_DECREF(value);
217 value = res;
218 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000219 /* if the class of the instance doesn't exactly match the
220 class of the type, believe the instance
221 */
222 else if (inclass != type) {
223 Py_DECREF(type);
224 type = inclass;
225 Py_INCREF(type);
226 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000227 }
228 *exc = type;
229 *val = value;
230 return;
231finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000232 Py_DECREF(type);
233 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000234 /* If the new exception doesn't set a traceback and the old
235 exception had a traceback, use the old traceback for the
236 new exception. It's better than nothing.
237 */
238 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000239 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000240 if (initial_tb != NULL) {
241 if (*tb == NULL)
242 *tb = initial_tb;
243 else
244 Py_DECREF(initial_tb);
245 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000246 /* normalize recursively */
Thomas Wouters89d996e2007-09-08 17:39:28 +0000247 tstate = PyThreadState_GET();
248 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
249 --tstate->recursion_depth;
250 PyErr_SetObject(PyExc_RuntimeError, PyExc_RecursionErrorInst);
251 return;
252 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000253 PyErr_NormalizeException(exc, val, tb);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000254 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000255}
256
257
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000261 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000262
263 *p_type = tstate->curexc_type;
264 *p_value = tstate->curexc_value;
265 *p_traceback = tstate->curexc_traceback;
266
267 tstate->curexc_type = NULL;
268 tstate->curexc_value = NULL;
269 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270}
271
272void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000273PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000275 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000277
278/* Convenience functions to set a type error exception and return 0 */
279
280int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000282{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000283 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000284 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000285 return 0;
286}
287
Guido van Rossum373c8691997-04-29 18:22:47 +0000288PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000290{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000291 if (PyErr_ExceptionMatches(PyExc_MemoryError))
292 /* already current */
293 return NULL;
294
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000295 /* raise the pre-allocated instance if it still exists */
296 if (PyExc_MemoryErrorInst)
297 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
298 else
299 /* this will probably fail since there's no memory and hee,
300 hee, we have to instantiate this class
301 */
302 PyErr_SetNone(PyExc_MemoryError);
303
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000304 return NULL;
305}
306
Guido van Rossum373c8691997-04-29 18:22:47 +0000307PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000308PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000309{
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000310 PyObject *message;
Guido van Rossum373c8691997-04-29 18:22:47 +0000311 PyObject *v;
Guido van Rossum3a241811994-08-29 12:14:12 +0000312 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000313#ifdef PLAN9
314 char errbuf[ERRMAX];
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000315#else
316#ifndef MS_WINDOWS
317 char *s;
318#else
319 WCHAR *s_buf = NULL;
320#endif /* Unix/Windows */
321#endif /* PLAN 9*/
322
Guido van Rossume9fbc091995-02-18 14:52:19 +0000323#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000324 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000325 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000326#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000327
Martin v. Löwis3484a182002-03-09 12:07:51 +0000328#ifdef PLAN9
329 rerrstr(errbuf, sizeof errbuf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000330 message = PyUnicode_DecodeUTF8(errbuf, strlen(errbuf), "ignore");
Martin v. Löwis3484a182002-03-09 12:07:51 +0000331#else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000332#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000333 if (i == 0)
334 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000335 else
Guido van Rossume0e59821998-10-14 20:38:13 +0000336 s = strerror(i);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000337 message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore");
Guido van Rossum743007d1999-04-21 15:27:31 +0000338#else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000339 if (i == 0)
340 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
341 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000342 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000343 /* Note that the Win32 errors do not lineup with the
344 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000345 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000346 a Win32 error code
347 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000348 if (i > 0 && i < _sys_nerr) {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000349 message = PyUnicode_FromString(_sys_errlist[i]);
Guido van Rossum795e1892000-02-17 15:19:15 +0000350 }
351 else {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000352 int len = FormatMessageW(
Guido van Rossum795e1892000-02-17 15:19:15 +0000353 FORMAT_MESSAGE_ALLOCATE_BUFFER |
354 FORMAT_MESSAGE_FROM_SYSTEM |
355 FORMAT_MESSAGE_IGNORE_INSERTS,
356 NULL, /* no message source */
357 i,
358 MAKELANGID(LANG_NEUTRAL,
359 SUBLANG_DEFAULT),
360 /* Default language */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000361 (LPWSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000362 0, /* size not used */
363 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000364 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000365 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000366 situations */
Mark Hammond3d61a062002-10-04 00:13:02 +0000367 s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000368 message = PyUnicode_FromFormat("Windows Error 0x%X", i);
Mark Hammond3d61a062002-10-04 00:13:02 +0000369 } else {
Mark Hammond3d61a062002-10-04 00:13:02 +0000370 /* remove trailing cr/lf and dots */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000371 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
372 s_buf[--len] = L'\0';
373 message = PyUnicode_FromUnicode(s_buf, len);
Mark Hammond3d61a062002-10-04 00:13:02 +0000374 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000375 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000376 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000377#endif /* Unix/Windows */
378#endif /* PLAN 9*/
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000379
380 if (message == NULL)
381 {
382#ifdef MS_WINDOWS
383 LocalFree(s_buf);
384#endif
385 return NULL;
386 }
387
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000388 if (filenameObject != NULL)
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000389 v = Py_BuildValue("(iOO)", i, message, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000390 else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000391 v = Py_BuildValue("(iO)", i, message);
392 Py_DECREF(message);
393
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000394 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000395 PyErr_SetObject(exc, v);
396 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000397 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000398#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000399 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000400#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000401 return NULL;
402}
Guido van Rossum743007d1999-04-21 15:27:31 +0000403
Barry Warsaw97d95151998-07-23 16:05:56 +0000404
405PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000406PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000407{
Neal Norwitzcd795962007-08-24 19:54:13 +0000408 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000409 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000410 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000411 return result;
412}
413
414#ifdef Py_WIN_WIDE_FILENAMES
415PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000416PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000417{
Brett Cannonbf364092006-03-01 04:25:17 +0000418 PyObject *name = filename ?
419 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000420 NULL;
421 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
422 Py_XDECREF(name);
423 return result;
424}
425#endif /* Py_WIN_WIDE_FILENAMES */
426
427PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000428PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000429{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000430 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000431}
Guido van Rossum683a0721990-10-21 22:09:12 +0000432
Brett Cannonbf364092006-03-01 04:25:17 +0000433#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000434/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000435PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000436 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000437 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000438 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000439{
440 int len;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000441 WCHAR *s_buf = NULL; /* Free via LocalFree */
442 PyObject *message;
Guido van Rossum795e1892000-02-17 15:19:15 +0000443 PyObject *v;
444 DWORD err = (DWORD)ierr;
445 if (err==0) err = GetLastError();
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000446 len = FormatMessageW(
Guido van Rossum795e1892000-02-17 15:19:15 +0000447 /* Error API error */
448 FORMAT_MESSAGE_ALLOCATE_BUFFER |
449 FORMAT_MESSAGE_FROM_SYSTEM |
450 FORMAT_MESSAGE_IGNORE_INSERTS,
451 NULL, /* no message source */
452 err,
453 MAKELANGID(LANG_NEUTRAL,
454 SUBLANG_DEFAULT), /* Default language */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000455 (LPWSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000456 0, /* size not used */
457 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000458 if (len==0) {
459 /* Only seen this in out of mem situations */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000460 message = PyUnicode_FromFormat("Windows Error 0x%X", err);
Mark Hammond3d61a062002-10-04 00:13:02 +0000461 s_buf = NULL;
462 } else {
Mark Hammond3d61a062002-10-04 00:13:02 +0000463 /* remove trailing cr/lf and dots */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000464 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
465 s_buf[--len] = L'\0';
466 message = PyUnicode_FromUnicode(s_buf, len);
Mark Hammond3d61a062002-10-04 00:13:02 +0000467 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000468
469 if (message == NULL)
470 {
471 LocalFree(s_buf);
472 return NULL;
473 }
474
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000475 if (filenameObject != NULL)
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000476 v = Py_BuildValue("(iOO)", err, message, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000477 else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000478 v = Py_BuildValue("(iO)", err, message);
479 Py_DECREF(message);
480
Guido van Rossum795e1892000-02-17 15:19:15 +0000481 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000482 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000483 Py_DECREF(v);
484 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000485 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000486 return NULL;
487}
488
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000489PyObject *PyErr_SetExcFromWindowsErrWithFilename(
490 PyObject *exc,
491 int ierr,
492 const char *filename)
493{
Neal Norwitzcd795962007-08-24 19:54:13 +0000494 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000495 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
496 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000497 name);
498 Py_XDECREF(name);
499 return ret;
500}
501
502#ifdef Py_WIN_WIDE_FILENAMES
503PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
504 PyObject *exc,
505 int ierr,
506 const Py_UNICODE *filename)
507{
Brett Cannonbf364092006-03-01 04:25:17 +0000508 PyObject *name = filename ?
509 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000510 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000511 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
512 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000513 name);
514 Py_XDECREF(name);
515 return ret;
516}
517#endif /* Py_WIN_WIDE_FILENAMES */
518
Thomas Heller085358a2002-07-29 14:27:41 +0000519PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
520{
521 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
522}
523
Guido van Rossum795e1892000-02-17 15:19:15 +0000524PyObject *PyErr_SetFromWindowsErr(int ierr)
525{
Thomas Heller085358a2002-07-29 14:27:41 +0000526 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
527 ierr, NULL);
528}
529PyObject *PyErr_SetFromWindowsErrWithFilename(
530 int ierr,
531 const char *filename)
532{
Neal Norwitzcd795962007-08-24 19:54:13 +0000533 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000534 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
535 PyExc_WindowsError,
536 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000537 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000538 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000539}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000540
541#ifdef Py_WIN_WIDE_FILENAMES
542PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
543 int ierr,
544 const Py_UNICODE *filename)
545{
Brett Cannonbf364092006-03-01 04:25:17 +0000546 PyObject *name = filename ?
547 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000548 NULL;
549 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
550 PyExc_WindowsError,
551 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000552 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000553 return result;
554}
555#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000556#endif /* MS_WINDOWS */
557
Guido van Rossum683a0721990-10-21 22:09:12 +0000558void
Neal Norwitzb382b842007-08-24 20:00:37 +0000559_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000560{
561 PyErr_Format(PyExc_SystemError,
562 "%s:%d: bad argument to internal function",
563 filename, lineno);
564}
565
566/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
567 export the entry point for existing object code: */
568#undef PyErr_BadInternalCall
569void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000570PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000571{
Fred Drake6d63adf2000-08-24 22:38:39 +0000572 PyErr_Format(PyExc_SystemError,
573 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000574}
Fred Drake6d63adf2000-08-24 22:38:39 +0000575#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
576
Guido van Rossum1548bac1997-02-14 17:09:47 +0000577
578
Guido van Rossum1548bac1997-02-14 17:09:47 +0000579PyObject *
580PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000581{
582 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000583 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000584
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000585#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000586 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000587#else
588 va_start(vargs);
589#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000590
Walter Dörwald573c08c2007-05-25 15:46:59 +0000591 string = PyUnicode_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000592 PyErr_SetObject(exception, string);
593 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000594 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000595 return NULL;
596}
Guido van Rossum7617e051997-09-16 18:43:50 +0000597
598
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599
Guido van Rossum7617e051997-09-16 18:43:50 +0000600PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000601PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000602{
Neal Norwitzb382b842007-08-24 20:00:37 +0000603 const char *dot;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000604 PyObject *modulename = NULL;
605 PyObject *classname = NULL;
606 PyObject *mydict = NULL;
607 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000608 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000609 dot = strrchr(name, '.');
610 if (dot == NULL) {
611 PyErr_SetString(PyExc_SystemError,
612 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000613 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000614 }
615 if (base == NULL)
616 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000617 if (dict == NULL) {
618 dict = mydict = PyDict_New();
619 if (dict == NULL)
620 goto failure;
621 }
622 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Neal Norwitzcd795962007-08-24 19:54:13 +0000623 modulename = PyUnicode_FromStringAndSize(name,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000624 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000625 if (modulename == NULL)
626 goto failure;
627 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
628 goto failure;
629 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000630 if (PyTuple_Check(base)) {
631 bases = base;
632 /* INCREF as we create a new ref in the else branch */
633 Py_INCREF(bases);
634 } else {
635 bases = PyTuple_Pack(1, base);
636 if (bases == NULL)
637 goto failure;
638 }
639 /* Create a real new-style class. */
Guido van Rossume845c0f2007-11-02 23:07:07 +0000640 result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000642 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000643 Py_XDECREF(bases);
644 Py_XDECREF(mydict);
645 Py_XDECREF(classname);
646 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000647 return result;
648}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000649
650/* Call when an exception has occurred but there is no way for Python
651 to handle it. Examples: exception in __del__ or during GC. */
652void
653PyErr_WriteUnraisable(PyObject *obj)
654{
655 PyObject *f, *t, *v, *tb;
656 PyErr_Fetch(&t, &v, &tb);
657 f = PySys_GetObject("stderr");
Christian Heimes2be03732007-11-15 02:26:46 +0000658 if (f != NULL && f != Py_None) {
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000659 PyFile_WriteString("Exception ", f);
660 if (t) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661 PyObject* moduleName;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000662 char* className;
663 assert(PyExceptionClass_Check(t));
664 className = PyExceptionClass_Name(t);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000665 if (className != NULL) {
666 char *dot = strrchr(className, '.');
667 if (dot != NULL)
668 className = dot+1;
669 }
670
671 moduleName = PyObject_GetAttrString(t, "__module__");
Brett Cannonbf364092006-03-01 04:25:17 +0000672 if (moduleName == NULL)
673 PyFile_WriteString("<unknown>", f);
674 else {
Neal Norwitzcd795962007-08-24 19:54:13 +0000675 char* modstr = PyUnicode_AsString(moduleName);
Neal Norwitz2633c692007-02-26 22:22:47 +0000676 if (modstr &&
Georg Brandl1a3284e2007-12-02 09:40:06 +0000677 strcmp(modstr, "builtins") != 0)
Brett Cannonbf364092006-03-01 04:25:17 +0000678 {
679 PyFile_WriteString(modstr, f);
680 PyFile_WriteString(".", f);
681 }
682 }
683 if (className == NULL)
684 PyFile_WriteString("<unknown>", f);
685 else
686 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000687 if (v && v != Py_None) {
688 PyFile_WriteString(": ", f);
689 PyFile_WriteObject(v, f, 0);
690 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000692 }
693 PyFile_WriteString(" in ", f);
694 PyFile_WriteObject(obj, f, 0);
695 PyFile_WriteString(" ignored\n", f);
696 PyErr_Clear(); /* Just in case */
697 }
698 Py_XDECREF(t);
699 Py_XDECREF(v);
700 Py_XDECREF(tb);
701}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000702
Armin Rigo092381a2003-10-25 14:29:27 +0000703extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000704
Guido van Rossum2fd45652001-02-28 21:46:24 +0000705
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000706/* Set file and line information for the current exception.
707 If the exception is not a SyntaxError, also sets additional attributes
708 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000709
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000710void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000711PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000712{
713 PyObject *exc, *v, *tb, *tmp;
714
715 /* add attributes for the line number and filename for the error */
716 PyErr_Fetch(&exc, &v, &tb);
717 PyErr_NormalizeException(&exc, &v, &tb);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000718 /* XXX check that it is, indeed, a syntax error. It might not
719 * be, though. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000720 tmp = PyLong_FromLong(lineno);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000721 if (tmp == NULL)
722 PyErr_Clear();
723 else {
724 if (PyObject_SetAttrString(v, "lineno", tmp))
725 PyErr_Clear();
726 Py_DECREF(tmp);
727 }
728 if (filename != NULL) {
Neal Norwitzcd795962007-08-24 19:54:13 +0000729 tmp = PyUnicode_FromString(filename);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000730 if (tmp == NULL)
731 PyErr_Clear();
732 else {
733 if (PyObject_SetAttrString(v, "filename", tmp))
734 PyErr_Clear();
735 Py_DECREF(tmp);
736 }
737
738 tmp = PyErr_ProgramText(filename, lineno);
739 if (tmp) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000740 if (PyObject_SetAttrString(v, "text", tmp))
741 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000742 Py_DECREF(tmp);
743 }
744 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000745 if (PyObject_SetAttrString(v, "offset", Py_None)) {
746 PyErr_Clear();
747 }
748 if (exc != PyExc_SyntaxError) {
749 if (!PyObject_HasAttrString(v, "msg")) {
750 tmp = PyObject_Str(v);
751 if (tmp) {
752 if (PyObject_SetAttrString(v, "msg", tmp))
753 PyErr_Clear();
754 Py_DECREF(tmp);
755 } else {
756 PyErr_Clear();
757 }
758 }
759 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
760 if (PyObject_SetAttrString(v, "print_file_and_line",
761 Py_None))
762 PyErr_Clear();
763 }
764 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000765 PyErr_Restore(exc, v, tb);
766}
767
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000768/* Attempt to load the line of text that the exception refers to. If it
769 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000770
771 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000772 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000773
774PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000775PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000776{
777 FILE *fp;
778 int i;
779 char linebuf[1000];
780
Tim Petersa7444f42006-02-27 23:29:46 +0000781 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000782 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000783 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000784 if (fp == NULL)
785 return NULL;
786 for (i = 0; i < lineno; i++) {
787 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
788 do {
789 *pLastChar = '\0';
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000790 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
791 fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000792 break;
793 /* fgets read *something*; if it didn't get as
794 far as pLastChar, it must have found a newline
Thomas Wouters477c8d52006-05-27 19:21:47 +0000795 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000796 it obviously found a newline; else we haven't
797 yet seen a newline, so must continue */
798 } while (*pLastChar != '\0' && *pLastChar != '\n');
799 }
800 fclose(fp);
801 if (i == lineno) {
802 char *p = linebuf;
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000803 PyObject *res;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000804 while (*p == ' ' || *p == '\t' || *p == '\014')
805 p++;
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000806 res = PyUnicode_FromString(p);
807 if (res == NULL)
808 PyErr_Clear();
809 return res;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000810 }
811 return NULL;
812}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000813
814#ifdef __cplusplus
815}
816#endif