blob: e0ff90f29de10f3b0768cbea40e573ce9174b2dc [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
Guido van Rossum743007d1999-04-21 15:27:31 +000013#include "windows.h"
14#include "winbase.h"
15#endif
16
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000017#include <ctype.h>
18
Anthony Baxterac6bd462006-04-13 02:06:09 +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 Rossum373c8691997-04-29 18:22:47 +000055 Py_XINCREF(exception);
56 Py_XINCREF(value);
57 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058}
59
60void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062{
Guido van Rossum373c8691997-04-29 18:22:47 +000063 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064}
65
66void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000067PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068{
Gregory P. Smithdd96db62008-06-09 04:58:54 +000069 PyObject *value = PyString_FromString(string);
Guido van Rossum373c8691997-04-29 18:22:47 +000070 PyErr_SetObject(exception, value);
71 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossum3a241811994-08-29 12:14:12 +000074
Guido van Rossum373c8691997-04-29 18:22:47 +000075PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000076PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Tim Peters024da352001-05-30 06:09:50 +000078 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000079
80 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
Barry Warsawc0dc92a1997-08-22 21:22:58 +000083
84int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +000086{
Barry Warsawfa5c3152000-05-02 19:27:51 +000087 if (err == NULL || exc == NULL) {
88 /* maybe caused by "import exceptions" that failed early on */
89 return 0;
90 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +000091 if (PyTuple_Check(exc)) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +000092 Py_ssize_t i, n;
Barry Warsawc0dc92a1997-08-22 21:22:58 +000093 n = PyTuple_Size(exc);
94 for (i = 0; i < n; i++) {
95 /* Test recursively */
96 if (PyErr_GivenExceptionMatches(
97 err, PyTuple_GET_ITEM(exc, i)))
98 {
99 return 1;
100 }
101 }
102 return 0;
103 }
104 /* err might be an instance, so check its class. */
Brett Cannonbf364092006-03-01 04:25:17 +0000105 if (PyExceptionInstance_Check(err))
106 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000107
Brett Cannonbf364092006-03-01 04:25:17 +0000108 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
Georg Brandl4b5c53a2009-04-02 02:47:44 +0000109 int res = 0, reclimit;
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000110 PyObject *exception, *value, *tb;
111 PyErr_Fetch(&exception, &value, &tb);
Georg Brandl4b5c53a2009-04-02 02:47:44 +0000112 /* Temporarily bump the recursion limit, so that in the most
113 common case PyObject_IsSubclass will not raise a recursion
114 error we have to ignore anyway. */
115 reclimit = Py_GetRecursionLimit();
116 Py_SetRecursionLimit(reclimit + 5);
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000117 res = PyObject_IsSubclass(err, exc);
Georg Brandl4b5c53a2009-04-02 02:47:44 +0000118 Py_SetRecursionLimit(reclimit);
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000119 /* This function must not fail, so print the error here */
120 if (res == -1) {
121 PyErr_WriteUnraisable(err);
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000122 res = 0;
123 }
124 PyErr_Restore(exception, value, tb);
125 return res;
Brett Cannonbf364092006-03-01 04:25:17 +0000126 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000127
128 return err == exc;
129}
Guido van Rossum743007d1999-04-21 15:27:31 +0000130
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000131
132int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000133PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000134{
135 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
136}
137
138
139/* Used in many places to normalize a raised exception, including in
140 eval_code2(), do_raise(), and PyErr_Print()
141*/
142void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000143PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000144{
145 PyObject *type = *exc;
146 PyObject *value = *val;
147 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000148 PyObject *initial_tb = NULL;
Brett Cannon1e534b52007-09-07 04:18:30 +0000149 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000150
Guido van Rossumed473a42000-08-07 19:18:27 +0000151 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000152 /* There was no exception, so nothing to do. */
153 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000154 }
155
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000156 /* If PyErr_SetNone() was used, the value will have been actually
157 set to NULL.
158 */
159 if (!value) {
160 value = Py_None;
161 Py_INCREF(value);
162 }
163
Brett Cannonbf364092006-03-01 04:25:17 +0000164 if (PyExceptionInstance_Check(value))
165 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000166
167 /* Normalize the exception so that if the type is a class, the
168 value will be an instance.
169 */
Brett Cannonbf364092006-03-01 04:25:17 +0000170 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000171 /* if the value was not an instance, or is not an instance
172 whose class is (or is derived from) type, then use the
173 value as an argument to instantiation of the type
174 class.
175 */
Brett Cannonbf364092006-03-01 04:25:17 +0000176 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000177 PyObject *args, *res;
178
179 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000180 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000181 else if (PyTuple_Check(value)) {
182 Py_INCREF(value);
183 args = value;
184 }
185 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000186 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000187
188 if (args == NULL)
189 goto finally;
190 res = PyEval_CallObject(type, args);
191 Py_DECREF(args);
192 if (res == NULL)
193 goto finally;
194 Py_DECREF(value);
195 value = res;
196 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000197 /* if the class of the instance doesn't exactly match the
198 class of the type, believe the instance
199 */
200 else if (inclass != type) {
201 Py_DECREF(type);
202 type = inclass;
203 Py_INCREF(type);
204 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000205 }
206 *exc = type;
207 *val = value;
208 return;
209finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000210 Py_DECREF(type);
211 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000212 /* If the new exception doesn't set a traceback and the old
213 exception had a traceback, use the old traceback for the
214 new exception. It's better than nothing.
215 */
216 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000217 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000218 if (initial_tb != NULL) {
219 if (*tb == NULL)
220 *tb = initial_tb;
221 else
222 Py_DECREF(initial_tb);
223 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000224 /* normalize recursively */
Brett Cannon1e534b52007-09-07 04:18:30 +0000225 tstate = PyThreadState_GET();
226 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
227 --tstate->recursion_depth;
Georg Brandld3f03fa2009-04-02 18:09:04 +0000228 /* throw away the old exception... */
229 Py_DECREF(*exc);
230 Py_DECREF(*val);
231 /* ... and use the recursion error instead */
232 *exc = PyExc_RuntimeError;
233 *val = PyExc_RecursionErrorInst;
234 Py_INCREF(*exc);
235 Py_INCREF(*val);
236 /* just keeping the old traceback */
Brett Cannon1e534b52007-09-07 04:18:30 +0000237 return;
238 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000239 PyErr_NormalizeException(exc, val, tb);
Brett Cannon1e534b52007-09-07 04:18:30 +0000240 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000241}
242
243
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000245PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000247 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000248
249 *p_type = tstate->curexc_type;
250 *p_value = tstate->curexc_value;
251 *p_traceback = tstate->curexc_traceback;
252
253 tstate->curexc_type = NULL;
254 tstate->curexc_value = NULL;
255 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256}
257
258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000261 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000263
264/* Convenience functions to set a type error exception and return 0 */
265
266int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000267PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000268{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000269 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000270 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000271 return 0;
272}
273
Guido van Rossum373c8691997-04-29 18:22:47 +0000274PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000276{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000277 if (PyErr_ExceptionMatches(PyExc_MemoryError))
278 /* already current */
279 return NULL;
280
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000281 /* raise the pre-allocated instance if it still exists */
282 if (PyExc_MemoryErrorInst)
283 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
284 else
285 /* this will probably fail since there's no memory and hee,
286 hee, we have to instantiate this class
287 */
288 PyErr_SetNone(PyExc_MemoryError);
289
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000290 return NULL;
291}
292
Guido van Rossum373c8691997-04-29 18:22:47 +0000293PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000294PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000295{
Guido van Rossum373c8691997-04-29 18:22:47 +0000296 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000297 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000298 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000299#ifdef PLAN9
300 char errbuf[ERRMAX];
301#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000302#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000303 char *s_buf = NULL;
Mark Hammond3d61a062002-10-04 00:13:02 +0000304 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000305#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000306#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000307 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000308 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000309#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000310#ifdef PLAN9
311 rerrstr(errbuf, sizeof errbuf);
312 s = errbuf;
313#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000314 if (i == 0)
315 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000316 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000317#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000318 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000319#else
320 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000321 /* Note that the Win32 errors do not lineup with the
322 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000323 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000324 a Win32 error code
325 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000326 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000327 s = _sys_errlist[i];
328 }
329 else {
330 int len = FormatMessage(
331 FORMAT_MESSAGE_ALLOCATE_BUFFER |
332 FORMAT_MESSAGE_FROM_SYSTEM |
333 FORMAT_MESSAGE_IGNORE_INSERTS,
334 NULL, /* no message source */
335 i,
336 MAKELANGID(LANG_NEUTRAL,
337 SUBLANG_DEFAULT),
338 /* Default language */
339 (LPTSTR) &s_buf,
340 0, /* size not used */
341 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000342 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000343 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000344 situations */
345 sprintf(s_small_buf, "Windows Error 0x%X", i);
346 s = s_small_buf;
347 s_buf = NULL;
348 } else {
349 s = s_buf;
350 /* remove trailing cr/lf and dots */
351 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
352 s[--len] = '\0';
353 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000354 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000355 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000356#endif /* Unix/Windows */
357#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000358 if (filenameObject != NULL)
359 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000360 else
361 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000362 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000363 PyErr_SetObject(exc, v);
364 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000365 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000366#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000367 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000368#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000369 return NULL;
370}
Guido van Rossum743007d1999-04-21 15:27:31 +0000371
Barry Warsaw97d95151998-07-23 16:05:56 +0000372
373PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000374PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
375{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000376 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000377 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000378 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000379 return result;
380}
381
382#ifdef Py_WIN_WIDE_FILENAMES
383PyObject *
384PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
385{
Brett Cannonbf364092006-03-01 04:25:17 +0000386 PyObject *name = filename ?
387 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000388 NULL;
389 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
390 Py_XDECREF(name);
391 return result;
392}
393#endif /* Py_WIN_WIDE_FILENAMES */
394
395PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000396PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000397{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000398 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000399}
Guido van Rossum683a0721990-10-21 22:09:12 +0000400
Brett Cannonbf364092006-03-01 04:25:17 +0000401#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000402/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000403PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000404 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000406 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000407{
408 int len;
409 char *s;
Mark Hammond3d61a062002-10-04 00:13:02 +0000410 char *s_buf = NULL; /* Free via LocalFree */
411 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000412 PyObject *v;
413 DWORD err = (DWORD)ierr;
414 if (err==0) err = GetLastError();
415 len = FormatMessage(
416 /* Error API error */
417 FORMAT_MESSAGE_ALLOCATE_BUFFER |
418 FORMAT_MESSAGE_FROM_SYSTEM |
419 FORMAT_MESSAGE_IGNORE_INSERTS,
420 NULL, /* no message source */
421 err,
422 MAKELANGID(LANG_NEUTRAL,
423 SUBLANG_DEFAULT), /* Default language */
Mark Hammond3d61a062002-10-04 00:13:02 +0000424 (LPTSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000425 0, /* size not used */
426 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000427 if (len==0) {
428 /* Only seen this in out of mem situations */
429 sprintf(s_small_buf, "Windows Error 0x%X", err);
430 s = s_small_buf;
431 s_buf = NULL;
432 } else {
433 s = s_buf;
434 /* remove trailing cr/lf and dots */
435 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
436 s[--len] = '\0';
437 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000438 if (filenameObject != NULL)
439 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000440 else
441 v = Py_BuildValue("(is)", err, s);
442 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000443 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000444 Py_DECREF(v);
445 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000446 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000447 return NULL;
448}
449
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450PyObject *PyErr_SetExcFromWindowsErrWithFilename(
451 PyObject *exc,
452 int ierr,
453 const char *filename)
454{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000455 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000456 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
457 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000458 name);
459 Py_XDECREF(name);
460 return ret;
461}
462
463#ifdef Py_WIN_WIDE_FILENAMES
464PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
465 PyObject *exc,
466 int ierr,
467 const Py_UNICODE *filename)
468{
Brett Cannonbf364092006-03-01 04:25:17 +0000469 PyObject *name = filename ?
470 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000471 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000472 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
473 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000474 name);
475 Py_XDECREF(name);
476 return ret;
477}
478#endif /* Py_WIN_WIDE_FILENAMES */
479
Thomas Heller085358a2002-07-29 14:27:41 +0000480PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
481{
482 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
483}
484
Guido van Rossum795e1892000-02-17 15:19:15 +0000485PyObject *PyErr_SetFromWindowsErr(int ierr)
486{
Thomas Heller085358a2002-07-29 14:27:41 +0000487 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
488 ierr, NULL);
489}
490PyObject *PyErr_SetFromWindowsErrWithFilename(
491 int ierr,
492 const char *filename)
493{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000494 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000495 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
496 PyExc_WindowsError,
497 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000498 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000499 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000500}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000501
502#ifdef Py_WIN_WIDE_FILENAMES
503PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
504 int ierr,
505 const Py_UNICODE *filename)
506{
Brett Cannonbf364092006-03-01 04:25:17 +0000507 PyObject *name = filename ?
508 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000509 NULL;
510 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
511 PyExc_WindowsError,
512 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000513 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000514 return result;
515}
516#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000517#endif /* MS_WINDOWS */
518
Guido van Rossum683a0721990-10-21 22:09:12 +0000519void
Fred Drake6d63adf2000-08-24 22:38:39 +0000520_PyErr_BadInternalCall(char *filename, int lineno)
521{
522 PyErr_Format(PyExc_SystemError,
523 "%s:%d: bad argument to internal function",
524 filename, lineno);
525}
526
527/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
528 export the entry point for existing object code: */
529#undef PyErr_BadInternalCall
530void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000532{
Fred Drake6d63adf2000-08-24 22:38:39 +0000533 PyErr_Format(PyExc_SystemError,
534 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000535}
Fred Drake6d63adf2000-08-24 22:38:39 +0000536#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
537
Guido van Rossum1548bac1997-02-14 17:09:47 +0000538
539
Guido van Rossum1548bac1997-02-14 17:09:47 +0000540PyObject *
541PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000542{
543 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000544 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000545
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000546#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000547 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000548#else
549 va_start(vargs);
550#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000551
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000552 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000553 PyErr_SetObject(exception, string);
554 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000555 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000556 return NULL;
557}
Guido van Rossum7617e051997-09-16 18:43:50 +0000558
559
Georg Brandl658d5132006-05-23 11:17:21 +0000560
Guido van Rossum7617e051997-09-16 18:43:50 +0000561PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000562PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000563{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000564 char *dot;
565 PyObject *modulename = NULL;
566 PyObject *classname = NULL;
567 PyObject *mydict = NULL;
568 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000569 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000570 dot = strrchr(name, '.');
571 if (dot == NULL) {
572 PyErr_SetString(PyExc_SystemError,
573 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000574 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000575 }
576 if (base == NULL)
577 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000578 if (dict == NULL) {
579 dict = mydict = PyDict_New();
580 if (dict == NULL)
581 goto failure;
582 }
583 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000584 modulename = PyString_FromStringAndSize(name,
Armin Rigo7ccbca92006-10-04 12:17:45 +0000585 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000586 if (modulename == NULL)
587 goto failure;
588 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
589 goto failure;
590 }
Georg Brandl658d5132006-05-23 11:17:21 +0000591 if (PyTuple_Check(base)) {
592 bases = base;
593 /* INCREF as we create a new ref in the else branch */
594 Py_INCREF(bases);
595 } else {
596 bases = PyTuple_Pack(1, base);
597 if (bases == NULL)
598 goto failure;
599 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000600 /* Create a real new-style class. */
601 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
602 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000603 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000604 Py_XDECREF(bases);
605 Py_XDECREF(mydict);
606 Py_XDECREF(classname);
607 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000608 return result;
609}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000610
611/* Call when an exception has occurred but there is no way for Python
612 to handle it. Examples: exception in __del__ or during GC. */
613void
614PyErr_WriteUnraisable(PyObject *obj)
615{
616 PyObject *f, *t, *v, *tb;
617 PyErr_Fetch(&t, &v, &tb);
618 f = PySys_GetObject("stderr");
619 if (f != NULL) {
620 PyFile_WriteString("Exception ", f);
621 if (t) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000622 PyObject* moduleName;
Neal Norwitzf83b7512007-02-26 23:48:27 +0000623 char* className;
624 assert(PyExceptionClass_Check(t));
625 className = PyExceptionClass_Name(t);
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000626 if (className != NULL) {
627 char *dot = strrchr(className, '.');
628 if (dot != NULL)
629 className = dot+1;
630 }
631
632 moduleName = PyObject_GetAttrString(t, "__module__");
Brett Cannonbf364092006-03-01 04:25:17 +0000633 if (moduleName == NULL)
634 PyFile_WriteString("<unknown>", f);
635 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000636 char* modstr = PyString_AsString(moduleName);
Neal Norwitz88516a62007-02-26 22:41:45 +0000637 if (modstr &&
638 strcmp(modstr, "exceptions") != 0)
Brett Cannonbf364092006-03-01 04:25:17 +0000639 {
640 PyFile_WriteString(modstr, f);
641 PyFile_WriteString(".", f);
642 }
643 }
644 if (className == NULL)
645 PyFile_WriteString("<unknown>", f);
646 else
647 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000648 if (v && v != Py_None) {
649 PyFile_WriteString(": ", f);
650 PyFile_WriteObject(v, f, 0);
651 }
Neal Norwitz1a269202006-04-17 00:33:23 +0000652 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000653 }
654 PyFile_WriteString(" in ", f);
655 PyFile_WriteObject(obj, f, 0);
656 PyFile_WriteString(" ignored\n", f);
657 PyErr_Clear(); /* Just in case */
658 }
659 Py_XDECREF(t);
660 Py_XDECREF(v);
661 Py_XDECREF(tb);
662}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000663
Armin Rigo092381a2003-10-25 14:29:27 +0000664extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000665
Guido van Rossum2fd45652001-02-28 21:46:24 +0000666
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000667/* Set file and line information for the current exception.
668 If the exception is not a SyntaxError, also sets additional attributes
669 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000670
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000671void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000672PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000673{
674 PyObject *exc, *v, *tb, *tmp;
675
676 /* add attributes for the line number and filename for the error */
677 PyErr_Fetch(&exc, &v, &tb);
678 PyErr_NormalizeException(&exc, &v, &tb);
Richard Jones7b9558d2006-05-27 12:29:24 +0000679 /* XXX check that it is, indeed, a syntax error. It might not
680 * be, though. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000681 tmp = PyInt_FromLong(lineno);
682 if (tmp == NULL)
683 PyErr_Clear();
684 else {
685 if (PyObject_SetAttrString(v, "lineno", tmp))
686 PyErr_Clear();
687 Py_DECREF(tmp);
688 }
689 if (filename != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000690 tmp = PyString_FromString(filename);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000691 if (tmp == NULL)
692 PyErr_Clear();
693 else {
694 if (PyObject_SetAttrString(v, "filename", tmp))
695 PyErr_Clear();
696 Py_DECREF(tmp);
697 }
698
699 tmp = PyErr_ProgramText(filename, lineno);
700 if (tmp) {
Georg Brandla1121fa2006-05-29 14:13:21 +0000701 if (PyObject_SetAttrString(v, "text", tmp))
702 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000703 Py_DECREF(tmp);
704 }
705 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000706 if (PyObject_SetAttrString(v, "offset", Py_None)) {
707 PyErr_Clear();
708 }
709 if (exc != PyExc_SyntaxError) {
710 if (!PyObject_HasAttrString(v, "msg")) {
711 tmp = PyObject_Str(v);
712 if (tmp) {
713 if (PyObject_SetAttrString(v, "msg", tmp))
714 PyErr_Clear();
715 Py_DECREF(tmp);
716 } else {
717 PyErr_Clear();
718 }
719 }
720 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
721 if (PyObject_SetAttrString(v, "print_file_and_line",
722 Py_None))
723 PyErr_Clear();
724 }
725 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000726 PyErr_Restore(exc, v, tb);
727}
728
729/* com_fetch_program_text will attempt to load the line of text that
730 the exception refers to. If it fails, it will return NULL but will
Brett Cannonbf364092006-03-01 04:25:17 +0000731 not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000732
733 XXX The functionality of this function is quite similar to the
734 functionality in tb_displayline() in traceback.c.
735*/
736
737PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000738PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000739{
740 FILE *fp;
741 int i;
742 char linebuf[1000];
743
Tim Petersa7444f42006-02-27 23:29:46 +0000744 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000745 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000746 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000747 if (fp == NULL)
748 return NULL;
749 for (i = 0; i < lineno; i++) {
750 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
751 do {
752 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000753 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000754 break;
755 /* fgets read *something*; if it didn't get as
756 far as pLastChar, it must have found a newline
Walter Dörwaldc611f172006-05-25 08:53:28 +0000757 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000758 it obviously found a newline; else we haven't
759 yet seen a newline, so must continue */
760 } while (*pLastChar != '\0' && *pLastChar != '\n');
761 }
762 fclose(fp);
763 if (i == lineno) {
764 char *p = linebuf;
765 while (*p == ' ' || *p == '\t' || *p == '\014')
766 p++;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000767 return PyString_FromString(p);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000768 }
769 return NULL;
770}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000771
772#ifdef __cplusplus
773}
774#endif
775